Reference a Volume/Drive by Label

走远了吗. 提交于 2019-11-28 01:38:08

问题


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:


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.




回答2:


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"



回答3:


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



来源:https://stackoverflow.com/questions/9065280/reference-a-volume-drive-by-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!