Reference a Volume/Drive by Label
Asked Answered
D

3

8

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

Dirge answered 30/1, 2012 at 14:22 Comment(6)
Looks to be a duplicate of thisPalaestra
3 answers I didn't understand, though. Plus the one that was marked as the answer was actually unresolved.Dirge
How was it unresolved? the question below it is actually answered in the post.Palaestra
Because the asker asked a fairly basic question about it and never got answered?Dirge
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.Palaestra
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.Dirge
S
15

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.

Subcortex answered 30/1, 2012 at 15:35 Comment(9)
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" :(Dirge
@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.Subcortex
Got it working with 'for /f %D in ('wmic volume get DriveLetter^, Label ^| find "yourLabel"')' and then 'xcopy "%SystemDrive%\sourcePath" "%usb%\targetPath"'? Thank you! :)Dirge
'wmic' is not recognized as an internal or external command, operable program or batch file.Yakut
@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.Subcortex
I have Windows 7x64.Yakut
@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.Subcortex
@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.Subcortex
For case-insensitive find use /i.Flannelette
H
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"
Healthful answered 4/4, 2013 at 17:15 Comment(0)
V
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.)

Volatile answered 7/4, 2016 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.