Reference a Volume/Drive by Label

前端 未结 3 1441
天涯浪人
天涯浪人 2020-12-18 04:54

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

相关标签:
3条回答
  • 2020-12-18 04:57

    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.

    0 讨论(0)
  • 2020-12-18 05:07

    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"
    
    0 讨论(0)
  • 2020-12-18 05:09

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

    0 讨论(0)
提交回复
热议问题