Jump to content

Get Disk Number From Drive Letter In CMD


Recommended Posts

Good day

I have googled and googled but cant seam to get a working answer.

I want to have a cmd batch file get the current drive letter then using the drive letter get the disk number eg

@echo offpushd "%~dp0"echo %~d0set _DiskNum = wmic diskdrive %~d0 get disknumber #I know this isn't a real commandgrubinst.exe --verbose -t=0 --silent-boot --skip-mbr-test(%_DiskNum%) #This command I will stillpause                                                                 #work out once the above exit                                                                  #command is working.

Thank you in advance

Link to comment
Share on other sites


This is not going to be an incredibly easy task.  In fact, you may have to go VBS or program for it.

 

Basically for WMI, you would have to hit Win32_LogicalDisk and then link it to Win32_DiskPartition in order to see what you're getting.  Or if you have string testing capability, just checking Win32_LogicalDiskToPartition would cut it if you can get the data out.   Microsoft provides an example here.

 

As for API, you'd have to call DeviceIoControl using IOCTL_STORAGE_GET_DEVICE_NUMBER

 

But it seems there's no "one shot" answer to this.

Link to comment
Share on other sites

Okay here is what I have its almost done

ComputerName = "."dim strDriveNumset objFSO = CreateObject("Scripting.FileSystemObject")Set objShell = CreateObject("Wscript.Shell")Dim oDicSet oDic = CreateObject("scripting.dictionary")Set wmiServices  = GetObject ( _    "winmgmts:{impersonationLevel=Impersonate}!//" & ComputerName)' Get physical disk driveSet wmiDiskDrives =  wmiServices.ExecQuery ( "SELECT Caption, DeviceID FROM Win32_DiskDrive")strPath = objShell.CurrentDirectorystrDrive = objFSO.GetDriveName(strPath)For Each wmiDiskDrive In wmiDiskDrives    'Use the disk drive device id to    ' find associated partition    query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _        & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"        Set wmiDiskPartitions = wmiServices.ExecQuery(query)    For Each wmiDiskPartition In wmiDiskPartitions        'Use partition device id to find logical disk        Set wmiLogicalDisks = wmiServices.ExecQuery _            ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _             & wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")         For Each wmiLogicalDisk In wmiLogicalDisks					if wmiLogicalDisk.DeviceID = strDrive then					strDriveNum = wmiDiskDrive.DeviceID					end if        Next          NextNextdrv0 = "\\.\PHYSICALDRIVE0"drv1 = "\\.\PHYSICALDRIVE1"drv2 = "\\.\PHYSICALDRIVE2"drv3 = "\\.\PHYSICALDRIVE3"drv4 = "\\.\PHYSICALDRIVE4"drv5 = "\\.\PHYSICALDRIVE5"drv6 = "\\.\PHYSICALDRIVE6"drv7 = "\\.\PHYSICALDRIVE7"drv8 = "\\.\PHYSICALDRIVE8"drv9 = "\\.\PHYSICALDRIVE9"if strDriveNum = drv0 thenstrDrv = "(hd0)"end ifif strDriveNum = drv1 thenstrDrv = "(hd1)"end ifif strDriveNum = drv2 thenstrDrv = "(hd2)"end ifif strDriveNum = drv3 thenstrDrv = "(hd3)"end ifif strDriveNum = drv4 thenstrDrv = "(hd4)"end ifif strDriveNum = drv5 thenstrDrv = "(hd5)"end ifif strDriveNum = drv6 thenstrDrv = "(hd6)"end ifif strDriveNum = drv7 thenstrDrv = "(hd7)"end ifif strDriveNum = drv8 thenstrDrv = "(hd8)"end ifif strDriveNum = drv9 thenstrDrv = "(hd9)"end ifstrRun = Chr(34) & strDrive & "\grubinst.exe" & Chr(34) & " --verbose -t=0 --silent-boot --skip-mbr-test" & strDrvwscript.echo strRunobjShell.Run strRun, 9

Can someone maybe help with tidying up?

Edited by silent001
Link to comment
Share on other sites

I can't help much on the VBS side, but I notice since I tried this as a program that it requires admin rights to get the file handle to call DeviceIOControl.  So it may require you to elevate the script too to work.

Link to comment
Share on other sites

While I'm sure there's a good VBS solution to this, I thought I'd go ahead and post the program since I attempted it.

(past all the UAC elevation stuff I had to put in the BAT)

rem ----D:rem grubinst.exe --verbose -t=0 --silent-boot --skip-mbr-test(0)

Seems to be catching the general idea.  Though my batch skills aren't the best these days.

@echo offpushd "%~dp0"echo %~d0disknumber %~d0 > temp.txtset /p _DiskNum=<temp.txtdel temp.txt@echo onrem grubinst.exe --verbose -t=0 --silent-boot --skip-mbr-test(%_DiskNum%) pause

But I would say it's certainly a sub-optimal solution, especially if you can get the VBS to go fully.  In fact, i'd go ahead and program the call to grubinst.exe within the program or whatever it is I'm wanting to do with the data.

(File Pulled)

Edited by Glenn9999
  • Upvote 1
Link to comment
Share on other sites

This is my final script and working

ComputerName = "."dim strDriveNumset objFSO = CreateObject("Scripting.FileSystemObject")Set objShell = CreateObject("Wscript.Shell")Dim oDicSet oDic = CreateObject("scripting.dictionary")Set wmiServices  = GetObject ( _    "winmgmts:{impersonationLevel=Impersonate}!//" & ComputerName)' Get physical disk driveSet wmiDiskDrives =  wmiServices.ExecQuery ( "SELECT Caption, DeviceID FROM Win32_DiskDrive")strPath = objShell.CurrentDirectorystrDrive = objFSO.GetDriveName(strPath)For Each wmiDiskDrive In wmiDiskDrives    'Use the disk drive device id to    ' find associated partition    query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _        & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"        Set wmiDiskPartitions = wmiServices.ExecQuery(query)    For Each wmiDiskPartition In wmiDiskPartitions        'Use partition device id to find logical disk        Set wmiLogicalDisks = wmiServices.ExecQuery _            ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _             & wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")         For Each wmiLogicalDisk In wmiLogicalDisks					if wmiLogicalDisk.DeviceID = strDrive then					strDriveNum = wmiDiskDrive.DeviceID					end if        Next          NextNextdrv0 = "\\.\PHYSICALDRIVE0"drv1 = "\\.\PHYSICALDRIVE1"drv2 = "\\.\PHYSICALDRIVE2"drv3 = "\\.\PHYSICALDRIVE3"drv4 = "\\.\PHYSICALDRIVE4"drv5 = "\\.\PHYSICALDRIVE5"drv6 = "\\.\PHYSICALDRIVE6"drv7 = "\\.\PHYSICALDRIVE7"drv8 = "\\.\PHYSICALDRIVE8"drv9 = "\\.\PHYSICALDRIVE9"if strDriveNum = drv0 thenstrDrv = "(hd0)"end ifif strDriveNum = drv1 thenstrDrv = "(hd1)"end ifif strDriveNum = drv2 thenstrDrv = "(hd2)"end ifif strDriveNum = drv3 thenstrDrv = "(hd3)"end ifif strDriveNum = drv4 thenstrDrv = "(hd4)"end ifif strDriveNum = drv5 thenstrDrv = "(hd5)"end ifif strDriveNum = drv6 thenstrDrv = "(hd6)"end ifif strDriveNum = drv7 thenstrDrv = "(hd7)"end ifif strDriveNum = drv8 thenstrDrv = "(hd8)"end ifif strDriveNum = drv9 thenstrDrv = "(hd9)"end ifstrRunPath = "grubinst"strRun = "grubinst.exe --verbose -t=0 --silent-boot --skip-mbr-test " & strDrvstrCommand = "cmd /K @echo off & CD " & strDrive & " & cd " & strRunPath & " & " & strRun & " & pause & exit"wscript.echo strCommandobjShell.run strCommand'Set oShell = Nothing

 Glenn9999 your batch files don't work it says  disknumber is not  a valid command

Link to comment
Share on other sites

>Glenn9999 your batch files don't work it says  disknumber is not  a valid command

 

Did you read what I wrote?

 

"While I'm sure there's a good VBS solution to this, I thought I'd go ahead and post the program since I attempted it.

 

Looking at attachment: Download count...0.

Link to comment
Share on other sites

Here is a new WMI Query that I hope is more to what you need.

 

'-> WMI ObjectDim Wmi :Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")'-> Run Time VariblesDim Obj, Rpt'-> Wmi Query And Class Loop    For Each Obj in Wmi.ExecQuery("SELECT * FROM Win32_DiskDrive")'-> Collect The Objects For The Wmi report    Rpt = Rpt & "-----------------------------------" & vbCrLf & _    "Interface" &vbtab& Obj.InterfaceType & vbCrLf & _    "Model    " &vbtab& Obj.Model & vbCrLf & _    "Name    " & vbtab& Obj.Name & vbCrLf   Next'-> Report The WMI Query MsgBox Space(10) & "WMI Disk Drive Report" & vbCrLf & Rpt,4120,"Result WMI"
post-5386-0-52439400-1448647111_thumb.pn
Link to comment
Share on other sites

Silent I may be a little late for the party, but here is a snippet of a audit batch code that can be run

as non-admin for basic hard drive information.

 

Happy Thanksgiving!

~DP

 



@Echo Off
SetLocal

:: SYSTEM INFORMATION FOR LOCAL PRIMARY HARD DRIVE
::
:: ### HD MODEL:
For /F "Tokens=2 Delims==\" %%A In ('WMIc DiskDrive Get Model /Value') Do Set HDModel=%%A

:: ### HD SERIAL NUMBER:
For /F "Tokens=2 Delims==\" %%A In ('WMIc DiskDrive Get Serialnumber /Value') Do Set HDSerial=%%A

:: ### HD FREE SPACE:
For /F "UseBackQ Tokens=1-2 Delims==" %%A In (`WMIc LogicalDisk Where^
"DeviceID = '%CD:~,2%'" Get FreeSpace^, Size /Value`) Do If "%%B" NEq "" (
Call Set _%%A=%%B)
Rem Set/a _FreePercent=%_FreeSpace:~,-3% / %_Size:~,-5%
Set/a _FreePercent=%_FreeSpace:~,-6% / %_Size:~,-8%
If %_FreeSpace:~,-6% lss 214748 (Set/a _FreeSpace=%_FreeSpace:~,-6%*9313/10000
) Else (Set/a _FreeSpace=%_FreeSpace:~,-6%/1074*1000)

:: ### OUTPUT:
Echo. Hard Drive Model: %HDModel%
Echo. HD Serial Number: %HDSerial%
Echo. HD Free Space: Local Drive %CD:~,2% %_FreeSpace:~,-3% GB = %_FreePercent%%%^ Available
Endlocal
Pause>Nul


Link to comment
Share on other sites

The topic (or something very close to it) has been discussed extensively here:
http://reboot.pro/topic/20240-how-to-get-partitions-path-in-command-prompt/
http://reboot.pro/topic/20476-retrieving-devicedrive-information/

Maybe you can find something of interest in the above, but more specifically if I get it right your question revolves around "On which \\.\PhysicalDrive# is the current partition/volume residing?"

IF this is the case, I believe this would do nicely:

@echo offecho This cmd has been run from LogicalDrive %~d0\FOR /F "tokens=2 delims=#," %%? IN ('WMIC Path Win32_LogicalDiskToPartition Get Antecedent^, Dependent  ^|FIND "%~d0"') DO (echo LogicalDrive %~d0\ is residing on \\.\Physicaldrive%%?)pause

(slightly longer than a one-liner in batch)


jaclaz

Link to comment
Share on other sites

Try some plain commands:

WMIC Path Win32_LogicalDiskToPartition Get Antecedent, Dependent

Or a:

WMIC Path Win32_LogicalDiskToPartition Get Antecedent, Dependent /Format:table

Or a:

WMIC Path Win32_LogicalDiskToPartition Get Antecedent, Dependent /Format:csv

Maybe you have not the "default" output of the WMIC command?

 

jaclaz

Link to comment
Share on other sites

All 3 work OK ... see, for instance:

N:\>WMIC Path Win32_LogicalDiskToPartition Get Antecedent, DependentAntecedent                                                                        Dependent\\MACHINE-NAME\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #0"  \\MACHINE-NAME\root\cimv2:Win32_LogicalDisk.DeviceID="C:"\\MACHINE-NAME\root\cimv2:Win32_DiskPartition.DeviceID="Disk #1, Partition #0"  \\MACHINE-NAME\root\cimv2:Win32_LogicalDisk.DeviceID="D:"\\MACHINE-NAME\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #1"  \\MACHINE-NAME\root\cimv2:Win32_LogicalDisk.DeviceID="E:"\\MACHINE-NAME\root\cimv2:Win32_DiskPartition.DeviceID="Disk #2, Partition #0"  \\MACHINE-NAME\root\cimv2:Win32_LogicalDisk.DeviceID="F:"\\MACHINE-NAME\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #1"  \\MACHINE-NAME\root\cimv2:Win32_LogicalDisk.DeviceID="G:"\\MACHINE-NAME\root\cimv2:Win32_DiskPartition.DeviceID="Disk #1, Partition #1"  \\MACHINE-NAME\root\cimv2:Win32_LogicalDisk.DeviceID="H:"\\MACHINE-NAME\root\cimv2:Win32_DiskPartition.DeviceID="Disk #2, Partition #1"  \\MACHINE-NAME\root\cimv2:Win32_LogicalDisk.DeviceID="I:"\\MACHINE-NAME\root\cimv2:Win32_DiskPartition.DeviceID="Disk #3, Partition #1"  \\MACHINE-NAME\root\cimv2:Win32_LogicalDisk.DeviceID="J:"\\MACHINE-NAME\root\cimv2:Win32_DiskPartition.DeviceID="Disk #3, Partition #0"  \\MACHINE-NAME\root\cimv2:Win32_LogicalDisk.DeviceID="K:"\\MACHINE-NAME\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #2"  \\MACHINE-NAME\root\cimv2:Win32_LogicalDisk.DeviceID="L:"\\MACHINE-NAME\root\cimv2:Win32_DiskPartition.DeviceID="Disk #8, Partition #0"  \\MACHINE-NAME\root\cimv2:Win32_LogicalDisk.DeviceID="T:"\\MACHINE-NAME\root\cimv2:Win32_DiskPartition.DeviceID="Disk #8, Partition #1"  \\MACHINE-NAME\root\cimv2:Win32_LogicalDisk.DeviceID="V:"\\MACHINE-NAME\root\cimv2:Win32_DiskPartition.DeviceID="Disk #5, Partition #0"  \\MACHINE-NAME\root\cimv2:Win32_LogicalDisk.DeviceID="Y:"
Link to comment
Share on other sites

 

All 3 work OK ... see, for instance:

 

They may work fine :), but the drive letter N: is nowhere to be seen in the output, which should mean that it is not a partition that is mounted to drive letter N: :w00t:.

 

jaclaz

Link to comment
Share on other sites

 

All 3 work OK ... see, for instance:

They may work fine :), but the drive letter N: is nowhere to be seen in the output, which should mean that it is not a partition that is mounted to drive letter N: :w00t:.

 

Of course! N: is a 12.5 GB Gavotte Ramdisk !!! :yes:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.


×
×
  • Create New...