Jump to content











Photo
- - - - -

how to echo Drive Letter of 1st partition of any Multi Partitioned Fixed USB Drive using Command line tools ?

fixed usb drive windows 10 driver letter

Best Answer Wonko the Sane , 05 April 2017 - 05:51 AM

As seen in the previously given zdrivesOR.cmd, essentially the whole thing fits in a oneliner, here is to make it easier to follow with added the provision by niemtim007 to input a Disk #:

@echo off
setlocal
set mydisk=
set /P mydisk= Disk Number [ ? ] = 
::All Lines
FOR /F  delims^=\^"^ tokens^=5^,11 %%A IN ('WMIC Path Win32_LogicalDiskToPartition Get /Format:csv ') DO ECHO %%A %%B

::Just the drive letter
FOR /F  delims^=\^"^ tokens^=5^,11 %%A IN ('WMIC Path Win32_LogicalDiskToPartition Get /Format:csv ^|FIND "Disk #%mydisk%, Partition #0"') DO ECHO %%B

:duff:

Wonko

Go to the full post


  • Please log in to reply
19 replies to this topic

#1 devdevadev

devdevadev

    Silver Member

  • Advanced user
  • 540 posts
  •  
    India

Posted 04 April 2017 - 10:52 AM

Is there any command line tool that can show (echo) Drive Letter of First Partition of any selected Multi-Partitioned Fixed Disk Drive ?

 

Please help me...



#2 erwan.l

erwan.l

    Platinum Member

  • Developer
  • 3041 posts
  • Location:Nantes - France
  •  
    France

Posted 04 April 2017 - 11:59 AM

Hi,

 

You could drive vmount?

I dont have much batch skills but you should be able to parse the output vmount disks or vmount partitions.

 

or else use wmic command?

 

Regards,

Erwan



#3 devdevadev

devdevadev

    Silver Member

  • Advanced user
  • 540 posts
  •  
    India

Posted 04 April 2017 - 12:31 PM

I don't know how to ask but I want a command line tool which will echo Drive Letter [ L: ] if I choose Disk Drive 3 for following Fixed USB Drive.

 

I mean for below USB Drive if I give

 

post-65263-0-45426400-1491309156.png

 

INPUT - Disk 3, PARTITION 1, Batch file should give OUTPUT = DRIVE LETTER [L:]

 

Regards...

Attached Files



#4 erwan.l

erwan.l

    Platinum Member

  • Developer
  • 3041 posts
  • Location:Nantes - France
  •  
    France

Posted 04 April 2017 - 01:35 PM

WMIC command is probably what you are looking for.

You will need to do the parsing thus.

 

WMIC Path Win32_LogicalDiskToPartition Get Antecedent^,Dependent /Format:list 

 

You can filter with the where clause.


  • genetix likes this

#5 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 04 April 2017 - 06:11 PM

Here:
http://reboot.pro/to...st-and-ddlistw/

the stupid board software botched codeboxes, get the actual batch from here:
http://reboot.pro/to...listw/?p=173100

and some more here:

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

BUT now that Nirsoft's Driveletterview:

http://www.nirsoft.n...etter_view.html

can be piped from console, it is probably the easiest tool to use, *like*:



@ECHO OFF
(
ECHO [General]
ECHO Columns=64 00 00 00 00 00 01 00 96 00 04 00 00 00 05 00 00 00 06 00 00 00 07 00 FA 00 03 00 92 00 02 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
ECHO Sort=7
)>DriveLetterView.cfg
DriveLetterView.exe /stab ""|FIND "Phy"
PAUSE

:duff:

Wonko



#6 genetix

genetix

    Frequent Member

  • Advanced user
  • 132 posts
  •  
    Finland

Posted 04 April 2017 - 06:53 PM

Yeh, Erwan.l is correct, but that's gonna be nasty for loop, heh.

 

Win32_DiskDrive:
Media_Type,Name/DeviceID,Partitions
(get Media_type 'External hard disk media', name or DeviceID match with Media_type, Partitions to match Win32_DiskPartition->DeviceID)
 
Win32_DiskPartition:
Bootable, DeviceID
(Check Bootable, if differ than DeviceID match Bootable to DeviceID = first removable media partitition.)
 
-edit-
 
needs also Win32_LogicalDiskToPartition, if drive letter is identified:
 
Antecedent tokens=2 with delims=" -> Dependent tokens=2 with delims="
 
lol, still nasty damn loop would be way easier to add signature to VolumeID(if known) or "disk_id" file to the partition, although, that's lame.
 
@devdevadev
 
 Can you use PowerShell or .ps1 scripts?


#7 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 04 April 2017 - 07:20 PM

Nothing particularly "nasty", an example batch using ONLY WMIC is on the given thread, here:
http://reboot.pro/to...rompt/?p=189688

and in a clear version here:
http://reboot.pro/to...rompt/?p=189698

as said there:

 


Well, it was just some fun. :)
 
the "clear" batch is not really a batch, it is just a bunch of lines, three for loops, only to show how for simple things a simple batch can compete with the much more complex .vbs script (winning hands down ;) when it comes to go through Ockham's Razor, or more plainly using the KISS principle).

  it's just three FOR loops.

 

:duff:

Wonko


  • devdevadev likes this

#8 devdevadev

devdevadev

    Silver Member

  • Advanced user
  • 540 posts
  •  
    India

Posted 04 April 2017 - 09:14 PM

First thanks for providing great batch file...Sorry but I am unable to understand functioning of variables used within batch file...

 

Blueprint of what I am trying to do is-

SET USBDRIVELETTER=
SET %%B=3
SET %%D=0
CALL mydrivesOR.cmd
SET USBDRIVELETTER=%%E
echo USB Drive Letter %USBDRIVELETTER% is connected. 
But I don't know how to implement it....what is " %%B ", " %%D " and " %%E " ??
 
Why It is not like only " E " or " %E% " ?
 
i don't know much about these complex variables ?? Please help me to grasp all these things...


#9 genetix

genetix

    Frequent Member

  • Advanced user
  • 132 posts
  •  
    Finland

Posted 05 April 2017 - 12:53 AM

variables as you suggest above comes from FOR LOOP DO. if FOR LOOP defines %%A for example as start %%A is token 1 %%B token 2 and so on.

 

As for the loop needed to use WMIC you would basically query/ask class in WMI and section and compare those inside for loop to next class using another for loop or if statement to query/ask the match and when found add that as variable.

 

 

 

For the nasty part... Some of WMIC query compare escapes there are really nasty actually specially the " while capturing Win32_LogicalDiskToPartition is quite some tour to cmd basics, heh.



#10 niemtin007

niemtin007
  • Members
  • 6 posts
  •  
    Vietnam

Posted 05 April 2017 - 03:22 AM

Maybe this will help you

@echo off
setlocal
mode con lines=10 cols=75
:select
set disk=
set /P disk= Disk Number [ ? ] = 
rem check External Portable Hard Drives
wmic diskdrive get Name, Model, InterfaceType, MediaType | find /i "\\.\PHYSICALDRIVE%disk%" | find /i "Fixed hard disk media" >nul
    if errorlevel 1 goto :continue
color 4f& echo.& echo Invalid Input.& pause>nul& cls& goto :select
:continue
rem the first partition is 0
set partition=0
for /f "tokens=3 delims==" %%b in (
'WMIC Path Win32_LogicalDiskToPartition Get Antecedent^, Dependent ^| find "Disk #%disk%, Partition #%partition%"'
) do set letter=%%b
set letter=%letter:~1,2%
echo The letter of the disk %disk%, partition %partition% is %letter%
pause>nul
endlocal
exit

  • genetix and devdevadev like this

#11 devdevadev

devdevadev

    Silver Member

  • Advanced user
  • 540 posts
  •  
    India

Posted 05 April 2017 - 04:06 AM

First lots of thanks for developing feature rich  'MultiBoot_Toolkit_2.1.0'. 

 

Another thanks for writing above codes for me....It's give me exactly what I was looking for....

 

Thanks & Regards...


  • niemtin007 likes this

#12 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 05 April 2017 - 05:51 AM   Best Answer

As seen in the previously given zdrivesOR.cmd, essentially the whole thing fits in a oneliner, here is to make it easier to follow with added the provision by niemtim007 to input a Disk #:

@echo off
setlocal
set mydisk=
set /P mydisk= Disk Number [ ? ] = 
::All Lines
FOR /F  delims^=\^"^ tokens^=5^,11 %%A IN ('WMIC Path Win32_LogicalDiskToPartition Get /Format:csv ') DO ECHO %%A %%B

::Just the drive letter
FOR /F  delims^=\^"^ tokens^=5^,11 %%A IN ('WMIC Path Win32_LogicalDiskToPartition Get /Format:csv ^|FIND "Disk #%mydisk%, Partition #0"') DO ECHO %%B

:duff:

Wonko


  • devdevadev and niemtin007 like this

#13 genetix

genetix

    Frequent Member

  • Advanced user
  • 132 posts
  •  
    Finland

Posted 05 April 2017 - 10:40 AM

 

Maybe this will help you

 

D4mn, that's nice looking. I tested looping thru all I mentioned above and was like 4 loops. Wonder what will that do if the CD/DVD/BD drive is before removable or there's more than 1 removable media.

 

Then again it's still very very nice example how to dig up that info with 1 loop.


  • niemtin007 likes this

#14 devdevadev

devdevadev

    Silver Member

  • Advanced user
  • 540 posts
  •  
    India

Posted 05 April 2017 - 02:18 PM

Finally I have just added following code in my 'SMART_USB.cmd'. Thanks 'Wonko' for providing single line logic for getting driver letter of Partition 1 of any Disk....
@echo off
setlocal
rmpartusb% LIST | find "DRIVE"
set DD=
set /P DD=Please enter your lucky number: 
set /a DD=DD
if "%DD%"=="" goto :end
if "%DD%"=="0" goto :end
set mydisk=%DD%
set USBDRIVELETTER=
FOR /F  delims^=\^"^ tokens^=5^,11 %%A IN ('WMIC Path Win32_LogicalDiskToPartition Get /Format:csv ^|FIND "Disk #%mydisk%, Partition #0"') DO set USBDRIVELETTER=%%B
ECHO %USBDRIVELETTER% & PAUSE

BTW, Is there any simple way to get if connected USB Disk Drive is "Removable" or "Fixed' ?

 

If USB Drive is "Removable", then I will use

FOR /F  delims^=\^"^ tokens^=5^,11 %%A IN ('WMIC Path Win32_LogicalDiskToPartition Get /Format:csv ^|FIND "Disk #%mydisk%, Partition #0"') DO set USBDRIVELETTER=%%B

If USB Drive is "Fixed", then I will use

FOR /F  delims^=\^"^ tokens^=5^,11 %%A IN ('WMIC Path Win32_LogicalDiskToPartition Get /Format:csv ^|FIND "Disk #%mydisk%, Partition #1"') DO set USBDRIVELETTER=%%B

OR

FOR /F  delims^=\^"^ tokens^=5^,11 %%A IN ('WMIC Path Win32_LogicalDiskToPartition Get /Format:csv ^|FIND "Disk #%mydisk%, Partition #2"') DO set USBDRIVELETTER=%%B

How can I do this in above batch file ?

 

Thanks for providing all your support and help....

 

Regards...



#15 genetix

genetix

    Frequent Member

  • Advanced user
  • 132 posts
  •  
    Finland

Posted 05 April 2017 - 09:16 PM

Well, I honestly would say this whole post started a bit wrong as you can't just assume that there is 1 External USB connected media in system and you cannot assume there is no Removable USB connected media in system or how many of both there are. None of examples here gives that information, none of it checks if it's active or not as, if any of above examples would break at "External hard disk media" where USB would of course start with "Removable media ..." where again floppy drives would bug out.

 

Ideally loop should start something similar to this:

setlocal enabledelayexpansion

set /a "_EXTERNAL_COUNT=0"
set /a "_REMOVABLE_COUNT=0"

FOR /F "skip=1" %%a IN ('WMIC DiskDrive get MediaType,DeviceID,Partitions') DO (
IF [%%B]==[External] (
set /a "_EXTERNAL_COUNT+=1"
set "_ExtMedia_!_EXTERNAL_COUNT! = %%%a:~-1%"
rem set "_ExtMedia_!_EXTERNAL_COUNT!_Partitions = %%E"
)
IF [%%B]==[Removable] (
set /a "_REMOVABLE_COUNT+=1"
set "_RemMedia_!_REMOVABLE_COUNT! = %%%a:~-1%"
rem set "_RemMedia_!_REMOVABLE_COUNT!_Partitions = %%F"
)

endlocal

NOTE: That Partitions variable is still wrong.

 

The point there is that basically would count how many External Fixed Drives there is how many Removable, include Drive number they are located and that would need to be matched like @Wonko example looping thru both unless _########_COUNT=0 and those Drives should be matched to WMIC VOLUME Bootable,Caption to verify that, if ex. External Disk #0 Partition #1 -> Drive letter<->Caption/Name is not flagged bootable=true then that partition is not the first partition shown while booted from.

 

Anyway, as I said I did try to write it and took 4 loops total and it still does, if done right, but assuming above examples are enough for project you are working with.



#16 alacran

alacran

    Platinum Member

  • .script developer
  • 2710 posts
  •  
    Mexico

Posted 05 April 2017 - 10:52 PM

@ genetix

 

There is no need to assume anything, he is sure there will be only one USB connected, because he (the user) is in front of the PC and he will connect just one USB with his tools. So there is no need to over complicate the batch my friend.

 

alacran



#17 niemtin007

niemtin007
  • Members
  • 6 posts
  •  
    Vietnam

Posted 05 April 2017 - 11:23 PM

Is there any simple way to get if connected USB Disk Drive is "Removable" or "Fixed' ?

 

 

There is the code I use in my own Multiboot Toolkit, maybe this will help you!?

rem --> check USB disk
wmic diskdrive get Name, Model, InterfaceType, MediaType | find /i "removable Media" | find /i "\\.\PHYSICALDRIVE%disk%" >nul
    if not errorlevel 1 goto :removable
rem --> check HDD box
wmic diskdrive get Name, Model, InterfaceType, MediaType | find /i "External hard disk media" | find /i "\\.\PHYSICALDRIVE%disk%" >nul
    if not errorlevel 1 goto :Fixed

:removable
rem --> put your code here

:Fixed
rem --> put your code here


#18 devdevadev

devdevadev

    Silver Member

  • Advanced user
  • 540 posts
  •  
    India

Posted 05 April 2017 - 11:32 PM

 

Finally I have just added following code in my 'SMART_USB.cmd'. Thanks 'Wonko' for providing single line logic for getting driver letter of Partition 1 of any Disk....
@echo off
setlocal
rmpartusb% LIST | find "DRIVE"
set DD=
set /P DD=Please enter Disk number: 
set /a DD=DD
if "%DD%"=="" goto :end
if "%DD%"=="0" goto :end
set mydisk=%DD%
set USBDRIVELETTER=
FOR /F  delims^=\^"^ tokens^=5^,11 %%A IN ('WMIC Path Win32_LogicalDiskToPartition Get /Format:csv ^|FIND "Disk #%mydisk%, Partition #0"') DO set USBDRIVELETTER=%%B
ECHO %USBDRIVELETTER% & PAUSE

 

I first list ALL External USB Drive (both Removable and Fixed) using 'RMPARTUSB.exe'. And then choose disk number "%DD%" which I want to made 'SMART_USB'.  So here there is no need to assume anything. I am choosing desired USB Drive manually in my batch file...

 

BTW, Thanks for providing your approcach....

 

Regards..



#19 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 06 April 2017 - 08:53 AM

@alacran

Usually writing a batch is 15% writing the batch and the other 150% ;) is finding ways to prevent the user from doing something he/she shouldn't.

 

Of course for "internal" use it is fine :), but, just as an example, validating that the input given is actually a number (and a single one) would prevent a number of possible mistypings:

@echo off
setlocal enableextensions

:loop
SET /p Number=Please type a number 0-9:
::check that they are only digits 
FOR /F  "tokens=1 delims=0123456789" %%A IN ("%Number%") DO ECHO Oh,oh, not a single number&PAUSE&GOTO :loop
::and get only first digit
SET Number=%Number:~0,1%
ECHO OK, number is %Number%

:duff:

Wonko



#20 genetix

genetix

    Frequent Member

  • Advanced user
  • 132 posts
  •  
    Finland

Posted 06 April 2017 - 09:11 AM

@ genetix

 

There is no need to assume anything, he is sure there will be only one USB connected, because he (the user) is in front of the PC and he will connect just one USB with his tools. So there is no need to over complicate the batch my friend.

 

alacran

 

Yeah, I've just for past 5-15 years been mostly building something which is specifically compatible to some "unique scenario". It just has always been a bad idea. Which is why I am so much against creating something that is "just supporting that unique system..." and waste of time, but instead would support all scenarios, so, it can be reused by anyone, if I would of figured to do a bit extra work like between years 2000-2015, I wouldn't be writing the code currently as I am now, but instead I could be creating something new to improve it.





Also tagged with one or more of these keywords: fixed usb drive, windows 10, driver letter

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users