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?
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.
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?
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?
@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
@manus
This should do it:
forfiles /p "(where are you looking?)" /d -730 /s /c "cmd /c echo @file" >> 2_year_old_files.txt
**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
@cici
Normally this /q error is because the path you have specified is invalid in some way
Normally this /q error is because the path you have specified is invalid in some way
@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
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
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
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
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"
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
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"
If I wanted to exclude these files / directories it is searching in how would i do that?
thanks
phyl
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?
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.
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.