Home > Windows Server 2003 > Delete files older than certain number of days

Delete files older than certain number of days

I was recently asked to investigate a problem with a server and the lack of space on a partition. After a quick look using Treesize I noticed a suspicious folder being used by an application for logging purposes.

There were over 700,000 files in it! When I tried to browse the folder using explorer it took an absolute age to open as you can imagine. To rectify the problem and recover the majority of the disk space being used by the logs I wanted to delete the contents of the logging folder that was older than 30 days. After a quick search I discovered a command line utility called forfiles.exe that is included with Windows Server 2003. Using forfiles.exe I was able to delete all files older than 30 days like so:

forfiles.exe /p (pathtofilestodelete) /s /m *.* /d -30 /c "cmd /c del /q @path"

A working example is:

forfiles.exe /p d:\logs /s /m *.* /d -30 /c "cmd /c del /q @path"

This will delete ALL files from d:\logs (and all sub folders it contains because /s has been used to force recursion) older than 30 days without prompting you to confirm deletion.

Here is an explanation of the switches I used:

/p = The path to search for the files you want to check the date of and remove
/s = Recurse subdirectories contained within the path specified using /p and check them as well
/m = The search mask to be used for the file type you want to check the date on (*.* being all files)
/d = The date to compare the files against. A standard date type can also be used (dd/mm/yyyy)
/c = The command to be used on a file that matches the /m and /d criteria
/q = Used within /c to instruct the del command to delete files quietly

Note: different operating systems came bundled with different versions of forfiles.exe. You will need to download the appropriate version of forfiles.exe for the operating system you wish to use it on.

forfiles.exe - Windows Server 2000
forfiles.exe - Windows Server 2003
forfiles.exe - Windows Server 2008

Categories: Windows Server 2003 Tags:
  1. Chris
    October 8th, 2008 at 14:09 | #1

    Thanks a lot James,

    Your example worked great for me accross a lot of Terminal Servers and saved me a lot of time.

    inverted out 🙂

  2. Peter Moran
    October 27th, 2008 at 12:19 | #2

    This is just what I was looking for. Thanks for all the info in a easy to digest format.

  3. gerry
    December 8th, 2008 at 14:32 | #3

    Thank you, awesome.

  4. Antony
    December 15th, 2008 at 20:12 | #4

    Thanks, perfect - just what I needed.

  5. Rick
    January 12th, 2009 at 20:37 | #5

    Just what I was looking for, thanks a lot!

  6. Andreu
    January 15th, 2009 at 09:42 | #6

    Hi, great script. But if the file has the A attribute forfile.exe doesn't delete it. Any tip?

  7. James Clements
    January 16th, 2009 at 09:44 | #7

    Hi there Andreu, that's a good question! Ok - so we know you can use forfiles.exe to recursively loop through directories and make changes to files. Instead of using it to delete files older than say 30 says you could use it to set the attributes of files older than 30 days. Here is the command to loop through a directory for all files older than 30 days and remove the 'Archive' attribute (+A) using 'attrib'.

    forfiles.exe /p (pathtofilestodelete) /s /m *.* /d -30 /c "cmd /c attrib -a @path"

  8. rc
    January 22nd, 2009 at 23:16 | #8

    for some reason i'm getting an error when running your working example.

    Error: Invalid Syntax '/c' option is not allowed more than '1' time(s)

    When i run this.

    forfiles.exe /P c:\mypath /S /M *.* /D -30 /C “cmd /c del /q @path”

  9. James Clements
    January 23rd, 2009 at 14:52 | #9

    Paste the above line into notepad. Delete each of the speech marks and replace them within notepad. You will notice they are infact a different character. Now paste the new line into a command prompt and try it.

  10. Chip
    February 20th, 2009 at 18:24 | #10

    This awesome, can you explain the @Path???? Is that to hide the path on the screen?

  11. James Clements
    February 23rd, 2009 at 10:30 | #11

    Hey Chip, @path is used by forfiles.exe to return the full path of the current file that matches the criteria (e.g. older than 30 days) that you have specified.

    So, forfiles.exe will check each and every file/folder one at a time. Then, each time it finds a match it invokes the delete command and uses the @path to give the delete command the full path of the file that needs to be deleted.

    Essentially you *almost* have a program - forfiles.exe running another program - a command prompt using the delete command. The reason we use @path is because forfiles.exe knows where the path to the file that needs to be deleted is but the command prompt running the delete command doesn't.

  12. Yakov Kagan
    March 17th, 2009 at 08:22 | #12

    When running the following (on an actually large multi-level tree with about 1000000 subfolders):

    forfiles /pc:\mypath /s /m*.tif /d-7 /c"cmd /c del /Q @PATH"

    I get an error:

    Invalid name (recursive set) line 348

    Can you help?

  13. James Clements
    March 17th, 2009 at 15:38 | #13

    Hi Yakov. The syntax is slightly wrong there. try this:

    forfiles /p c:\mypath /s /m *.tif /d -7 /c "cmd /c del /q @path"

    Does the error appear straight away? or a little while into the process?

  14. Mihai Vlad
    March 19th, 2009 at 09:16 | #14

    Hy James

    When i run your script into cmd I get this:

    'forfiles.exe' is not recognized as an internal or external command,
    operable program or batch file.

    Can you help?

  15. James Clements
    March 19th, 2009 at 09:42 | #15

    Hi Mihai, forfiles.exe is only included on Windows Server 2003/2008 and Windows Vista. If you are not using one of these operating systems then you will need to copy the file forfiles.exe from one of the above operating systems from here:

    C:\Windows\System32

  16. Mihai Vlad
    March 19th, 2009 at 14:06 | #16

    thanks a lot 😉

  17. PS
    April 10th, 2009 at 11:02 | #17

    Can u tel me how many subdirectories it will support. I want to delete 7 days older files but not able to execute above commad

    Error Message

    ERROR: Invalid syntax. '/c' option is not allowed more than '1' time(s)
    Type "FORFILES /?" for usage.

  18. James Clements
    April 11th, 2009 at 14:16 | #18

    Hey PS,

    Make sure you use the correct speech marks, paste the command into notepad and delete the speech mark characters. Then replace them in notepad and you will see they are a different character.

  19. Bilal
    April 28th, 2009 at 11:28 | #19

    Thanks it worked

  20. John
    April 28th, 2009 at 17:28 | #20

    works great, thanks

  21. Ron
    June 5th, 2009 at 21:24 | #21

    Thanks so much James. Wasn't aware of the forfile.exe utility - what a handy little thing!

  22. Peppe
    June 9th, 2009 at 10:52 | #22

    Thanks a lot!!!

  23. Ron
    June 10th, 2009 at 10:47 | #23

    Cheers! Worked first time!

  24. unixGuy
    June 15th, 2009 at 17:16 | #24

    "ERROR: UNC paths (\\machine\share) are not supported."

    haven't seen such a stupid tool lately. seems like m$ keep sticking to 'good ol dos' drive letters...

  25. James Clements
    June 15th, 2009 at 19:59 | #25

    unixGuy, I understand your frustration! Of course the simple workaround here is to map a drive on your PC. You probably knew that already 🙂

  26. Jasbir Singh
    June 17th, 2009 at 09:22 | #26

    Dear James
    Thanks for highlighting such a beautiful tool.But i have one issue.When im trying to run this tool it is getting executes without any error but it is not deleting any of the files.I used below sytax:

    forfiles.exe /pc:\test /s /m*.* /d-45 /c "cmd /c del /q @path"

    And also i have many files older than 45 days.

    Please Check.

    Thanks & Regards
    Jasbir Singh

  27. James Clements
    June 17th, 2009 at 11:02 | #27

    Jasbir, when pasting your syntax into notepad it became immediately clear that you had missed some spaces out between some of the command line arguments. This is what it should look like:

    forfiles.exe /p c:\test /s /m *.* /d -45 /c "cmd /c del /q @path"

  28. Wendy
    July 9th, 2009 at 14:24 | #28

    I want to delete files older than 3 years. would I use
    forfiles.exe /p c:\mypath /s /m *.* /d -01/01/2006 /c "cmd /c del /q @path"

  29. James Clements
    July 9th, 2009 at 16:29 | #29

    Hey Wendy, you are almost right. Forfiles.exe does support full dates in the format:

    +dd/MM/yyyy
    (this is for files with a modified date of greater than or equal to)

    or

    -dd/MM/yyyy
    (this is for files with a modified date of less than or equal to)

    But the quickest way for you would be to use the number of days in a year multiplied by the number of years you wish to delete. So, 365x3= 1095 days.

    So you could use this:

    forfiles.exe /p c:\mypath /s /m *.* /d -1095 /c "cmd /c del /q @path"

  30. Bibmek
    July 31st, 2009 at 09:18 | #30

    Ok. Forfiles work fine. I want logfile in another folder how many files was deleted.

  31. john
    September 24th, 2009 at 15:40 | #31

    can you use this on unc paths or mapped network drives?

  32. James Clements
    September 25th, 2009 at 14:38 | #32

    John,

    Only on a mapped drive I'm afraid. But then it only takes a second to map a drive to a UNC 🙂

  33. Hooner
    October 29th, 2009 at 20:54 | #33

    Hi James, this is a great tool, thanks for sharing.

    Is there a way of telling forfiles to delete any subdirectory trees it comes across within the target directory as well as the files contained within them.

    Or am I asking too much.

    Thx

  34. Khanyi
    November 2nd, 2009 at 12:58 | #34

    Gr8.....This is good...Thnx!!

  35. Richard
    December 18th, 2009 at 17:22 | #35

    Is there a way to bypass the recycle bin when deleting files using Forfiles?

  36. Richard
    December 21st, 2009 at 16:39 | #36

    With respect to my previous post, when I ran the script manually, the deleted files went to the recycle bin. Yet when it ran from within Scheduled Tasks this weekend, it deleted them permanently, bypassing the RB.

    So, it works fine and is a very handy utility. Thanks for the tips.

  37. dreamer
    December 29th, 2009 at 09:24 | #37

    nice one m8!

    saved me alot of time

    thx

  38. Praveen
    January 11th, 2010 at 12:27 | #38

    Hi,

    I tried to running this in my system(WIN-XP operating system), for some reason it is NOT deleting any files.
    I didn't had forfiles.exe in my system, I downloaded from the net and placed it in D:\forfiles.exe and tried to run forfiles command from command promt, but no success.

    Thanks

  39. Dave
    February 22nd, 2010 at 19:01 | #39

    Praveen, i'm in the same boat as you..but when i ran it on my windows 2003 machine it worked fine...i think it's something to do with the forfiles not being part of XP

  40. Ralph
    March 17th, 2010 at 18:06 | #40

    AWESOME !!! Just what I was looking for !!!!

  41. lebisol
    March 24th, 2010 at 00:16 | #41

    @Praveen
    Same here on the win2000 server....nothing gets deleted.
    Then again...it IS windows... 🙁
    Thanks!

  42. James Clements
    March 24th, 2010 at 14:02 | #42

    Hi lebisol. I believe the issue you are experiencing is caused by the fact you are using forfiles.exe from Windows Server 2003 on Windows Server 2000. I have updated the post to include download links for relevant versions.

  43. lebisol
    March 24th, 2010 at 17:55 | #43

    Hi James,
    Actually it turns out is the the syntax perhaps diff. version for win2000.
    I was using win2000 kit on win2000 adv. server and what did work was:
    forfiles -pC:\Folder\ -m*.* -d-7 -c"cmd /c del /q @FILE"

    Thanks for posting this, it geared me in the right direction.
    Awesome!

  44. April 6th, 2010 at 01:03 | #44

    This tool deletes the files but leaves the folders. So is there a way to delete the folders(empty) as well using this command ?
    Note that the "Modified date" of the folder changes to the command run date once the it has deleted the files.

    I managed to delete the empty folders by following the below links
    http://www.pcmag.com/article2/0,2817,803393,00.asp
    http://blogs.msdn.com/oldnewthing/archive/2008/04/17/8399914.aspx

  45. June 14th, 2010 at 21:32 | #45

    PROBLEM #38: Hi,

    I tried to running this in my system(WIN-XP operating system), for some reason it is NOT deleting any files.
    I didn't had forfiles.exe in my system, I downloaded from the net and placed it in D:\forfiles.exe and tried to run forfiles command from command promt, but no success.

    SOLUTION: for windows XP, use forfiles.exe (for 2003). This works.
    Example: forfiles.exe /P "C:\DATAHOUSED\SORT" /S /M *.vot /C "cmd /c del /q @path"

  46. cici
    July 1st, 2010 at 20:20 | #46

    I am running this on WinServ 2003, but I keep getting this error:
    ERROR: Invalid argument/option - '/q'.
    Type "FORFILES /?" for usage.

    If I take out the /q, then I'll get
    ERROR: Invalid argument/option - '@path'.

    I don't see anything wrong with my command. Any idea?

    Here's my command:
    forfiles.exe /p c:\data\log\ /s /m *.* /d -365 /c "cmd /c del /q @path"

    Thanks in advance

  47. James Clements
    July 2nd, 2010 at 12:44 | #47

    Hi cici. I have just looked and tested what the command that you used above and the syntax is correct. It actually works fine for me! I wonder if there is a problem with the '@' symbol if you have copied/pasted the command? (I have seen issues where the character set appears different when pasting into the command prompt - especially with things like quotes e.t.c)

    I assume you have tried typing it all out as well?

  48. manus
    August 25th, 2010 at 12:05 | #48

    Hi there.
    Thanks for the info.
    I was wandering if there is a way to find the files (older than 2 years in my case) and print them out in text document or something readable to send the customer.
    Thanks in advance.

  49. Stanleigh
    September 26th, 2010 at 04:51 | #49

    Hi James,

    I am trying to archive (zip) all files in a certain directory older than 30 days old. Can you please take a look at my command?

    FORFILES /P C:\FuelsManager\NWA FEDS Interface\NWA Interface Logs\ /S /m *.* /d -30 /c "cmd /c wzzip @file"

    Errors: Invalid argument 'FEDS' (when I include double quotes to counter for the spaces in the path names, I get an 'invalid directory error') Can you please steer me in the right direction?

  50. Stanleigh
    September 26th, 2010 at 04:53 | #50

    James, I forgot to mention that I am working in a Windows 2003 Server environment. Thank you.

    I am trying to archive (zip) all files in a certain directory older than 30 days old. Can you please take a look at my command?

    FORFILES /P C:\FuelsManager\NWA FEDS Interface\NWA Interface Logs\ /S /m *.* /d -30 /c "cmd /c wzzip @file"

    Errors: Invalid argument 'FEDS' (when I include double quotes to counter for the spaces in the path names, I get an 'invalid directory error') Can you please steer me in the right direction?

  51. nobody
    October 6th, 2010 at 14:58 | #51

    @Stanleigh
    Stan, you should modify your command to include quotes around your path:

    FORFILES /P "C:\FuelsManager\NWA FEDS Interface\NWA Interface Logs\" /S /m *.* /d -30 /c "cmd /c wzzip @file"

    good luck

  52. nobody
    October 6th, 2010 at 15:03 | #52

    @manus

    This should do it:

    forfiles /p "(where are you looking?)" /d -730 /s /c "cmd /c echo @file" >> 2_year_old_files.txt

  53. nobody
    October 6th, 2010 at 15:06 | #53

    **edit**

    That command will append the text file. If you want it to create a new one each time, remove one of the 'less than' symbols:

    forfiles /p "(where are you looking?)" /d -730 /s /c "cmd /c echo @file" > 2_year_old_files.txt

  54. dhyermi
    October 8th, 2010 at 09:45 | #54

    @cici
    Normally this /q error is because the path you have specified is invalid in some way

  55. dhyermi
    October 8th, 2010 at 09:45 | #55

    cici :I am running this on WinServ 2003, but I keep getting this error:ERROR: Invalid argument/option - '/q'.Type "FORFILES /?" for usage.
    If I take out the /q, then I'll getERROR: Invalid argument/option - '@path'.
    I don't see anything wrong with my command. Any idea?
    Here's my command:forfiles.exe /p c:\data\log\ /s /m *.* /d -365 /c "cmd /c del /q @path"
    Thanks in advance

    Normally this /q error is because the path you have specified is invalid in some way

  56. Stanleigh
    October 10th, 2010 at 11:54 | #56

    @nobody
    Thank you for your time. After several modifications, the file works as long as there are no sub-directories in the path. Unfortunately, I have files in the sub-directories that need to be zipped.

    The problem now is that the zip is being created in each and every sub-directory (there is a sub-directory for each day of the respective month). I know its gotta' be something simple and it's driving me mad. Thank you for your help!

    @echo off

    cd\
    d:
    cd\Program Files\BizManager\BIZ Feds Interface\BIZ Interface Logs

    FORFILES /P "D:\Program Files\BizManager\BIZ Feds Interface\BIZ Interface Logs"\ /S /M *.* /D -20 /C "cmd /c wzzip -m -rp Test.zip @path"

    for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "Test.zip" %%e-%%f-%%g.zip

    move *.zip "Z:\Archived Interface Logs\Test\Archive\2010"

    goto :EOF

  57. pingram
    October 28th, 2010 at 17:28 | #57

    Oh so close, but can't seem to get it to work exactly, using windows 2003 server, this is my script I am using and the response... I am trying to clean up WINDOWS\Temp folder, it has files back to 10/13, but it returns the response below on some directories in the temp folder but does not delete any of the files?? Can you tell me what I am doing wrong.

    C:\Documents and Settings\Administrator>forfiles.exe /p c:\WINDOWS\Temp /s /m *.
    * /d -5 /c "cmd /c del /q @path"

    Could Not Find c:\WINDOWS\Temp\History\History.IE5\*
    Could Not Find c:\WINDOWS\Temp\History\History.IE5\desktop.ini
    Could Not Find c:\WINDOWS\Temp\Temporary Internet Files\Content.IE5\*
    Could Not Find c:\WINDOWS\Temp\Temporary Internet Files\Content.IE5\desktop.ini

  58. pingram
    October 28th, 2010 at 17:46 | #58

    and what is really weird and scary is that there are no such directories in the C\WINDOWS\Temp directory so, where is it looking that it is returning the above information about not being able to find those directories....
    Could Not Find c:\WINDOWS\Temp\History\History.IE5\*
    Could Not Find c:\WINDOWS\Temp\History\History.IE5\desktop.ini
    Could Not Find c:\WINDOWS\Temp\Temporary Internet Files\Content.IE5\*
    Could Not Find c:\WINDOWS\Temp\Temporary Internet Files\Content.IE5\desktop.ini

  59. pingram
    October 28th, 2010 at 18:25 | #59

    I tried this command on the plain old c:\Temp folder that has bunches of old files in it
    forfiles.exe /p C:\Temp /s /m *.* /d -30 /c "cmd /c del /q @path"
    and it returns

    C:\>forfiles.exe /p C:\Temp /s /m *.* /d -30 /c "cmd /c del /q @path"
    ERROR: No files found with the specified search criteria.

    I don't get it!!

    Any help would be greatly appreciated, I can't seem to find anyone to do this for me and I guess this outside of my expertise.

    thanks
    pingram

  60. James Clements
    October 28th, 2010 at 23:32 | #60

    Hi pingram, OK - working backwards, looking at your last comment first, it seems as though the last thing you tried was to create a folder on your C:\ called Temp and try running fofiles.exe on it. This was your command and output (I've copied and pasted exactly what you used):

    forfiles.exe /p C:\Temp /s /m *.* /d -30 /c "cmd /c del /q @path"
    ERROR: No files found with the specified search criteria.

    So... as per the error, no files were found in the folder that were over 30 (you used -30) days old. Did you definitely put files in there that were older than 30 days? If you didn't then the output you received is the correct one as there would have been nothing for forfiles.exe to delete. I have just copied and pasted your command myself having first created a C:\Temp folder and verified that everything is as it should be. If I place a file (I just grabbed a random one from my hard drive) in C:\Temp that is older than 30 days it does get removed. If there is nothing in C:\Temp older than 30 days then I correctly get "ERROR: No files found with the specified search criteria". So the program is actually behaving as expected isn't it?

    Moving on, with reference to your first comment - you wish to tidy the C:\WINDOWS\Temp folder. OK, a couple of things here, firstly some running programs/applications use this folder to temporarily store log files. So, if you run forfiles.exe to try and clean C:\WINDOWS\Temp and there is a file that is open by the system or another program/application, then there is no way that it can be deleted, even if it does meet your criteria of being older than 5 days. Perhaps an easier way of describing this for you is: have you ever opened a Word document located your desktop then forgotten you still had the Word application open (maybe you minimised it for example) and then tried to delete the actual Word document? You would not have been able to - as the word process has a lock on the file. Well this is effectively what could be happening with C:\WINDOWS\Temp. As an example I have just checked my computer and I can see files in there that I know would not be able to be deleted by forfiles.exe or any other program unless the application or system process that has the files open is closed first.

    The other point I would like to make is that in the C:\WINDOWS\Temp folder you may have some files with special attributes (such as R=read only files) set. It could be worth trying to modify your command to use a few of the other options that 'del' offers. To view a list, in a command prompt type del /? to get them with a description of what each of them does. For example, I would next try to test:

    forfiles.exe /p c:\WINDOWS\Temp /s /m *.* /d -5 /c "cmd /c del /q /f /s @path"

  61. pingram
    October 29th, 2010 at 16:06 | #61

    thanks for the update, i will try your update, but here is what someone found and told me, i cannot run it as *.* it will not delete files, if I run it as *, it does delete files, however it still returns the deal about it cannot find these other directories that are not listed in the temp files, that confuses me, why is it even looking for them. But it does clean up the other files so i am happy, have created a bat file to run automatically, I just tried yours and it did run with the *.*, not sure why yours would and mine would not, hmmm, i looked at the help and could not see what the /f and /s were doing?
    thanks

  62. pingram
    October 29th, 2010 at 16:15 | #62

    oh, and by the way on the c:\temp files i tried earlier they were all really old, over a year old, i had not moved or touched them, the only way i got it to finally work there was this way
    Forfiles /p C:\Temp /s /m * /D -5 /c "cmd /c del /q @path"
    when i had it this way it would not delete them
    Forfiles /p C:\Temp /s /m *.* /D -5 /c "cmd /c del /q @path"

  63. pingram
    October 29th, 2010 at 16:49 | #63

    If I wanted to exclude these files / directories it is searching in how would i do that?

    thanks
    phyl

  64. Eitan
    November 30th, 2010 at 09:52 | #64

    I run the forfiles command to delete old files, but when there are no old files to delete it returns an error saying: ERROR: No files found with the specified search criteria.
    But it's fine by me and I don't want this to be returned as an error. Is there a way to suppress this error?

  65. December 26th, 2010 at 06:45 | #65

    James:

    Are there any way to bypass the confirmation of delete in Windows 7.
    I have some security cameras installed and they are recording to a hard drive, so I delete the videos older than a month, but every time I run this command Windows 7 CMD asks me if I want it to delete the files.
    I tried running it from a bat file but it does not delete any thing.

    Please help me out.

  66. Danny
    July 22nd, 2011 at 08:26 | #66

    If you want to deleted UNC paths, without mapping your self use this script...

    net use k: \\unc\path\here username password
    forfiles.exe /p k:\logs /s /m *.* /d -30 /c "cmd /c del /q @path"
    net use k: /d

    Hope this can help someone... this way you can run the script without mapping a drive everytime.

  67. Thanasis
    April 1st, 2012 at 11:24 | #67

    Dear All, Hello
    I wrote this command on Win 7 and it works. Then i tranfern to XP and doesn't work - giving me error can't execute .

    The whole command is:
    FORFILES -pF:\CAM-RECORD\OFFICE-ARCHIEVE -s -m*.* -d-30 -c “CMD /C del /q @FILE”
    FORFILES -pF:\CAM-RECORD\GARAGE-ARCHIEVE -s -m*.* -d-30 -c “CMD /C del /q @FILE”
    FORFILES -pF:\CAM-RECORD\OUTSIDE-ARCHIEVE -s -m*.* -d-30 -c “CMD /C del /q @FILE”

    I think i used the same forfiles as downloaded and work on win7 i transfern it to win XP.
    I tried many combinations but without result. Maybe i am using the wrong forfiles.exe?
    Do i need to download another forfiles and change the command a little?
    Any comment will be appreciated, thanks in advance.

  68. Tom
    April 11th, 2012 at 20:54 | #68

    @Thanasis

    Looks like you are missing a space between -p and F:\

  69. sri
    April 18th, 2012 at 22:02 | #69

    Tom :
    @Thanasis
    Looks like you are missing a space between -p and F:\

    Hi All,
    I would like to delete some log files older than 90 days in Window 2008 R2 and we need a report for it how can we do ?
    thank you for your time in advance.

  70. sri
    April 18th, 2012 at 22:10 | #70

    i given in this way "forfiles /p c:\test /m *.log /d -730 /c "cmd /c del /q @path" - it worked but while creating this as a file into .exe or .bat so that we can schedule to run every month but its not working.

    any suggestions will be helpful. thank you.

  71. bhanu prakash
    May 31st, 2012 at 11:03 | #71

    i wrote this command like this
    forfiles.exe /p D:\prakash_test /s /m *.* /d -1 /c "cmd /c del /q @path"
    it is deleting only the files in sub directories i want to delete all sub folders also under the prakash_test.
    please help me getting this

  72. Jeyaprabakar
    May 30th, 2013 at 22:17 | #72

    Is there any way to ignore the error "ERROR: No files found with the specified search criteria"
    while running the batch script with the "FORFILES".

    It simply fails when I call the script from other third party tool due to the above mentioned issue..

    forfiles.exe /P "C:\SessLogs" /M *.* /C "cmd /c move @file D:\Sesslog_Archive\Sesslogs " /d -7

  73. KB
    September 24th, 2013 at 02:49 | #73

    Thanks James. works like a charm.

  74. kmor
    December 10th, 2013 at 17:35 | #74

    Hi
    I Have been trying to setup forfiles to remove file a number of days old in the Temporary Internet Files folder in both 2003 and 2008. I started off with the following:

    forfiles /p "%Temporary Internet Files%" /s /m *.* /d -10 /c "cmd /c del /q @file"

    and have also tried for 2008:

    forfiles /p "C:\Users\%%a\AppData\Local\Microsoft\Windows\Temporary Internet Files" /s /m *.* /d -10 /c "cmd /c del /q @path"

    To no avail.

    Has anyone successfully been able to remove these files with forfiles, or any other suggestions would be appreciated.

  75. Jesse
    January 9th, 2014 at 17:02 | #75

    Per M$ the /d command follows the following format:
    MM/DD/YEAR not DD/MM/YEAR

    http://technet.microsoft.com/en-us/library/cc753551.aspx

  76. Asim Khan
    January 9th, 2015 at 12:35 | #76

    very good command with forfiles.exe download links
    Thanks

  77. anamica
    September 15th, 2015 at 07:09 | #77

    @James Clements

    Can someone please tell me will it work on separate drives & as a batch file.. I need to schedule task for batch file to run.. How to make it work?? Please help.. I will appreciate your help.. Any success..

  78. anamica
    September 15th, 2015 at 18:30 | #78

    Can someone please suggest how to use this batch file for multiple drives as a batch file & put an exclusion exception for folder & file name project.

  79. anamica
    September 16th, 2015 at 06:27 | #79

    James,

    Can you please tell me if in below syntax does d stands for last accessed or modified or what?? It will help out a lot.

    forfiles.exe /p (pathtofilestodelete) /s /m *.* /d -30 /c "cmd /c del /q @path"

  80. anamica
    September 16th, 2015 at 06:29 | #80

    James,

    Can you please tell me in below syntax does d stands for last accessed or modified or what?? It will help out a lot.

    forfiles.exe /p (pathtofilestodelete) /s /m *.* /d -30 /c "cmd /c del /q @path"

  81. James Clements
    November 18th, 2015 at 16:04 | #81

    @anamica
    You can view a list of all options for forfiles.exe by running: forfiles.exe /?

    Note that while forfiles.exe is a great tool, it is quite old now and to be honest I usually tend to use PowerShell for tasks like this (which offers many more options including one to exclude certain directories). In answer to your previous question, /d:

    "Selects files with a last modified date greater than or equal to (+), or less than or equal to (-), the specified date using the "dd/MM/yyyy" format; or selects files with a last modified date greater than or equal to (+) the current date plus "dd" days, or less than or equal to (-) the current date minus "dd" days. A valid "dd" number of days can be any number in the range of 0 - 32768. "+" is taken as default sign if not specified."

  82. Katia
    April 22nd, 2016 at 13:01 | #82

    James,

    I used this command to delete directories older than 180 days and 360 days.
    I have about 50 commands in one batch file specifying after how many days should certain files/folders be deleted. For some reason when I ran it, it seems to have deleted the ones I specified /d -360 as /d -180 instead..

    Have you ever come across this?
    I can't for the life of me figure out why it deleted anything older than 6 months rather than a year.

  83. James Clements
    April 25th, 2016 at 12:32 | #83

    @Katia,

    The only thing I can think of is that if you ran the command to remove anything older than 180 days (/d -180), obviously the files that are older than 360 days would also be removed at the same time, as these would also fall into the category of being older than 180 days. Maybe change the order in which you are running the commands, to ensure that files older than 360 days are removed, before running the command to remove files older than 180 days? It also depends on your directory structure, if you have nested directories you may want to specify a deeper path to the files to be deleted to ensure this doesn't happen if you can?

    In all honesty though, since I wrote this post 8 years ago, advances in PowerShell have provided much more functionality to achieve the same results and I actually prefer to use PowerShell for things like this now.

  84. Yovani
    May 6th, 2016 at 19:17 | #84

    @James Clements
    It works!! Thank you so much!!!

  85. Rajendra K Kottakota
    June 7th, 2016 at 16:54 | #85

    This is a great help. I had to put a purge script on aged Windows2000 server OS. Here the script to delete .bak files older than 60 days.

    REM------------------------------------
    REM Script Name: PurgeArchivedDBFiles.bat
    REM Author: Rajendra Kottakota
    REM Creation Date: 06/07/2016
    REM Updated Date:
    REM Script Prupose: This script deletes .bak files older than 60 days from today's date from the folder SRCFLDR
    REM Before deleting thefiles, it prints the names of the files into LOGFILE
    REM------------------------------------

    set MYDATE=%date:~10,4%%date:~4,2%%date:~7,2%
    set MYTIME=%time:~0,2%%time:~3,2%%time:~6,2%
    for /f "tokens=* delims= " %%a in ("%MYTIME%") do set MYTIME=%%a
    set SRCFLDR=D:\PowerOps\PowerBase\Archive
    set LOGFILE=D:\PowerOps\PowerBase\ScheduledPrugeScripts\PurgeArchivedDBFilesLog%MYDATE%%MYTIME%.txt

    Echo Purge started at %date% %time% EST > %LOGFILE%

    Echo Below files are being deleted: >> %LOGFILE%

    FORFILES -p%SRCFLDR% -s -m*.bak -d-60 -c"CMD /C Echo @FILE" >> %LOGFILE%

    FORFILES -p%SRCFLDR% -s -m*.bak -d-60 -c"CMD /C DEL @FILE" >> %LOGFILE%

    Echo ------------------------------------ >> %LOGFILE%

*

code