Delete files older than certain number of days
July 31st 2008 @ 1:16 pm Windows Server 2003

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

James Clements
rss 40 comments
  1. Chris
    October 8th, 2008 | 2:09 pm | #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 | 12:19 pm | #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 | 2:32 pm | #3

    Thank you, awesome.

  4. Antony
    December 15th, 2008 | 8:12 pm | #4

    Thanks, perfect - just what I needed.

  5. Rick
    January 12th, 2009 | 8:37 pm | #5

    Just what I was looking for, thanks a lot!

  6. Andreu
    January 15th, 2009 | 9:42 am | #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 | 9:44 am | #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 | 11:16 pm | #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 | 2:52 pm | #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 | 6:24 pm | #10

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

  11. James Clements
    February 23rd, 2009 | 10:30 am | #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 | 8:22 am | #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 | 3:38 pm | #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 | 9:16 am | #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 | 9:42 am | #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 | 2:06 pm | #16

    thanks a lot ;)

  17. PS
    April 10th, 2009 | 11:02 am | #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 | 2:16 pm | #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 | 11:28 am | #19

    Thanks it worked

  20. John
    April 28th, 2009 | 5:28 pm | #20

    works great, thanks

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

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

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

    Thanks a lot!!!

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

    Cheers! Worked first time!

  24. unixGuy
    June 15th, 2009 | 5:16 pm | #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 | 7:59 pm | #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 | 9:22 am | #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 | 11:02 am | #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 | 2:24 pm | #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 | 4:29 pm | #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 | 9:18 am | #30

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

  31. john
    September 24th, 2009 | 3:40 pm | #31

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

  32. James Clements
    September 25th, 2009 | 2:38 pm | #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 | 8:54 pm | #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 | 12:58 pm | #34

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

  35. Richard
    December 18th, 2009 | 5:22 pm | #35

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

  36. Richard
    December 21st, 2009 | 4:39 pm | #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 | 9:24 am | #37

    nice one m8!

    saved me alot of time

    thx

  38. Praveen
    January 11th, 2010 | 12:27 pm | #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 | 7:01 pm | #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 | 6:06 pm | #40

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

comment on this article