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.


About Paul Salmon

Paul Salmon is the founder of Technically Easy. He is a an experienced PC user, and enjoys solving computer-related problems that he encounters on a regular basis.

Facebook | Twitter | Google+



  1. Stanley
    Posted December 24, 2014 at 9:58 am | Permalink

    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
    Posted November 14, 2013 at 3:09 pm | Permalink

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

    • Posted November 15, 2013 at 10:39 am | Permalink

      While I haven’t tried, you could possibly try and replace “D:\Files” with “\\servername\files”.

  3. Posted August 10, 2012 at 1:59 pm | Permalink

    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
    Posted August 9, 2012 at 8:45 am | Permalink

    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
    Posted July 13, 2012 at 10:24 am | Permalink

    I need a similar tool for folder

    • Posted August 14, 2012 at 7:32 pm | Permalink

      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
    Posted May 22, 2012 at 4:12 am | Permalink

    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. Posted December 8, 2011 at 2:35 pm | Permalink

    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.

    • Posted December 8, 2011 at 2:47 pm | Permalink

      Have you downloaded the forefiles.exe from the link in this post? That is the same one I used to execute the command.

  8. Tj
    Posted December 7, 2011 at 1:36 pm | Permalink

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

  9. Jacob Smith
    Posted November 16, 2011 at 10:33 pm | Permalink

    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. Posted October 5, 2011 at 4:09 am | Permalink

    Is forfiles a safe exe?

    • Posted October 5, 2011 at 6:00 am | Permalink

      The forefiles executable is safe and is provided by Microsoft with the Windows Resource Kit.

  11. Posted September 20, 2011 at 4:13 am | Permalink

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

  12. Posted August 9, 2011 at 8:30 pm | Permalink

    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
    Posted July 9, 2011 at 3:02 am | Permalink

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

  14. Posted February 27, 2011 at 8:33 pm | Permalink

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


  15. Posted February 24, 2011 at 1:20 pm | Permalink

    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 😉

    • Posted February 24, 2011 at 1:24 pm | Permalink

      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. Posted February 22, 2011 at 10:07 am | Permalink

    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.

    • Posted February 22, 2011 at 12:03 pm | Permalink

      I have also become used to the GUI, but I tend to do a few things from DOS since I can type quicker than I can click.

  17. Posted February 18, 2011 at 8:49 am | Permalink

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

Post a Comment

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


Subscribe without commenting