Search code examples
windowsbatch-filecommand-linewmiwmic

Translate Hard drive letter into the corresponding Device ID (or viceversa)


I need to Identify the drive letter of a Manufacturer ID of a hard drive device (or maybe viceversa thing I could do it too).

The command to retrieve the manufacturer IDs:

WMIC.exe DiskDrive Get /Format:List

Example Output (splitted and with HTML entities formatted):

PNPDeviceID=DISK&VEN_WDC_WD10&PROD_02FAEX-00Z3A0

The command to retrieve the drive letters:

WMIC.exe Volume Get /Format:List

Example Output:

DriveLetter=C:

The problem is that I can't find any useful property that I could associate from both outputs to make a query, I mean I don't know what to do with the manufacturer IDs to search the drive letter of each ID, I can't see any way to identify the drive letter of the ID.

So at the moment that I get the DeviceID DISK&VEN_WDC_WD10&PROD_02FAEX-00Z3A0 I need to translate it to the equivalent driveLetter, which is C:, that's all.

I've tried using WMIC tool 'cause I don't know how to associate this info using native commandline tools provided in Windows, but really is not full necessary for me to accomplis this task accesing to WMI Classes, I could accept a solution using other presintalled tools in Windows (maybe BCDedit?), or a solution in VBScript language should be accepted too, but for environment circunstances I can't do this task in any other languages (included native PowerShell) and also not using 3rd party tools like Devcon utility provided by Microsoft.

The reason why I need that is to finish this Script which should retrieve and exclude the DeviceID of the DriveLetter C::

@Echo OFF & REM Mode con cols=150 lines=50

:: Exclude this drive during the process.
Set "ExcludedDrive=C:"

:: This variable should be set later,
:: Stores the device ID of the drive letter excluded above.
Set "ExcludedID="

REM ************
REM PSEUDO CODE:
REM ************
REM 
REM To get Volume Information:
REM WMIC.exe Volume Get /Format:List
REM WMIC.exe Volume Where 'DriveLetter="C:"' Get /Format:CSV
REM 
REM To get DiskDrive Information:
REM WMIC.exe DiskDrive Get /Format:List
REM 
REM :: Identify the drive letter of each DeviceID to add exclusions
REM For Each %%DriveLetter in %ExcludedDrive% do (
REM 
REM     :: Retrieve an WMIC Result
REM     Set WMIC_Query_Result=¿?
REM     Set WMIC_Query_Result_DriveLetter=¿?
REM     Set WMIC_Query_Result_DeviceID=¿?
REM 
REM     If %WMIC_Query_Result_DriveLetter% EQU %%DriveLetter (
REM         Set "ExcludedID=%WMIC_Query_Result_DeviceID%"
REM     )
REM )
REM 
REM ******************
REM END OF PSEUDO CODE
REM ******************

For /F "Tokens=* Delims=" %%a In (
    'REG.exe Query "HKLM\SYSTEM\CurrentControlSet\Enum\SCSI" ^| Find /I "Disk&"'
) Do (
    Echo "%%a" | Find /I "%ExcludedID%" || (
        For /F "Tokens=* Delims=" %%b In ('REG.exe Query "%%~a"') Do (
            Reg.exe ADD "%%b\Device Parameters\Disk" /V "UserWriteCacheSetting" /T "REG_DWORD" /D "0x00000000" /F 1>NUL
        )
    )
)

Pause&Exit

Solution

  • Does

    diskpart /s diskpart.script
    

    with two lines in the script

    select disk 0
    detail disk
    

    help?