Copy then delete files older than X days [Updated 8/7/07]

This script simply uses the forfiles command along with xcopy to copy ntbackup files from a source location to a destination location. The script then checks if the file exists in the destionation location, and if it does, deletes the source file. A report is then sent using blat.exe to the specified account in the email variable. The script was written to be VERY verbose for logging purposes, there is only an actual few lines of action taking place. I currently use this to move backups from one location to another to free up space automatically. I am sure it has LOTS more uses.

NOTE: This must not be used on a computer with a 'forfiles' version created before 2000 as the syntax has since changed.
If you only want to delete files, check out deleteold.bat.

All you need to do is change the variables at the top of the batch file. Heres what they should be, in case my names aren't totally self-explanitory.

loglocation - the location you want your logs to be recorded at
olderthan - number of days old the files you want to target have to be
source - the location the files will start at (this field must not end with a \)
destination - the location you want to move the files to before deleting
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)
subject - The subject of the report email that will be sent.

This is a very multi-purpose file and can be used for all kinds of stuff! Enjoy!

copyout.bat


@echo off

set loglocation=C:\logs\Copyout.txt
set olderthan=10
set source=H:\backups
set destination=K:\
set email=blog@integrii.net
set blat=c:\blat.exe
set extension=*
set relayserver=localhost
set subject="Copyout.bat Report"

if exist %loglocation% del %loglocation%

echo ericgreer@gmail.com - copyout.bat - v1.78
echo Copying and deleting files older than %olderthan% days with the file extension *.%extension%.
echo Copying and deleting files older than %olderthan% days with the file extension *.%extension%. >> %loglocation%
echo Copying from %source% to %destination% and deleting from %source%.
echo Copying from %source% to %destination% and deleting from %source%. >> %loglocation%
echo This file may take an extremely long time to run while it looks unresponsive.
echo Check %loglocation% for copy progress.

cd %source%

echo List of files to be copied and removed: >> %loglocation%
echo List of files to be copied and 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 copy...
echo Starting copy... >> %loglocation%
date /t >> %loglocation%
time /t >> %loglocation%

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C XCOPY %source%\@FILE %destination%"

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

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C if exist %destination%\@FILE del %source%\@FILE /Q & echo @FILE Deleted." >> %loglocation%

echo Log file located at %loglocation%

echo Copy and Delete finished... Sending Report.
echo Copy and Delete finished... >> %loglocation%

echo Source Directory Contents: >> %loglocation%
echo. >> %loglocation%
dir %source% >> %loglocation%

echo. >> %loglocation%
echo. >> %loglocation%
echo. >> %loglocation%

echo Destination Directory Contents: >> %loglocation%
echo. >> %loglocation%
dir %destination% >> %loglocation%

date /t >> %loglocation%
time /t >> %loglocation%

set server=%computername%
%blat% %loglocation% -t %email% -s "%server% %subject%" -server %relayserver% -f do-not-reply@%server%.copyout.bat

echo Report sent!