Self-Cleaning Robocopy Backup Batch File

Earlier, I posted a simple batch file for sysadmins to keep a number of backups. Here’s another one that’s more sophisticated.

It creates a folder with today’s date and time as a name, copies over the tree of the folder you wish to back up. Then it keeps 50 copies, and deletes the oldest.

It assumes a folder on a backup drive named robocopy. It also assumes a folder on C called shared. Easy to change. Enjoy.

cd robocopy\

set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%
set CUR_HH=%time:~0,2%
if %CUR_HH% lss 10 (set CUR_HH=0%time:~1,1%)

set CUR_NN=%time:~3,2%
set CUR_SS=%time:~6,2%
set CUR_MS=%time:~9,2%

robocopy c:\shared d:\robocopy\%SUBFILENAME%\ /s /r:5 /w:5

for /f "skip=50 tokens=*" %%A in ('dir d:\robocopy /ad /b /o-d') do rmdir /s /q "d:\robocopy\%%A"
echo "Done"

Leave a Reply

Your email address will not be published. Required fields are marked *