Delete Files Older Than a Number of Days From DOS

Delete Files Older Than a Number of Days From DOS

With the operating systems available today, using the graphical user interface (GUI) of the operating system has become second-nature for me. It is very easy to copy, move, and delete files through the GUI interface than typing out the commands from a command line.

Unlimited data backup with BackBlaze

With all the user interface options in an operating system, there are many times where it can be more efficient to use the command line. In one such case, I wanted to create a simple method of deleting files that were older than seven days from a specified directory. While I could have written an application to do this, I wanted to make it more simple by using a batch file. With an additional executable available with the Windows 2003 SDK, I can easily delete files older than a number of days from DOS.

Deleting Files From DOS Older Than Seven Days

Delete Files Older Than a Number of Days From DOS

There is a small executable called ForeFiles that can easily allow you to get the files in a directory that meet certain time criteria. In my case I want to find all files that were older than 7 days.

To do this, I added the following command to a batch file:

forfiles.exe /p D:\Files /s /m *.* /d -7 /c "cmd /c del @file"

The above line executes the forefiles.exe program with the following parameters:

Parameter Description
/p This parameter specifies the path that contain the files I wish to delete.
/s This parameter tells the program to recurse into any subfolders to look for other files.
/m If you want to specify a specific file type, this parameter will allow you to limit the search to specific files, such as *.doc for Word documents. In my case, I looked for all files (*.*).
/d This one is the key parameter – it specifies the last modified date value. In my example I specify “-7” which indicates that the files need to have a modified date 7 days less than the current date.
/c This is the command that I execute on the files found by the program. The delete command is executed in a command window for each file.

While there are many ways to perform such an action through other applications, some times I find using batch files, and then scheduling them to run, can be just as easy.

Get free email updates! Sign up for new post notifications.

26 Responses to “Delete Files Older Than a Number of Days From DOS”

  1. Stanley says:

    Hi Paul,
    I need an enhanced version of this functionality and was wondering if it could still be done with forfiles.

    I need to move the files found to another folder structure like the one we are searching in.

    i.e. If we have a file in directory “e:\temp1\aa\bb”
    I want the program to move it to: “f:\temp1\aa\bb”

    Thanks very much for your assistance.

  2. George says:

    Any chance this could be made to work with UNC paths?

  3. Sid says:

    Thanks paul, and to you all for sharing your experiences. It worked pretty well for me, but I’ve rather a difficult task at hand. I need to identify files (delete later) in a subfolder.
    Here’s the case: I’ve around 200 users on my c drive and every user has a subfolder called music. I need to identify all the files in those subdirectories older than 365 days.

    .. etc

    forfiles /p c\users\nick\music\ /m *.mp3 /d -375 /c “cmd /c echo @file is an old song”

    The above command is working for sure, but I don’t want to run this 200 times. Is there a way to say: find music folder and identify all files in there.

  4. Dan says:

    I’m getting “Access is denied.” error when using forfiles to delete files, but when I use Windows Explorer to delete there are no errors and able to delete the files. Any help is greatly appreciated. Thanks

  5. Amr AbdelGelil says:

    I need a similar tool for folder

    • found this tip on another forum:

      # Delete files older than 30 days from the y: including any files inside folders
      forfiles /P y:\ /S /M *.* /C “cmd /c del @FILE” /d -30

      # Delete folders older than 30 days from the y:
      forfiles /P y:\ /M *.* /C “cmd /c if @ISDIR==TRUE RMDIR /Q /S @PATH” /d -30

  6. Michelle says:

    So nice of you to share this code! I’ve been struggling on my own to better understand how to manage things better in DOS and this one sure helps me. So thank you a lot, I’ll start scooping and see what else can I learn from here 🙂

  7. Imre says:

    Hi, this does not work for me , mine says this:

    C:\temp>forfiles.exe /p C:\Temp /s /m *.* /d -7 /c “cmd /c del @file
    FORFILES v 1.1 – by Emmanuel Boersma – 4/98

    Syntax : FORFILES [-pPath] [-mSearch Mask] [-ccommand] [-dDDMMYY] [-s]

    -pPath Path where to start searching
    -mSearch Mask Search files according to
    -cCommand Command to execute on each file(s)
    -d[+|-][DDMMYY|DD] Select files with date >= or = or <= (current date – DD days)
    -s Recurse directories
    -v Verbose mode

    The following variables can be used in Command :

    Default :
    Examples :
    FORFILES -pc:\ -s -m*.BAT -c”CMD /C Echo @FILE is a batch file”
    FORFILES -pc:\ -s -m*.* -c”CMD /C if @ISDIR==TRUE echo @FILE is a directory”
    FORFILES -pc:\ -s -m*.* -d-100 -c”CMD /C Echo @FILE : date >= 100 days”
    FORFILES -pc:\ -s -m*.* -d-010193 -c”CMD /C Echo @FILE is quite old!”


    I have been batt,ing for three days now and nobody has managed to help me, maybe here i find somebody that knows whats going on.

    im sure my version of FORFILES.exe is a problem.

Leave a Reply

Your email address will not be published. Required fields are marked *