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.

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.

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.

  8. Tj says:

    It’s working perfect! It deletes the files from sub folders also.
    Thanks for the posting.

  9. Jacob Smith says:

    I never knew this before I only knew it from you, this is such a brilliant idea ever. I use to delete my files in a simple way. I never thought about this one.

  10. Is forfiles a safe exe?

  11. Wow.. I never knew about this. Thanks for sharing this. Awesome!

  12. David says:

    Hi, Thanks for the command.

    BUT, when I use it I get the message that it cannot find the file, yet it lists the actual file it tells me it cannot find.
    I’m sure it’s a simple problem, I would appreciate any feedback.

    Forfiles /p C:\Work\Stuff\Temp /m *.log /d -30 /C “cmd /c del @file”

    Could Not Find C:\Work\Stuff\Temp\ex110701.log

  13. amit says:

    Thanks a ton… !!! I have been finding this.. and got everything except this great code..

  14. Jason says:

    Nice I can use this to clean out old IE cookies from our citrix profiles.


  15. Thomas says:

    Forfiles is a great command. I have only known it for 6 month or so. I had a folder with so many log files that it took minutes to open it in explore. Then I made a script with forfiles.exe that deleted all files older than 30 days. I configured it to run every night as a scheduled job. Now I don’t need to think about those log files. It is all running automatic. You can’t do that with a GUI 😉

    • Paul Salmon says:

      Running automatically is where small files like forefiles can excel. While GUI may be great for manual deletion, sometimes you just need to run a scheduled job once a day to automatically do the cleanup for you.

  16. Sheila says:

    I haven’t used DOS in years. I’ve become so used to the GUI that I just sort the files into date order and do it that way.

  17. Dana says:

    Thanks for the tips. I think I can utilize it into script (crontab job) to do it automatically.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.