Batch file to delete files older than N days


forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del @path"

See 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:\what\ever" /s /m *.* /D -<number of days> /C "cmd /c del @path"

Leave a Comment