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
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.
For my needs, I use the following in a batch file that looks for the Drive labeled "System" (This is where my Windows 7 OS is installed) and it puts the Drive Letter associated with the label "System" into a variable named %SystemVolume_DriveLetter%
for /f "delims=" %%l in ('WMIC Path Win32_volume where "Label='System'" Get DriveLetter /format:list') do >nul 2>&1 set "SystemVolume_%%l"
This works in Windows XP:
for /f %%D in ('wmic LogicalDisk get Caption^, VolumeName ^| find "DRIVE_LABEL"') do set DRIVE=%%D
(Use %D
instead of %%D
if run directly from command line.)