Using Robocopy to keep folders clean
SQL server has always had difficulty with its backup and maintenance plans in removing old backups. Until now this has been a major issue on some of our servers, each month someone has to go through the backup folder and remove the old Transaction Log and Full Backup folders. No longer – Robocopy to the rescue! Its simple but oh so efficient. (I know there are many different ways to do this, Perl/ANT etc however this is what I have available on the server)
By simply copying the files over to a temp folder, robocopy has some switches that allow us to tell it to MOVE the files that have a timestamp older than a certain number of days, once its done its work, we simply delete the temp folder and all those old files are gone. One added bonus here is the logging, we can tell what was moved, and therefore deleted. This one uses the Standard SQL install path, careful of those funky ~1 DOS path names though, it took me a few minutes to get those right. All the command should be on one line too. Save it as cleanup.bat or something similar, double click and you are away!