- An alternative approach with a single loop could be:
@echo off
set "_VolumeName=BOOTDISK" && for /f usebackq^ ^Tokens^=^4^ ^Delims^=^<^> %%i in (wmic LogicalDisk where "DriveType=2 and VolumeName='%%_VolumeName%%'" get DeviceID^,VolumeSerialNumber /format:xml ^| find "VALUE") do echo/"%%~i"| >nul find ":" && set "_DeviceID=%%~i" || set "_VolumeSerialNumber=%%~i"
set _ | findstr "_Dev _Vol""
_DeviceID=D:
_VolumeName=BOOTDISK
_VolumeSerialNumber=9ED49E6A
- A different way to handle this, using a single loop, could be:
@echo off
set "_VolumeName=BOOTDISK"
%:^?
if not defined _Caption =;( set "_get=Get Caption" );= else if not defined _DeviceID =;( set "_get=Get DeviceID"
);= else if not defined _VolumeSerialNumber =;( set "_get=Get VolumeSerialNumber" );= else set "_get=" && goto %:^!
for /f usebackq^ ^Tokens^=^4^ ^Delims^=^<^> %%i in =;(call wmic LogicalDisk where "DriveType=2 and VolumeName='%%_VolumeName%%'" %%_get%% /format:xml ^| find "VALUE");= do set "%get:* =%=%%~i" & goto %:^?
%:^!
echo/1st for loop output:
for %%G in =;(
Caption,DeviceID,VolumeName,VolumeSerialNumber
);= do call echo/%%~G=%%_%%~G%%
echo/
echo/2nd for loop output:
for /f tokens^=^1*^ ^ delims^=^= %%i in =;(
'set _ ^| findstr "Capti Devi Volume"'
);= do echo/VariableName %%~i == %%~j
This way, you’re pulling more detailed info in one go, like the DriveID, VolumeName, VolumeSerialNumber, and other useful properties. By filtering with DriveType=2 (removable drives) and checking for VolumeName='BOOTDISK', you can easily target the specific USB drive you’re interested in, getting all the key details at once.
1st for loop output:
Caption=D:
DeviceID=D:
VolumeName=BOOTDISK
VolumeSerialNumber=9ED49E6A
2nd for loop output:
VariableName _Caption == D:
VariableName _DeviceID == D:
VariableName _VolumeName == BOOTDISK
VariableName _VolumeSerialNumber == 9ED49E6A
- "I have a USB drive which letter/path could differ, but its name never changes".
- The volume name will always remain the same, and since this is a USB device, the drive type will consistently be 2. Therefore, I suggest using
DriveType=2 and VolumeName='BOOTDISK' as conditions to get the relevant output/value.
wmic LogicalDisk where "DriveType=2 and VolumeName='BOOTDISK'" get driveID
Numeric value that corresponds to the
type of disk drive this logical disk represents.
| Name |
Value |
Description |
| Unknown |
0 |
The type of drive is unknown. |
| NoRootDirectory |
1 |
The drive does not have a root directory. |
| Removable |
2 |
The drive is a removable storage device, such as a USB flash drive. |
| Fixed |
3 |
The drive is a fixed disk. |
| Network |
4 |
The drive is a network drive. |
| CDRom |
5 |
The drive is an optical disc device, such as a CD or DVD-ROM. |
| Ram |
6 |
The drive is a RAM disk. |
Additional resources: