Home > Batch Scripting, Scripting > Deleting Files older than X, Forfiles.exe.

Deleting Files older than X, Forfiles.exe.

As part of my recent Citrix cleanup i found the need to clear down an applications log files folder, this folder existed in the program files directory and was eating up gb’s of space with useless logs. The only difference between these files and standard logs was that sometimes recovery was neccessary so it was agreed that a 7 day retention would be kept.

With the 7 day retention it made it slightly more complicated to batch, but with a little searching and trial/error i present to you forfiles. As far as I’m aware this program comes with server 2003 tools, but could be wrong. a bit of googling will find you a copy.

Forfiles allows you to execute individual commands for files that meet certain criteria, in my case it was the Date index. Not only this but it allows for & commands… excellent little batch tool!

forfiles -d -8 -c “cmd /c echo Deleted: @file @fdate >> %apath% & del @file”

Once i had decided it was forfiles i was going to use, i had a few options. but seeing as this tool was not on all servers, and i wanted to log it centrally with the least ammount of logging, i chose to run it from central.

Below is a step by step of what i did.

@echo off
set apath=c:\avyaclean.log
echo. >> %apath%
echo.Time Starting: %time% >> %apath%
Pushd \\Servername\c$\Program Files\app\logs
forfiles -d -8 -c “cmd /c echo Deleted: @file @fdate >> %apath% & del @file”
echo. >> %apath%
echo. Finished @ %time% >> %apath%
popd
EXIT

The meat of this statement is here: forfiles -d -8 -c “cmd /c echo Deleted: @file @fdate >> %apath% & del @file”. this basically says, any files older than 8 days (modified, not created date) run a command prompt that echos the file name and modify date to a text file specified & delete the file.

You can do this on as many servers as you like, just change the servername and copy and paste 🙂

Categories: Batch Scripting, Scripting Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment