Jump to content











Photo
- - - - -

Get Physical Drive number from Drive letter under WinPE

winpe drive

  • Please log in to reply
26 replies to this topic

#1 steve6375

steve6375

    Platinum Member

  • Developer
  • 7566 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films
  •  
    United Kingdom

Posted 09 November 2016 - 05:01 PM

I wanted a way to get the drive number if the drive letter was known.

It has to work under minimal WinPE (with no WoW64 support) on both 32-bit and 64-bit Windows.

VB Script, WMIC or PowerShell cannot be used as it may not be present in a minimal or old WinPE version.

I could not find a native 64-bit utility, so I came up with this cmd script which uses FIND and DISKPART

@echo off
REM SteveSi www.easy2boot.com
REM Script to get Physical drive number from drive letter (32/64 bit WinPE)
if @%1@==@@ echo Please specify Drive Letter, e.g. "%~nx0 F:" & pause & goto :EOF
echo select volume %1 > dp.scr
echo list disk >> dp.scr
diskpart /s dp.scr > ss.txt
REM Find line marked with asterisk
find "* " ss.txt > ss1.txt
set DISK=
set D1=
set D2=
set D3=
REM allow for different languages, read 3rd line. For English 3rd param is disk number
REM Output will be * (word(s) for 'disk') number - e.g. * Disk 1  or * Disque 1 or * Datentrager 1
for /F "tokens=3,4,5 skip=2"  %%A in (ss1.txt) do set D1=%%A & set D2=%%B & set D3=%%C
REM C is 3rd character. If a number 0-99 then C will be undefined\empty
set C=%D3:~2,1%
if not "%D3%"=="" if not defined C set /a DISK=%D3%
set C=%D2:~2,1%
if not "%D2%"=="" if not defined C set /a DISK=%D2%
set C=%D1:~2,1%
if not "%D1%"==""  if not defined C set /a DISK=%D1%
if not "%DISK%"=="" echo %1 is on DISK %DISK% (environment variable DISK = %DISK%)
if "%DISK%"=="" echo ERROR: NO DISK NUMBER FOUND & pause
if exist dp.scr del dp.scr
if exist ss.txt del ss.txt
if exist ss1.txt del ss1.txt

This relies on the diskpart 'list drive' output 3rd, 4th or 5th word being the drive number.

I am unsure if this would work in all languages (e.g. Japanese or Chinese, etc.)

 

Could you please test it in every non-English language that you have and let me know if any problems?

 

e.g. save as test.cmd and then run under Windows or WinPE

 

test F:

or

test F



#2 maggreg

maggreg
  • Members
  • 3 posts
  •  
    Poland

Posted 09 November 2016 - 07:27 PM

Nice, thanks.

You can try this without temp file creation:

 

FOR /f "tokens=3,4,5" %%A IN ('^(echo sel vol %1 ^& echo lis dis^) ^| diskpart ^| find "* "') do (set D1=%%A & set D2=%%B & set D3=%%C)

 

pzdr



#3 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 09 November 2016 - 07:42 PM

Maybe:
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
IF "%1."=="." ECHO Missing Parameter&GOTO :EOF
SET MyVol=%1
SET MyDisk=
FOR /F "tokens=1 delims=*" %%A IN ('
^(
ECHO Select Volume %MyVol%^&ECHO list disk
^)^|diskpart^|FIND "* "
') DO SET Mydisk=%%A

IF NOT DEFINED Mydisk ECHO ERROR: NO DISK NUMBER FOUND&GOTO :EOF
SET Mydisk=%Mydisk:  =§%
FOR /F "tokens=1 delims=§" %%A IN ("%MyDisk%") DO SET MyDisk=%%A
SET /A MyDisk=%MyDisk:~-2,2%
ECHO %MyVol%: is on DISK %MyDisk% 
No need for temporary files ;) and removing the fuss about 3rd, 4th or 5th parameter by ASSUMING :w00t: :ph34r: that there is more than one space after the DISK number :dubbio:(and still valid for drives 0-99 through SET /A use)
 
:duff:
Wonko

P.S.: this is "better", the only assumption is that the word(s) for "Disk" do not contain a number:
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
IF "%1."=="." ECHO Missing Parameter&GOTO :EOF
SET MyVol=%1
SET MyDisk=
FOR /F "delims=" %%A IN ('
^(
ECHO Select Volume %MyVol%^&ECHO list disk
^)^|diskpart^|FIND "* "
') DO SET Mydisk=%%A

IF NOT DEFINED Mydisk ECHO ERROR: NO DISK NUMBER FOUND&GOTO :EOF
FOR /F "tokens=1 delims=0123456789" %%A IN ("%MyDisk%") DO SET /A Mydisk=!Mydisk:%%A=! 2>nul
ECHO %MyVol%: is on DISK %MyDisk% 

  • nguyentu likes this

#4 maggreg

maggreg
  • Members
  • 3 posts
  •  
    Poland

Posted 09 November 2016 - 08:05 PM

@Wonko space after * (FIND "* ") is important for GPT disks.

 

pzdr



#5 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 09 November 2016 - 08:41 PM

@Wonko space after * (FIND "* ") is important for GPT disks.
 
pzdr

Thanks, fixed. :)
 
:duff:
Wonko

#6 steve6375

steve6375

    Platinum Member

  • Developer
  • 7566 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films
  •  
    United Kingdom

Posted 09 November 2016 - 09:38 PM

Great! Wonko's last script seems pretty robust. :1st:

I will try to test on various WinPE language versions...

P.S. I added to the end...

ENDLOCAL & set DISK=%MyDisk%

so that DISK variable is set when this is called.


Edited by steve6375, 09 November 2016 - 09:42 PM.


#7 netlord

netlord

    Member

  • Members
  • 36 posts
  •  
    Germany

Posted 10 November 2016 - 09:16 AM

Hi there

 

my needs was, of course, slightly different. For example I only need fixed disks.

I solved it via powershell and WMI

 

 

 

$diskdrive = gwmi win32_diskdrive #-Filter "MediaType='Fixed hard disk media'"

 
foreach($drive in $diskdrive)
 
{
out-host -InputObject "`nDEVICE - $($drive.deviceid.substring(4)) Model - $($drive.model)"
##partition
$partitions = gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($drive.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
foreach($part in $partitions)
{
Out-Host -InputObject "`tPartition: $($part.name)"
$vols = gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($part.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"
 
foreach($vol in $vols)
{
out-host -InputObject "`t`t$($vol.name)"
}
}
}
 

 

This version looks for all disks (because -Filter "MediaType='Fixed hard disk media'" is out-commented

With little changes it should meet your needs.



#8 steve6375

steve6375

    Platinum Member

  • Developer
  • 7566 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films
  •  
    United Kingdom

Posted 10 November 2016 - 10:08 AM

As explained in the first post, I wanted a solution that does not use WMI or Powershell



#9 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 10 November 2016 - 02:12 PM

On second thought (and of no actual importance whatever) the [SPACE] after the [asterisk] can be removed using "detail volume" instead of  "list disk", this way only the line of the disk pertaining to the volume will be shown (and it will have either one - MBR - or two - GPT - asterisks in it).

:duff:

Wonko



#10 nguyentu

nguyentu

    Frequent Member

  • Advanced user
  • 102 posts
  •  
    Vietnam

Posted 11 November 2016 - 09:04 AM

Thanks, but this method may not work on Windows XP. DiskPart does not list the USB right?



#11 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 11 November 2016 - 10:38 AM

Thanks, but this method may not work on Windows XP. DiskPart does not list the USB right?

Not really really, it doesn't list the "removable" devices connected to USB.

It is OK with USB hard disks or with USB sticks set as "fixed".

 

:duff:

Wonko



#12 nguyentu

nguyentu

    Frequent Member

  • Advanced user
  • 102 posts
  •  
    Vietnam

Posted 11 November 2016 - 10:47 AM

Not really really, it doesn't list the "removable" devices connected to USB.

It is OK with USB hard disks or with USB sticks set as "fixed".

 

:duff:

Wonko

Well, removable devices  :loleverybody:



#13 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 11 November 2016 - 11:21 AM

Well, removable devices  :loleverybody:

In case:
http://reboot.pro/to...st-and-ddlistw/

http://reboot.pro/to...listw/?p=173100

and:

http://reboot.pro/to...command-prompt/

 

:duff:

Wonko


  • nguyentu likes this

#14 nguyentu

nguyentu

    Frequent Member

  • Advanced user
  • 102 posts
  •  
    Vietnam

Posted 11 November 2016 - 11:36 AM

 
Thanks, I just wanted to mention this case for someone not tried it on Windows XP.
 
As Steve said, we should not use the WMI, Powershell or 32-bit app on 64-bit system because these packages are not included on WinPE by default.


#15 pscEx

pscEx

    Platinum Member

  • Team Reboot
  • 12707 posts
  • Location:Korschenbroich, Germany
  • Interests:What somebody else cannot do.
  •  
    European Union

Posted 11 November 2016 - 12:24 PM

Under German win7 / 64 works correct when running as administrator.

Execute the batch a user fails.

 

May be interesting: I have mounted an Acronis backup of a different PC as drive L:

Result "Not found". A hint could be nice that this drive is defined in the OS, but not physically existing. In my case it is physically located on drive G: G: is correctly recognised as drive #2

 

Peter



#16 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 11 November 2016 - 12:42 PM

 

 
Thanks, I just wanted to mention this case for someone not tried it on Windows XP.
 
As Steve said, we should not use the WMI, Powershell or 32-bit app on 64-bit system because these packages are not included on WinPE by default.

 

Well :), it's not like there are all these builds of 64-bit WinPE 1.x (built from XP 64-bit or Server 2003 64-bit) around, as a matter of fact I am not even sure they exist at all. :dubbio:

On a 32-bit PE (that has WOW64 built-in ;)) you can use *any* third-party app (that probably does exist in 32 bit, even if Steve failed to reference which one(s) he had in mind), but the mentioned version of dd.exe works fine with all kind of disk-like devices, fixed and removable AFAICR.

 

@Peter

It depends on HOW EXACTLY (though which tool) you mount an image file, Windows SDK tool, Total Munter and Arsenal Image Mounter create a Physicaldrive, other drivers don't (or don't integrate them in the OS fully).

 

:duff:

Wonko



#17 nguyentu

nguyentu

    Frequent Member

  • Advanced user
  • 102 posts
  •  
    Vietnam

Posted 11 November 2016 - 11:18 PM

@Wonko

I think if I want to use dd.exe on the 64-bit WinPE that it does not include WOW64 (from Windows ADK, Windows Setup, Recovery...), I need to recompile the 64-bit version.



#18 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 12 November 2016 - 10:45 AM

@Wonko

I think if I want to use dd.exe on the 64-bit WinPE that it does not include WOW64 (from Windows ADK, Windows Setup, Recovery...), I need to recompile the 64-bit version.

I don't get it (there must be some communication problem).

Sure, if you want to use a 32-bit executable on a 64-bit OS (or PE) you need working WOW64, but in the context of the problem at hand, the little batch using diskpart solves the problem nicely.

On a Windows XP or Server 2003 (or PE 1.x) the diskpart won't list the "removable" drives, BUT on 32-bit OS or PE you can use the other batch(es) using the dd.exe by John Newbigin.

On a NON EXISTING 64-bit PE 1.x (without WOW64) both methods won't work, the first for removable drives only, the other won't work at all.

 

Now, for no apparent reason :w00t:, the Nirsoft DriveLetterView (which exists in both 32 and 64 bit flavours, and needs no install) can gather this info, with the problem (for me, but not necessarily for the OP or for other people) that since it is a GUI and has text output to file (not pipable AFAIK) one needs a temporary file to use it from a batch :
 http://www.nirsoft.n...etter_view.html

 

:duff:

Wonko



#19 steve6375

steve6375

    Platinum Member

  • Developer
  • 7566 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films
  •  
    United Kingdom

Posted 12 November 2016 - 11:53 AM

I did actually consider the NirSoft utility but the stext output seemed too difficult to parse. Also the diskpart solution needs no external program.

However, looking at the NirSoft utility again, the scomma output looks promising:

REM Usage %0 J:
set DISK=
DriveLetterView.exe /scomma fred.txt
find "%1\" fred.txt > fred1.txt && (
for /f "delims=, tokens=8" %%A in (fred1.txt) do echo %%A > fred2.txt
for /f "delims=e tokens=2" %%A in (fred2.txt) do set /a DISK=%%A
)
echo DISK %DISK%

I am sure you could tidy it up better....



#20 nguyentu

nguyentu

    Frequent Member

  • Advanced user
  • 102 posts
  •  
    Vietnam

Posted 12 November 2016 - 01:47 PM

I have made an application to get this result. Very easy!

Gdfy.exe --deviceid --drive=C
Gdfy.exe --deviceid --drive=D:
Gdfy.exe --deviceid --drive=E

Add --rae parameter to get the ERRORLEVEL as a number of disks, 1110 if not successful.

Gdfy.exe --deviceid --drive=C --rae

Gdfy.exe (25Kb)



#21 Guest_AnonVendetta_*

Guest_AnonVendetta_*
  • Guests

Posted 12 November 2016 - 02:08 PM

So.....what exactly is the use/advantage of knowing the drive #? Shouldn't the letter alone be good enough?



#22 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 12 November 2016 - 02:17 PM

@Steve
Well, the /stab is IMHO even easier to parse generically, but since you are anyway writing temp files, you can use the driveletterview.cfg ;):
@ECHO OFF
REM USAGE usendlv.cmd C:
(
ECHO [General]
ECHO Columns=64 00 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00 06 00 64 00 07 00 00 00 08 00 00 00 09 00 00 00 0A 00 00 00 0B 00 00 00 0C 00 00 00 0D 00 00 00 0E 00 00 00 0F 00 00 00 10 00 00 00 11 00 00 00 12 00 00 00 13 00 00 00 14 00
)>driveletterview.cfg
driveletterview.exe /stab DriveLetterView.cfg
FOR /F "tokens=2 delims=e" %%A in ('TYPE DriveLetterView.cfg ^|FIND "%1"') DO SET DISK=%%A
ECHO Volume %1 is on disk %disk%
@nguyentu
Is that 32 bit or 64 bit (or both? :w00t:)
 
Seriously, there is no real need of having such a complex syntax (IF the little tool only does this), I mean if it is a "vertical" tool that Gets Disk From Yourinput,
gdfy C:  
would be easier, as a side-side note:
C:\appoggio\Steve6375batches>gdfy
Missing parameters!
Visit www.sitecuatui.com for more information!

C:\appoggio\Steve6375batches>gdfy /?
Drive invalid!

C:\appoggio\Steve6375batches>gdfy --help
Drive invalid!

C:\appoggio\Steve6375batches>gdfy /?
Drive invalid!

C:\appoggio\Steve6375batches>gdfy /h
Drive invalid!
:w00t: :ph34r:
Anyway nice little thingy :).
 
@WhateverIsYourNameToday
If you don't know the difference between a volume and a disk (or between a drive letter and a physicaldrive), it's OK, you don't need this.
 
:duff:
Wonko

#23 Guest_AnonVendetta_*

Guest_AnonVendetta_*
  • Guests

Posted 12 November 2016 - 02:24 PM

@Wonko: How dare you call me by my given name without permission! Soon it will be the exact inverse of your nick.

 

I have an OK understanding of the diff between volume, partition, and disk, but why knowing the # is of any use.....



#24 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 12 November 2016 - 02:47 PM

I have an OK understanding of the diff between volume, partition, and disk, but why knowing the # is of any use.....

As an example if you have to access a sector (like the MBR) which is on a disk but not within the volume extents.
Typical example is to check Disk Signature or the partition tables on the PhysicalDrive (BTW checking disk signatures and offsets comparing them to data in MountedDevices in the Registry could be another way, though it won't as well work for non-fixed devices such as USB sticks).

:duff:
Wonko

#25 waqas4400

waqas4400
  • Banned
  • 1 posts
  •  
    Pakistan

Posted 27 November 2016 - 05:51 AM

??? Can solve your problem. It can also recover data and much more. You can manage your partition using this application.


Edited by pscEx, 27 November 2016 - 09:31 AM.
We do not support cracking here






Also tagged with one or more of these keywords: winpe, drive

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users