Wednesday, 6 April 2011

Forfiles or the command you always wanted

Forfiles allows you to search for files
over a certain date, of a certain type or configure more detailed crieria.

To find files in C:\SQLBackups that start with the name SQL1 and have the extension of .bak and are over 7 days old then delete the file the command would be:
This command has replaced many vbscripts and batch files I've built up over the years.
forfiles /p C:\SQLBackups /m SQL1*.bak /d -7 /c "cmd /c del @file"

To find directories in C:\Data\Pictures that start with the name Holiday and were create after 06/04/2011 then delete the directory and its contents quietly the command would be:
forfiles /p C:\Data\Pictures /m Holiday* /d 06/04/2011 /c "cmd /c rmdir /s /q @file"

No comments:

Post a Comment

Correction, question or suggestion, it's all welcome here.