Robocopy backup to dynamically-lettered removable drive

I use a Seagate FreeAgent GoFlex USB drive to carry large install files to customer sites. I wanted an easy, portable way to keep my Flex drive in sync with the working files on my laptop’s D: drive. I use Robocopy for big backup jobs on the server application products we support so I put it to use here, too, and it worked fine. The only annoying part was editing the Robocopy statement whenever the drive letter of the Flex changed.

To get around this I named the Flex volume “flex” (original) and added a statement to find the driver letter on-the-fly.

This should work for any removable drive for which you know the volume name. Just change the text of the find "flex" statement in line 10.

  • You’ll need a copy of Robocopy. I grabbed it from the Windows Server 2003 Resource Kit and it runs fine on the XP & Win7 workstations I’m using. I think the new version might be delivered standard with Server 2008.
  • This example mirrors the entire D: drive, excluding two directories I don’t care about, to the Flex drive. If you haven’t used Robocopy, do some reading before using /MIR. It will delete anything on the destination that doesn’t exist on the source.


@echo off

echo. > "%cd%\backup_flex.log"
echo %time% *** backup starting *** >> "%cd%\backup_flex.log"
echo. >> "%cd%\backup_flex.log"

rem Find the drive letter corresponding to the removable drive named "flex"
setLocal Enabledelayedexpansion
for %%i in (d e f g h i j k l m n o p q r s t u v w x y z) do (
	for /f "tokens=1,* delims=:" %%x in ('fsutil fsinfo volumeinfo %%i: ^|find /i "flex"') do set drive=%%i

REM Mirror the D: drive to the Flex drive, excluding some directories.
Robocopy D:\ %drive%:\ /MIR /XJ /R:0 /W:1 /NP /NDL /xd "D:\Tools\Outlook offline files" "D:\Tools\Windows Search Index" /LOG+:"%cd%\backup_flex.log" 2>>errors.txt

echo. >> "%cd%\backup_flex.log"
echo %time% *** flex backup done *** >> "%cd%\backup_flex.log"
echo. >> "%cd%\backup_flex.log"

p.s. If anyone comes up with a cool way to limit the drive letters the FOR statement needs to iterate through, I’d be happy to hear about it. I spent more than a few minutes trying to work the output from fsutil fsinfo drives into the loop but I’m missing something.

for /f "tokens=1,*" %%i in ('fsutil fsinfo drives') do (
  rem iterate through the ...volumeinfo command with %%j
Tagged , , , . Bookmark the permalink.

9 Responses to Robocopy backup to dynamically-lettered removable drive

Think something? Say something.