"Delete Files Older Than" Batch Script
If powershell is acceptable (should be, as its enabled by default on Server 2008+) try this:
$numberOfDays = 3
$Now = Get-Date
$TargetFolder = “C:\myoldfiles”
$LastWrite = $Now.AddDays(-$numberOfDays)
$Files = get-childitem $TargetFolder -include *.bak, *.x86 -recurse | Where {$_.LastWriteTime -le “$LastWrite”}
foreach ($File in $Files)
{
write-host “Deleting File $File” -foregroundcolor “Red”;
Remove-Item $File | out-null
}
Souce here.
forfiles -p c:\pathtofiles\ -m *.rar -d -5 -c "cmd /c del @path"
Where -5
is the age of the files you want to delete (5 days or older in this case). This script is deleting .rar
files - drop the -m *.rar
if you want to delete any file type.
If you insist on using batch files, Robocopy.exe is your answer. Its fast (multithreaded) and very robust. For your scenario you can use the following as a guide :
:: Moves dir & files older than 3 days to i:\Destination
:: Wildcards acceptable
robocopy i:\Source\ i:\Destination\ /MOVE /MIR /MINAGE:3 /ETA
:: Removes the destination tree
rd /s /q i:\destination
There is a long list of options, please do robocopy /? to see them all. You can even use it to do incremental backups, scheduling, creating backup profiles, etc.