Thursday, March 15, 2012

Batch delete files older then X days using forfiles : updated!


Purpose:
This batch example will search for files with the extension .txt and deletes file less then 5 days old.

Required:
----------------------------------------------------
Forfiles.exe
/p  The path to search
/s Recurse into sub-folders
/M Mask
/C command The command to execute for each file

/D - dd      Select files with a last modified date less than or
                equal to the current date minus "dd" days. (in the past)
-----------------------------------------------------
Del.exe
-----------------------------------------------------

Code:

Echo on
Forfiles /P c:\temp\ /S /M *.txt /C "cmd /c del @path" /D -5
If you have a space in the path the only way i found forfiles.exe to work is the following example

if a file is older then 30 days in e:\example space then delete
forfiles -p "e:\example space" -m *.* -s -d -30 -c "cmd /C del @FILE"