6

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. :(

  • 1
    Looks to be a duplicate of [this](http://stackoverflow.com/questions/47849/refer-to-select-a-drive-based-only-on-its-label-i-e-not-the-drive-letter) – MaskedPlant Jan 30 '12 at 14:48
  • 3 answers I didn't understand, though. Plus the one that was marked as the answer was actually unresolved. –  Jan 30 '12 at 14:52
  • How was it unresolved? the question below it is actually answered in the post. – MaskedPlant Jan 30 '12 at 15:00
  • Because the asker asked a fairly basic question about it and never got answered? –  Jan 30 '12 at 20:45
  • That doesn't make the question unresolved. The question was accepted and it works. At the end of the answer he gave the command to use in a batch script to call the vbs file that he posted. Also dbenham has the same answer posted in both locations. both dbenham and the answer in the other post should work for you. – MaskedPlant Jan 30 '12 at 23:13
  • I've never had success with batch files and vbs scripts. Plus I only want one file. dbenham gets double points, anyway. So, I'm sure he doesn't mind. –  Jan 31 '12 at 10:04

3 Answers3

14

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.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • I keep getting "%%D was unexpected at this time" when I run that command. Or if I run it in a batch file (like it's going to be used), and try and reference %%D, by cd %%D for example, it just says "The system cannot find the path specified" :( –  Jan 31 '12 at 10:03
  • @BenHooper - Use %D (single percent) if run from a command line. Make sure you use quotes - "%usb%" or "%%D" (or "%D") in your XCOPY statement. Otherwise it will fail if there are spaces in the path. – dbenham Jan 31 '12 at 12:24
  • Got it working with 'for /f %D in ('wmic volume get DriveLetter^, Label ^| find "yourLabel"')' and then 'xcopy "%SystemDrive%\sourcePath" "%usb%\targetPath"'? Thank you! :) –  Jan 31 '12 at 12:54
  • `'wmic' is not recognized as an internal or external command, operable program or batch file.` – Tomáš Zato Feb 14 '17 at 13:05
  • @TomášZato - Windows XP Home edition does not have WMIC. All other versions of XP, plus every version of Windows since, does have WMIC. A down vote for providing an answer that works *except* for on a discontinued OS that is no longer supported by the manufacturer is kind of crazy. – dbenham Feb 14 '17 at 15:00
  • I have Windows 7x64. – Tomáš Zato Feb 14 '17 at 15:26
  • @TomášZato - Then there is something wrong with your installation. WMIC is a standard utility within Win-7. Perhaps your PATH variable is corrupted, but then other standard commands would also fail. – dbenham Feb 14 '17 at 15:56
  • @TomášZato - Another possibility is you could be running a batch script that overwrites the PATH variable. Batch scripts should not overwrite standard environment variables with their own values. – dbenham Feb 14 '17 at 16:17
  • For case-insensitive `find` use `/i`. – spcsLrg Oct 26 '21 at 13:49
1

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"
Gordon
  • 312,688
  • 75
  • 539
  • 559
1

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.)

tav
  • 587
  • 6
  • 9