Delete files older than X days

This batch is a direct modification to the 'copyout.bat' file I posted here. This file scans a location for files older than a number of specified days and deletes them. The file is also setup to use blat.exe (download from sourceforge) to send email reports.

NOTE: This must not be used on a computer with a 'forfiles' version created before 2000 as the syntax has since changed.

loglocation - the location you want your log to be recorded at
olderthan - number of days old the files you want to delete have to be
source - the location the files will start at (dont end this field in a \)
email - the address that will be notified when the operation sends an email report
blat - the path to the blat.exe file (this, and all the others, can be a UNC path). Must be downloaded from souce forge most of the time.
extension - The file extension(s) to copy then delete, just use * for all files.
relayserver - Target mail server with relay enabled. (required)

Use at your own risk, no warranties and stuff.


@echo off

set loglocation=C:\Deleteold.txt
set olderthan=31
set source=F:
set blat=c:\blat.exe
set extension=bkf
set relayserver=localhost

color 1f
if exist %loglocation% del %loglocation%

echo - deleteold.bat - v1.3
echo Deleting files older than %olderthan% days with the file extension *.%extension% located at %source%.
echo Deleting files older than %olderthan% days with the file extension *.%extension% located at %source%. >> %loglocation%

cd %source%

echo List of files to be removed: >> %loglocation%
echo List of files to be removed:

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C Echo "@FILE"
FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C Echo "@FILE" >> %loglocation%

echo Starting delete...
echo Starting delete... >> %loglocation%
date /t >> %loglocation%
time /t >> %loglocation%

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C del %source%\@FILE /q & echo %source%\@FILE deleted." >> %loglocation%

echo Log file located at %loglocation%

echo Delete finished... Sending Report.
echo Delete finished... >> %loglocation%
date /t >> %loglocation%
time /t >> %loglocation%

set server=%computername%
%blat% %loglocation% -t %email% -s "%server% Deletion Log" -server %relayserver% -f do-not-reply@%server%.delete.log

echo Report sent!