Using Robocopy to delete old files from folder

31 Jul

Using Robocopy to delete old files from folder

There are numerous occasions where you may want to keep a folder tidy and remove older files.We needed a solution for a shared folder that we wanted to control so that users didn’t use it as a lazy dumping ground for their files.

After a bit of searching I found a novel way of using robocopy to accomplish exactly what we wanted.

mkdir c:\delete
c:\robocopy.exe c:\Source c:\Delete /e /MOVE /MINAGE:14 /LOG+:c:\robocopy.log
rmdir c:\delete /s /q

This script creates a folder called ‘delete’. Then uses robocopy to move files older than 14 days from the ‘source’ folder to the ‘delete’ folder. Lastly it deletes the ‘delete’ folder so that only files newer than 14 days is left in the ‘source’ folder.

Remember to change the location of the robocopy executable, robocopy.log, your source and delete folders.
Add ‘/XF [filename]’ to the robocopy line in order to exclude one or more files from being deleted (if required).

Original article can be found here. (Pure genius!)