We have a lot of servers that store files that are not needed after some time (for example, Logfiles). Usually these files are sometimes needed for some days but not after that.
Therefore I needed a fast and quick way to delete old files. I found some programs that are able to do so, but I wanted a really simple solution.
After some testing, I decided to “misuse” Robocopy for that. Actually, Robocopy has no option to delete old files but it has the strange option “/CREATE”. This option will copy all files but with 0 bytes, so the copy is very fast.
The Batchfile that does the real work (DeleteOldFiles_Exec.bat) looks like this:
IF NOT EXIST C:\TEMP\. md C:\TEMP
IF NOT EXIST C:\TEMP\TRASH\. md C:\TEMP\trash
%~dp0robocopy %SRC% "C:\TEMP\TRASH" %PATRN% /R:1 /s /mov /create /XA:A /minage:%days%
Rem /L for List only
rd C:\TEMP\TRASH /s /q
It will move the given files (%PATRN%) from the source folder (%SRC%) that are older than the given days (%DAYS%). The options for Robocopy are pretty easy except the /CREATE option (see above) and the /XA:A option which excludes all files with the ARCHIVE attribute set. This option is set since I do not want to delete any file that hasn't been backed up.
I then call this batch like this:
CALL deleteoldfiles_exec "C:\Temp" *.* 10
This will delete all files (*.*) in the directory C:\Temp that are older than 10 days.