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

Please inspire the world with your comments.

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>