Reference a Volume/Drive by Label

user1097771 picture user1097771 · Jan 30, 2012 · Viewed 17.9k times · Source

I'm trying to write a batch file to xcopy a folder to a removable USB drive. The problem that I face, however, is that drive letters are subject to change, so I would like to be able to do this by referencing the volume label instead of the drive letter.

Any ideas? An hour of Google-ing has proved fruitless. :(

Answer

dbenham picture dbenham · Jan 30, 2012

This command should discover the drive with the correct label and store the drive letter (with colon) in variable "usb"

for /f %%D in ('wmic volume get DriveLetter^, Label ^| find "yourLabel"') do set usb=%%D

You could embed your xcopy command(s) directly in the DO clause if you like. %%D contains the drive letter.