Posted By: Kibbee
I am looking for a way to delete all files older than 7 days in a batch file. I’ve searched around the web, and found some examples with hundreds of lines of code, and others that required installing extra command line utilities to accomplish the task.
Similar things can be done in BASH in just a couple lines of code. It seems that something at least remotely easy could be done for batch files in Windows. I’m looking for a solution that works in a standard Windows command prompt, without any extra utilities. Please no PowerShell or Cygwin either.
forfiles -p "C:whatever" -s -m *.* -d <number of days> -c "cmd /c del @path"
forfiles documentation for more details.
For more goodies, refer to An A-Z Index of the Windows XP command line.
If you don’t have
forfiles installed on your machine, copy it from any Windows Server 2003 to your Windows XP machine at
%WinDir%system32. This is possible since the EXE is fully compatible between Windows Server 2003 and Windows XP.
Later versions of Windows and Windows Server have it installed by default.
For Windows 7 and newer (including Windows 10):
The syntax has changed a little. Therefore the updated command is:
forfiles /p "C:whatever" /s /m *.* /D -<number of days> /C "cmd /c del @path"