My Dad has an external hard drive he uses to back up his My Documents folder to.  He was interested in an automated way to do this and then also to delete files off the backup drive that have been deleted off the main drive; so sometime last year (I’m guessing based on the the name I gave the main batch file), I created a couple of batch files for him to do that.

The main batch file is dsmbackup2007.cmd, and its helper is the snazzily-named-but-obscurely-written MaybeDelete.cmd.

Dad was having trouble with the backup program — it was giving errors — and it turned out to be that the MaybeDelete.cmd script had gotten misplaced.  I added better error messages to the dsmbackup2007.cmd script for this case and thought I’d post both scripts here (so I don’t lose ’em again):



@echo off

REM You can change this drive letter as needed.
set MAYBEDELETECMD=c:\bin\MaybeDelete.cmd


echo Ready to copy %USER%'s My Documents from %COPYFROM% to %COPYTO% .

REM Copy from the current user's My Documents folder to a folder on the backup drive
REM /S - Copy subdirectories also
REM /E - Copy even empty subdirectories
REM /M - Copy only modified files, and reset the "modified" flag once they're copied
REM /Y - Don't prompt to overwrite files on the backup drive (we want to automatically overwrite them with the newer version).
xcopy "%COPYFROM%*" %COPYTO%  /S /E /M /Y

REM See if there was an error.  If so, display an error message.
REM If not, jump to the end.
if ERRORLEVEL 1 goto Bad

echo Backup appears to have been successful!
goto RemoveStale

echo *****BACKUP FAILED*****

REM Delete files that are now gone from the copy-from folder.
echo Ready to delete files from backup drive that no longer exist on source drive...
if not exist %MAYBEDELETECMD% echo. & echo ERROR: Could not find MaybeDelete.cmd file & echo (expected it to be at %MAYBEDELETECMD%) & echo Please put the MaybeDelete.cmd file in the expected directory. & echo. & goto End
echo (This may take a little while, we're pedaling as fast as we can...)
for /R %COPYTO% %%F in (*) do call %MAYBEDELETECMD% "%%F" "%COPYFROM%"
echo Done.



REM @echo off
set FullPathToFile=%1
set MainDir=%2

REM Strip quotes off the maindir path.
REM for /F %%S in (%2) do set MainDir=%%~fnxS

REM Takes off the first few parts of the directory path that
REM only apply to the backup drive.
for /F "tokens=4* delims=\" %%c in (%FullPathToFile%) do set RelPathToFile=%%d

if not exist %MainDir%"%RelPathToFile%" (echo Deleting %FullPathToFile% & del %FullPathToFile%)

Potential Future Enhancements

More could be done:

  1. It looks like the MaybeDelete.cmd script fails to delete a file if the file to delete has spaces anywhere in its name;
  2. dsmbackup2007.cmd inflexibly expects its companion, MaybeDelete.cmd, to be in the c:\bin directory

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.