Friday, August 5, 2011

Forfiles to manipulate files based on date

Nothing new here...
You can use Forfiles to manipulate files based on their date.

/P Path to search
/M SearchMask
/S Subdirectories
/C Command
/D Date (valid as "mm/dd/yyyy" or in greater than + or less than -)

Examples:
Search current and subdirectories for files older than 30 days from current date and echo them to screen
forfiles /s /d -30 /c "cmd /c Echo @file"

Same but only display iso's
forfiles /s /m *.iso /d -30 /c "cmd /c Echo @file"

Same but this time display the full path to the file
forfiles /s /m *.iso /d -30 /c "cmd /c Echo @path\@file"


So now we have the basic concept you could use this to script to delete files older than x days/date.
forfiles /p C:\backups /s /d -30 /c "cmd /c del @path"
or move them to cheaper storage
forfiles /p C:\backups /s /d -30 /c "cmd /c move /y @path C:\Destination"

No comments:

Post a Comment