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 Servernamec$Program Filesapplogs
forfiles -d -8 -c “cmd /c echo Deleted: @file @fdate >> %apath% & del @file”
echo. >> %apath%
echo. Finished @ %time% >> %apath%

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 :)

Related Posts

Enabling RDP on a server remotely. There's very little more annoying in a windows environment than having to go to a console of a server because some idiot has disabled remote administr...
Remove shell extensions after installing Adobe Acr... A pet peeve of mine are context menu extensions... Why software vendors decide to include non optional context menu addons are besides me! A good exam...
Scripting Web interface 5.0 installation. To auto install web interface sites, sitemgr.exe must be used. the below script will create a default website for XenApp services and will also create...

Leave a Reply