Windows – How to delete files older than 30 days with robocopy

backuprobocopysynchronizationwindowswindows-server-2008

I have two folders synchronised with robocopy (src and mirror) and I try to not removing files in the mirror before 30 days. I tried with /MINAGE option to say "consider every files older than 30 days" so that it only deletes 30-day old files in the mirror but it seems that it is only acting on available files (not deleted).

robocopy /MIR /MINAGE:30 /SD:src /DD:mirror

So basically, if I create a new file, it will be correctly synced in the mirror (by another robocopy task that doesn't delete files at all); and if the day after I delete it, the file will be deleted in the mirror too when running the previous command. To summarize, I want to delete files deleted in the src folder and older than 30 days in the mirror.

Am I missing something ?

Thank you.

Best Answer

I don't know how to do it with robocopy, but when I wanted to do the same as what you're doing, I put this line in my batch file:

forfiles -p %_BACKUPPATH% -m *.rar -d -3 -c "cmd /c del @path"

A dissection is:

  • forfiles - The program for filtering the files
  • -p %_BACKUPPATH% - -p indicates "path" (mine is %_BACKUPPATH% but you can replace this with a "real" path), otherwise it uses the current directory
  • -m *.rar - filter by filetype .rar, skip this parameter for *.*
  • -d -3 - Only affect files older than 3 days (-3)
  • -c "cmd /c del @path" - anything after the -c is what will be executed for each file it finds, in this case, del.