Friday, March 18, 2011

Remove files older than X number of days

So another problem I needed a solution to today, a device drops log files to a file store, unfortunately it's not able to manage these logs once they're in that location. They accumulate until there is no more space and then once it runs out, it errors and is no longer able to dump files. So out for a quick solution I go and here it is.

I created a batch file that just had the following command in it

forfiles -s -m *.log -d -30 -c "cmd /c del @path"  

Now let me explain a little further. forfiles is an exe located on many versions of windows in the system32 directory by default, however, if it's not there you can download it and add it there from here: ftp://ftp.microsoft.com/ResKit/y2kfix/x86/

The *.log specifies to delete all log files from the folder, you could change this to be all files or whatever criteria  you desire.

-30 says to remove anything older than 30 days

the section after -c is the command to run against the files

If the batch file is NOT run from the same directory as the files you are wanting to run this against you need the command to look like this:

forfiles -p "C:\delete\these" -s -m *.log -d -30 -c "cmd /c del @path"  

Now just create a scheduled task to run this bat file on a routine basis and where good to go.

No comments:

Post a Comment