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








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
This is just what I was looking for. Thanks for all the info in a easy to digest format.
Thank you, awesome.
Thanks, perfect - just what I needed.
Just what I was looking for, thanks a lot!
Hi, great script. But if the file has the A attribute forfile.exe doesn't delete it. Any tip?
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"
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”
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.
This awesome, can you explain the @Path???? Is that to hide the path on the screen?
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.
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?
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?
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?
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
thanks a lot
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.
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.
Thanks it worked
works great, thanks
Thanks so much James. Wasn't aware of the forfile.exe utility - what a handy little thing!
Thanks a lot!!!
Cheers! Worked first time!
"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...
unixGuy, I understand your frustration! Of course the simple workaround here is to map a drive on your PC. You probably knew that already
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
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"
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"
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"
Ok. Forfiles work fine. I want logfile in another folder how many files was deleted.
can you use this on unc paths or mapped network drives?
John,
Only on a mapped drive I'm afraid. But then it only takes a second to map a drive to a UNC
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
Gr8.....This is good...Thnx!!
Is there a way to bypass the recycle bin when deleting files using Forfiles?
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.
nice one m8!
saved me alot of time
thx
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
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
AWESOME !!! Just what I was looking for !!!!
@Praveen
Same here on the win2000 server....nothing gets deleted.
Then again...it IS windows...
Thanks!
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.
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!
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
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"
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
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?