Archive for April, 2010

Using current drive letter in batch script on a usb drive which changes drives letters

Following on from lasts week tip about Robocopy, if you are backing up to a USB drive there is a chance that every time you use the drive (maybe on different machines) under Windows you get a different drive letter.

So if you have a batch file saved on the drive with your finely crafted backup script, it may fail if your drive D:\ is now drive E:\

Enter to the rescue just the variable you need:

%~d0

Which gives you the path to the file in which the script is being run from,

so something like

robocopy \\my-server\files %~d0\backupfolder /mir

saved as ‘backup.bat’ in the root of your usb drive will work everytime.

Bookmark and Share

Robocopy your backup friend

My new favourite tool for doing backups is Robocopy. A little microsoft utility, previously in the Microsoft Resource Kits, but now “standard issue” in Vista and Windows 7.

Even for my mainly XP based work, Robocopy is a great advance over using the xcopy command with /d flag – which only copied newer files and could easily fall over with files in use.

The basic syntax I use is as follows

ROBOCOPY “Source Folder” “Destination Folder” /MIR /R:5 /W:5 /FFT

The flags I’m using:

/MIR – mirror the source to directory – will delete files in the destination that are no longer in the source (the main benefit over xcopy)

/R:5 – retry 5 times on error, the default is 1 million, which effectively halts your command unless you are very patient!

/W:5 -wait 5 seconds on error – default if 30 seconds, I find a shorter time better

/FFT – uses Fat File Times – only compares file time to accuracy of 2seconds as used on FAT file system, needed if you’re copying to an external drive formatted as such. Also found works more accurately if copying from / to a Samba/Linux server with this flag.

Bookmark and Share