FORFILES to find and delete files older than n days

Advertisements
Advertisements

Here is a quick tip for finding and deleting files that are older than some number of days.  I had been using a .bat file to purge a directory of log files that tended to fill up a drive quickly on a weekly basis when I ran accross the handy FORFILES command.  Using this command I can choose to only delete files older than n days (in this example, 10).

Here is the code:

FORFILES -p “C:\logfiles” -s -m *.* -d -10 /C “cmd /c del @PATH”

A good idea is to use an echo command to debug your .bat file first to make sure you are picking up the right set of files like this:

FORFILES -p “C:\logfiles” -s -m *.* -d -10 /C “cmd /c echo @PATH”

Obviously deleting files is not the only useful thing you can accomplish with FORFILES, so it’s up to your imagination on what you can do with the /C parameter.  Check out FORFILES /? for a listing of all the params available for the command.

The command was installed on the Server 2008 machine I was using, but not on the XP install I had on my laptop, but you can grab the .exe here ftp://ftp.microsoft.com/ResKit/y2kfix/x86/ and throw it in your system32 dir for use anywhere.

Advertisements

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.