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.
backup_flex.bat
@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 )
Hi, about Robocopy backup to dynamically-lettered removable drive: can you make a batch file witch robocopy only file.doc and file.txt from c:\useprofile to the dynamically lettered removable drive called “Cruzer”? Can you send me the batch file?
Try this:
sorry for my late comment…i tried your script, it runs very well on win xp but it doesn’t work on win7 …it looks for the device but it doesn’t find the device Cruzer so it makes the copy into another letter, from where it starts the script…..can you help me? thanks for all
and it doesn’t make the copy because it appears the message:” error 5
Hi Glen,
i maked this script running on Windows7 in C:\ volume and i inserted a pendrive called ROBY collocated in F:\ volume
this in the batch script:
rem Find the drive letter corresponding to the removable drive named “ROBY”
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 “ROBY”‘) do set drive=%%i
)
REM Copy files to the ROBY drive
Robocopy %USERPROFILE% %drive%:\ presentazione.doc /E /S /R:0 /W:0
And this the response:
C:\Users\Gerardo\Desktop>rem Find the drive letter corresponding to the removabl
e drive named “ROBY”
C:\Users\Gerardo\Desktop>setLocal Enabledelayedexpansion
C:\Users\Gerardo\Desktop>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 ”
ROBY”‘) do set drive=%i )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo D: |find “ROBY”‘) do set drive=D )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo E: |find “ROBY”‘) do set drive=E )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo F: |find “ROBY”‘) do set drive=F )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo G: |find “ROBY”‘) do set drive=G )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo H: |find “ROBY”‘) do set drive=H )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo I: |find “ROBY”‘) do set drive=I )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo J: |find “ROBY”‘) do set drive=J )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo K: |find “ROBY”‘) do set drive=K )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo L: |find “ROBY”‘) do set drive=L )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo M: |find “ROBY”‘) do set drive=M )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo N: |find “ROBY”‘) do set drive=N )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo O: |find “ROBY”‘) do set drive=O )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo P: |find “ROBY”‘) do set drive=P )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo Q: |find “ROBY”‘) do set drive=Q )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo R: |find “ROBY”‘) do set drive=R )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo S: |find “ROBY”‘) do set drive=S )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo T: |find “ROBY”‘) do set drive=T )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo U: |find “ROBY”‘) do set drive=U )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo V: |find “ROBY”‘) do set drive=V )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo W: |find “ROBY”‘) do set drive=W )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo X: |find “ROBY”‘) do set drive=X )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo Y: |find “ROBY”‘) do set drive=Y )
C:\Users\Gerardo\Desktop>(for /F “tokens=1,* delims=:” %x in (‘fsutil fsinfo vol
umeinfo Z: |find “ROBY”‘) do set drive=Z )
C:\Users\Gerardo\Desktop>REM Copy files to the ROBY drive
C:\Users\Gerardo\Desktop>Robocopy C:\Users\Gerardo :\ presentazione.doc /E /S /R
:0 /W:0
——————————————————————————-
ROBOCOPY :: Copia di file efficace per Windows
——————————————————————————-
Avviato: Sat Jun 09 13:08:56 2012
Origine : C:\Users\Gerardo\
Destinazione : C:\Users\Gerardo\Desktop\:\
File: presentazione.doc
Opzioni: /S /COPY:DAT /R:0 /W:0
——————————————————————————-
2012/06/09 13:08:56 ERRORE 123 (0x0000007B) Accesso alla directory di destinazio
ne in corso C:\Users\Gerardo\Desktop\:\
La sintassi del nome del file, della directory o del vo^CTerminare il processo b
atch (S/N)?
First, let’s make sure the volume is named what we expect. Run this command:
fsutil fsinfo volumeinfo f:
Second, we can make the find statement a little more robust by making it not case sensitive with the /i switch.
find /i "flex"
(I updated my original post with this.)
On my Win7 machine, when I run this:
fsutil fsinfo volumeinfo d:
I get this:
So my
find
statement would look like this.find /i "data"
Glen, you are great!…ok, i made this command on prompt: “fsutil fsinfo volumeinfo f:” and this is the answer :
Microsoft Windows [Versione 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. Tutti i diritti riservati.
C:\Windows\system32>fsutil fsinfo volumeinfo f:
Nome volume: ROBY
Numero di serie volume: 0xb69ca32a
Lunghezza massima componente: 255
Nome file system: FAT32
Mantiene la distinzione tra maiuscole e minuscole nei nomi di file
Supporta il formato Unicode nei nomi di file
for runnig this command it requires to be runned as an administrator…so i understood that the same problems stopped the script: if i run it as administrator it works fine!
So, my trouble now is to make it executable as an administrator: is there the possibility to put a line command into the batch script? i dont want to be obliged to make the same procedure every time….
Good catch. I have UAC turned off on my machines so I didn’t get caught by that. I don’t know how to flip ‘run as admin’ programmatically but it must be possible. Depending on how you’re launching that .bat, you may be able to set it to ‘run as admin’ in the shortcut. Try this one: http://goo.gl/QWqdI
Good luck!
This is 4 years too late, but you can elevate priviledges in prompt by using ‘runas’.