Batch clearing of logs – everyday spring cleaning.

The Issue:

Lets just say hypothetically someone likes to log things, they like to log them a lot. Lets also say as the admin of the server they are logging to you don’t like to have your server crash when it runs out of disk space, and the only thing you like less than that is to chase them and make them clean up.

The Solution :

A script scheduled to run everyday.  In this example we will be cleaning out anything in d:\syslog

date /t >>c:\scripts\deletesyslog\%date:~-10,2%%date:~-7,2%%date:~-4,4%.log”
Time /t>>c:\scripts\deletesyslog\%date:~-10,2%%date:~-7,2%%date:~-4,4%.log”
echo Scan Started>>c:\scripts\deletesyslog\%date:~-10,2%%date:~-7,2%%date:~-4,4%.log”

FORFILES -p D:\Syslog\ -s -d -10 -c “CMD /C echo @PATH@FILE>>c:\scripts\deletesyslog\%date:~-10,2%%date:~-7,2%%date:~-4,4%.log”

FORFILES -p D:\Syslog\ -s -d -10 -c “CMD /C del /F /Q @FILE”

echo SCAN Complete>>c:\scripts\deletesyslog\%date:~-10,2%%date:~-7,2%%date:~-4,4%.log”
TIME /t>>c:\scripts\deletesyslog\%date:~-10,2%%date:~-7,2%%date:~-4,4%.log”

 The first 3 lines create a log file in c:\scripts\deletesyslog and log the date and time the script was run, the 4th line logs what is about to be deleted.   The last 2 lines log the time it finished.

Line 5 is the only one that really does anything so If you don’t really care about all this sissy logging that all you need.


-p (path)

-s recurse subdirectories

-d -10 selects files with a modified date of greater than 10 days (alter the 10 if that’s not good for you, remember to alter the matching 10 on the logging line above)

“CMD /C del /F /Q @FILE” is the actual delete command