ForFiles - Delete files older than X amount of Days
FORFILES /P C:\MyPath\ /S /M log*.xml /D -30 /C “cmd /c del @file”
/S - look at subdirectories
log*.xml - that is the file pattern, you could have it be like *.txt for example
/D -30 = files greater than 30 days
/C - thats the command you want to run
this is pretty much a one line command that could replace a ton of vbScripts, batch files, and other apps I have seen over the years to clean up files. Something for every sys admin’s toolbelt.
Simlar Posts

October 25th, 2007 at 4:25 am
Thanks for the info!
May 15th, 2008 at 12:34 pm
I am using the forfiles.exe version:5.2.3790.0 (srv03_rtm.030324-2048) in windowsXP.
I used your(ForFiles - Delete files older than X amount of Days Feb.27, 2007 in Geeky/Programming)above command and found it didn’t work in my computer but when I tested the following command in batch file
————————————————-
@ECHO OFF
ECHO ******www.moshiur.com***********
forfiles /p C:\test22\test2\ /s /m *.* /d -3 /c “cmd /c del echo @FILE”
EXIT
————————————————-
I found that it was working fine including files in sub directories in my given path but thing is that it couldn’t delete any subdirectories. Do you have advice ?
May 15th, 2008 at 5:33 pm
mine in the post works, it is just that wordpress screws up the “quotes” so when you copy/paste it , it doesnt work.
if you want to remove directories, try the cmd line “rd”
such as rd /s /q myfolder