Search code examples
batch-filecmdwmiwmic

Get all drive letters of a drive/disk


Get all drive letters of a drive/disk

Example:

Disk0 - C:, D:, E:
Disk1 - F:, G:, H:

I want to get all drive letters of a drive/disk. I tried the code:

WMIC PATH Win32_DiskDrive WHERE 'Index=0' Assoc:list /AssocClass:Win32_Volume

WMIC PATH Win32_DiskDrive WHERE 'Index=0' Assoc:list /ResultClass:Win32_DiskPartition

But the result not as I need.


Solution

  • Here's an example , feel free to try and modify it as necessary.

    @For /F Delims^=^= %%Z In ('Set Disk 2^>Nul')Do @Set "%%Z="
    @For /F Delims^= %%V In ('WMIC DiskDrive Assoc^
     /AssocClass:Win32_DiskDriveToDiskPartition 2^>NUL^|Find /I "Disk #"'
    )Do @For /F Tokens^=2Delims^=^" %%W In ("%%V"
    )Do @For /F Tokens^=2^,4Delims^=^" %%X In ('WMIC Path^
     Win32_LogicalDiskToPartition 2^>NUL^|Find "%%W" 2^>Nul'
    )Do @For /F "Tokens=2Delims=#," %%Z In ("%%X")Do @If Defined Disk%%Z (
        Call Set "Disk%%Z=%%Disk%%Z%% %%Y")Else Set "Disk%%Z=%%Y")
    @Set Disk 2>NUL&&Pause
    

    Example output, (Windows 7):

    Disk0=C:
    Disk2=E:
    Disk3=F: G: H:
    Press any key to continue . . .