Jump to content











Photo
- - - - -

List only physical drive in diskpart


  • Please log in to reply
26 replies to this topic

#1 guimenez

guimenez

    Frequent Member

  • Advanced user
  • 172 posts
  •  
    Portugal

Posted 29 December 2016 - 08:58 AM

I've an automated script to create partitions in diskpart.

The main problem it's that sometimes the WinPE doesn't show the physical disk (sometimes is damage) and when the script run it clear the disk 0, and in that case is the USB disk and i lose everything.

Is there any way to use diskpart only with physical disks?

Thanks



#2 Guest_AnonVendetta_*

Guest_AnonVendetta_*
  • Guests

Posted 29 December 2016 - 03:55 PM

The command is called 'list disk' (without quotes), to be typed within diskpart. Likewise for 'list partition', 'list volume', etc.



#3 guimenez

guimenez

    Frequent Member

  • Advanced user
  • 172 posts
  •  
    Portugal

Posted 29 December 2016 - 04:25 PM

The command is called 'list disk' (without quotes), to be typed within diskpart. Likewise for 'list partition', 'list volume', etc.

Thanks for replying.

that's not my problem.

when i make list disk it shows HDD and USB disks.

and if i don't have disk, my USB will be disk 0 and it will clear my WinPE from USB.

My script always clear the disk0. I need to use diskpart without showing USB disks so i can't acidentally erase my USB.

thanks



#4 Guest_AnonVendetta_*

Guest_AnonVendetta_*
  • Guests

Posted 29 December 2016 - 05:14 PM

Then I have no idea. You could try booting your PE or whatever into a ram disk, then unplug the drive when it's fully loaded. Then run your script. It can't delete what it can't find.



#5 guimenez

guimenez

    Frequent Member

  • Advanced user
  • 172 posts
  •  
    Portugal

Posted 29 December 2016 - 06:05 PM

Then I have no idea. You could try booting your PE or whatever into a ram disk, then unplug the drive when it's fully loaded. Then run your script. It can't delete what it can't find.

It's a nice idea, but i'm restoring WIM files from the USB so i will need the USB drive.

But thanks anyway :D



#6 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 29 December 2016 - 06:43 PM

Guimenez,

you asked two different things:

#1 How can I list disk devices in diskpart excluding USB ones?

#2 How can I list devices in diskpart excluding a specific USB one (that contains the PE)?

 

Both are "wrong" questions, in the sense that your problem is actually that of avoiding re-partitoning/re-formatting the "wrong" device (the USB one that contains the PE).

 

So more generally what you actually need/want is to know on which disk a given volume (with a known size, label and filesystem) is and avoid that disk.

So you want something *like* this:
http://reboot.pro/to...er-under-winpe/

more specifically something *like*:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
::Get disk number from label

IF "%1."=="." ECHO Missing or Wrong Parameter&GOTO :EOF

SET MyDisk=
SET Mylabel=%1
FOR /F "delims=" %%A IN ('Echo List Volume^|Diskpart^|FIND /I "%Mylabel%"') DO SET Myvol=%%A
IF NOT DEFINED Myvol ECHO ERROR: NO LABEL FOUND&GOTO :EOF
FOR /F "tokens=1 delims=0123456789" %%A IN ("%Myvol%") DO SET /A Myvol=!Myvol:%%A=! 2>nul

FOR /F "delims=" %%A IN ('
^(
ECHO Sel Vol %MyVol%^&ECHO det vol
^)^|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 Volume with label %Mylabel% is on DISK %MyDisk% and it is Volume #%Myvol%

:duff:

Wonko


  • guimenez likes this

#7 guimenez

guimenez

    Frequent Member

  • Advanced user
  • 172 posts
  •  
    Portugal

Posted 29 December 2016 - 07:04 PM

Guimenez,

you asked two different things:

#1 How can I list disk devices in diskpart excluding USB ones?

#2 How can I list devices in diskpart excluding a specific USB one (that contains the PE)?

 

Both are "wrong" questions, in the sense that your problem is actually that of avoiding re-partitoning/re-formatting the "wrong" device (the USB one that contains the PE).

 

So more generally what you actually need/want is to know on which disk a given volume (with a known size, label and filesystem) is and avoid that disk.

So you want something *like* this:
http://reboot.pro/to...er-under-winpe/

more specifically something *like*:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
::Get disk number from label

IF "%1."=="." ECHO Missing or Wrong Parameter&GOTO :EOF

SET MyDisk=
SET Mylabel=%1
FOR /F "delims=" %%A IN ('Echo List Volume^|Diskpart^|FIND /I "%Mylabel%"') DO SET Myvol=%%A
IF NOT DEFINED Myvol ECHO ERROR: NO LABEL FOUND&GOTO :EOF
FOR /F "tokens=1 delims=0123456789" %%A IN ("%Myvol%") DO SET /A Myvol=!Myvol:%%A=! 2>nul

FOR /F "delims=" %%A IN ('
^(
ECHO Sel Vol %MyVol%^&ECHO det vol
^)^|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 Volume with label %Mylabel% is on DISK %MyDisk% and it is Volume #%Myvol%

:duff:

Wonko

Thank you :D

 

I'm trying to run the batch bat it always says: "Missing or Wrong Parameter".

 

i will need to start my diskpart script only after this batch?

 

Thanks



#8 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 29 December 2016 - 07:17 PM

Well, you need to:

1) have a label applied to the (at least one) volume on your USB device (where the PE is)

2) pass that label to the batch as parameter (use a label with NO spaces in it)

 

Once you have tested the batch as working you can use the code and put it in your batch, adapting it to your batch, the above is just an example of a self-standing batch that given a volume label provides info on which disk  # it is (and which volume # it is).

 

:duff:

Wonko


  • guimenez likes this

#9 guimenez

guimenez

    Frequent Member

  • Advanced user
  • 172 posts
  •  
    Portugal

Posted 29 December 2016 - 07:24 PM

Well, you need to:

1) have a label applied to the (at least one) volume on your USB device (where the PE is)

2) pass that label to the batch as parameter (use a label with NO spaces in it)

 

Once you have tested the batch as working you can use the code and put it in your batch, adapting it to your batch, the above is just an example of a self-standing batch that given a volume label provides info on which disk  # it is (and which volume # it is).

 

:duff:

Wonko

My pen is called USB.

When i run script USB it only open diskpart, and then when i go out it says "No label found".



#10 cdob

cdob

    Gold Member

  • Expert
  • 1469 posts

Posted 29 December 2016 - 07:42 PM

when the script run it clear the disk 0, and in that case is the USB disk and i lose everything.

Do you boot the WinPE from USB disk always?

Idea: verify: Disk 0 is not a USB disk
(echo sel disk 0 & echo det disk ) | diskpart.exe | find.exe """USB""" || echo no USB disk

  • guimenez likes this

#11 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 29 December 2016 - 08:23 PM

Open diskpart.

 

Type in it "list volume" (without double quotes) and press ENTER.

 

Is "USB" listed? (hint: you could try applying to the volume on the stick a more distinctive label, like "MY_USB", to see if it makes a difference)

 

Now exit diskpart and on command line issue:
echo list volume | diskpart | FIND /I "USB"

 

If it is listed, than the batch (and of course cdob's test command line above) should work fine.

 

:duff:

Wonko


  • guimenez likes this

#12 guimenez

guimenez

    Frequent Member

  • Advanced user
  • 172 posts
  •  
    Portugal

Posted 30 December 2016 - 09:09 AM

Do you boot the WinPE from USB disk always?

Idea: verify: Disk 0 is not a USB disk

(echo sel disk 0 & echo det disk ) | diskpart.exe | find.exe """USB""" || echo no USB disk

 

My USB have WinPE and i always boot from USB to install Windows using "WinNTSetup" to restore WIMs.

I've created a batch that clear the disk 0 and create partitions to restore the images.

When i boot one PC that doesn't have HDD or is damage, it clear my USB :(

 

I've renamed my USB to MY_USB and running this command it always says "No USB disk" even if i select the disk 2 (MY_USB).

I've attached printscreens.

 

 

 

Open diskpart.

 

Type in it "list volume" (without double quotes) and press ENTER.

 

Is "USB" listed? (hint: you could try applying to the volume on the stick a more distinctive label, like "MY_USB", to see if it makes a difference)

 

Now exit diskpart and on command line issue:
echo list volume | diskpart | FIND /I "USB"

 

If it is listed, than the batch (and of course cdob's test command line above) should work fine.

 

:duff:

Wonko

 

 

Running this command it works fine and detect my USB.

 

I think i can use this one?

 

What i need to change to after identify the USB it will compare if it's disk 0 and if so it will close.

 

thank you for such a great help :D

Attached Files



#13 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 30 December 2016 - 10:45 AM

I don't get it. :(

 

Let's say that you saved the batch as "findlabel.cmd".

Are you passing to it the parameter? :unsure:

 

What happens if you open a command prompt and run in it:
findlabel.cmd MY_USB

:dubbio:

 

The command in the batch is exactly the same as echo list volume | diskpart | FIND /I "USB", so if this one works, so should the batch.

 

Try putting a couple of PAUSE and ECHO statements to understand if it works, *like*:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
::Get disk number from label


IF "%1."=="." ECHO Missing or Wrong Parameter&GOTO :EOF

SET MyDisk=
SET Mylabel=%1
FOR /F "delims=" %%A IN ('Echo List Volume^|Diskpart^|FIND /I "%Mylabel%"') DO SET Myvol=%%A
SET My
PAUSE
IF NOT DEFINED Myvol ECHO ERROR: NO LABEL FOUND&GOTO :EOF
FOR /F "tokens=1 delims=0123456789" %%A IN ("%Myvol%") DO SET /A Myvol=!Myvol:%%A=! 2>nul
SET My
PAUSE
FOR /F "delims=" %%A IN ('
^(
ECHO Sel Vol %MyVol%^&ECHO det vol
^)^|diskpart^|FIND "*" 
') DO SET Mydisk=%%A
SET My
PAUSE

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
SET My
PAUSE

ECHO Volume with label %Mylabel% is on DISK %MyDisk% and it is Volume #%Myvol%

Post your script, so that we can understand what it does and how it can be modified to exclude a disk.

 

:duff:

Wonko



#14 guimenez

guimenez

    Frequent Member

  • Advanced user
  • 172 posts
  •  
    Portugal

Posted 30 December 2016 - 11:28 AM

I don't get it. :(

 

Let's say that you saved the batch as "findlabel.cmd".

Are you passing to it the parameter? :unsure:

 

What happens if you open a command prompt and run in it:
findlabel.cmd MY_USB

:dubbio:

 

The command in the batch is exactly the same as echo list volume | diskpart | FIND /I "USB", so if this one works, so should the batch.

 

Try putting a couple of PAUSE and ECHO statements to understand if it works, *like*:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
::Get disk number from label


IF "%1."=="." ECHO Missing or Wrong Parameter&GOTO :EOF

SET MyDisk=
SET Mylabel=%1
FOR /F "delims=" %%A IN ('Echo List Volume^|Diskpart^|FIND /I "%Mylabel%"') DO SET Myvol=%%A
SET My
PAUSE
IF NOT DEFINED Myvol ECHO ERROR: NO LABEL FOUND&GOTO :EOF
FOR /F "tokens=1 delims=0123456789" %%A IN ("%Myvol%") DO SET /A Myvol=!Myvol:%%A=! 2>nul
SET My
PAUSE
FOR /F "delims=" %%A IN ('
^(
ECHO Sel Vol %MyVol%^&ECHO det vol
^)^|diskpart^|FIND "*" 
') DO SET Mydisk=%%A
SET My
PAUSE

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
SET My
PAUSE

ECHO Volume with label %Mylabel% is on DISK %MyDisk% and it is Volume #%Myvol%

Post your script, so that we can understand what it does and how it can be modified to exclude a disk.

 

:duff:

Wonko

Here you you have my script.

Thanks

Attached Files



#15 guimenez

guimenez

    Frequent Member

  • Advanced user
  • 172 posts
  •  
    Portugal

Posted 30 December 2016 - 12:14 PM

the batch above show me perfectly well that my USB is on disk 2 :D

What can i do to modify the batch so if it show my USB on disk 0 it stops?

Thanks



#16 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 30 December 2016 - 12:17 PM

Anyway, instead of using an external diskpart script, *like*:

...

diskpart /s listardiscos.txt

...
diskpart /s CSMPartOnlyCnoHidden.txt

....

diskpart /s CSMpartOnlyC.txt

 

you can integrate them in the batch and then use the disk number that you get from one of the methods posted and skip the:

 

 

echo.
echo *******************************************************************
echo * AVISO - AVISO - AVISO *
echo * Apenas formate automaticamente se o disco for detetado como "0" *
echo *******************************************************************
echo.
pause
cls

Woudln't it be "detectado"?

 

As a side-side note, it is not a really good idea to use "pause" to provide an option to skip a potentially destructive command, it is much safer and recommended to ask for something typed, like "Do you really want to format disk 0? Type YES to confirm." as pause can be bypassed accidentally (think of a stuck key on the keyboard or someone accidentally putting something on the desk and hitting the kwyboard or a cat, etc.)

 

IF "%MyDisk%"=="0" ECHO No good, exiting&PAUSE&GOTO :EOF

 

:duff:

Wonko


  • guimenez likes this

#17 cdob

cdob

    Gold Member

  • Expert
  • 1469 posts

Posted 30 December 2016 - 12:23 PM

What can i do to modify the batch so if it show my USB on disk 0 it stops?


As for testing:
(echo sel disk 0 & echo det disk ) | diskpart.exe | find.exe """USB""" && (echo USB disk found &goto :eof)
.
The search string is short still and should enhanced.
Which localized windows do you use?

Can you run diskpart
disk 0
detail disk

disk 1
detail disk

Disk 0 and 1 at a internal and a USB disk.

#18 guimenez

guimenez

    Frequent Member

  • Advanced user
  • 172 posts
  •  
    Portugal

Posted 30 December 2016 - 12:46 PM

As for testing:

(echo sel disk 0 & echo det disk ) | diskpart.exe | find.exe """USB""" && (echo USB disk found &goto :eof)
.
The search string is short still and should enhanced.
Which localized windows do you use?

Can you run diskpart
disk 0
detail disk

disk 1
detail disk

Disk 0 and 1 at a internal and a USB disk.

 

 

I'm using English localized Windows.

My language is portuguese(Portugal) and now the word "detetado" shouldn't be writed with the "c" before the "t" :D

 

After having this script working i will remove the warning and the pause thing.

 

Running this script (only for testing and my USB it's disk 2 for now) still doesn't do anything, even with disk 0 or 1.

(echo sel disk 2 & echo det disk  )  | diskpart.exe   | find.exe """MY_USB"""   && (echo USB disk found   & goto :eof )
 
 
I've attached the details from diskpart

Attached Files



#19 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 30 December 2016 - 02:01 PM

I thought it was the same as Spanish, but now I see that both detetado and detectado are correct in Portuguese, aren't they?

 

Try going in steps:



@ECHO OFF
(echo sel disk 2 & echo det disk ) | diskpart.exe | find.exe "MY_USB"
PAUSE
(echo sel disk 2 & echo det disk ) | diskpart.exe | find.exe "MY_USB" && SET Mydisk=2
SET My
PAUSE
IF "%MyDisk%"=="2" ECHO No good, exiting&PAUSE&GOTO :EOF

:duff:

Wonko


  • guimenez likes this

#20 guimenez

guimenez

    Frequent Member

  • Advanced user
  • 172 posts
  •  
    Portugal

Posted 30 December 2016 - 04:14 PM

I thought it was the same as Spanish, but now I see that both detetado and detectado are correct in Portuguese, aren't they?

 

Try going in steps:



@ECHO OFF
(echo sel disk 2 & echo det disk ) | diskpart.exe | find.exe "MY_USB"
PAUSE
(echo sel disk 2 & echo det disk ) | diskpart.exe | find.exe "MY_USB" && SET Mydisk=2
SET My
PAUSE
IF "%MyDisk%"=="2" ECHO No good, exiting&PAUSE&GOTO :EOF

:duff:

Wonko

Amazing Wonko,

i'ts just working perfectly.

Now i will modify my batch (and use disk 0) and it will never erase the USB again  :)

Once again thank you.

Have a great happy new year.

 

PS: You can write both words, but with the new portuguese deal, all the characters that you don't spell, you don't write. ex: electricidade(you don't speel the "c") so you write eletricidade. 



#21 cdob

cdob

    Gold Member

  • Expert
  • 1469 posts

Posted 30 December 2016 - 04:44 PM

I'm using English localized Windows.

I've attached the details from diskpart


Kingston DT 101 G2 USB Device
Disk ID: 8DFB7166
Type : USB

General UDisk USB Device
Disk ID: 00000001
Type : USB

There is the Type USB. This matches all USB drives.
You may search for the Type USB: this matches all USB drives. The label dosn't matter.

Or you may search for a specific label:
This matches prepared USB drives. A new drive may be deleted still.

#22 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 30 December 2016 - 05:20 PM

Once again thank you.

 

Actually most if not all the thanks should go to cdob that greatly simplified everything with his smart approach.

 

@cdob

Yes :), that is why originally suggested to change "USB" to "MY_USB" for the tests, with your approach (only checking the boot device/disk) there should be no issues, though I wonder if disk order is *always* correct, i.e. if disk 0 is *always* the boot drive (thinking of grub4dos drive exchange in some setups and/or RAM booting or even "queer" behaviour of BIOS/UEFI/whatever) :unsure:

 

:duff:

Wonko



#23 cdob

cdob

    Gold Member

  • Expert
  • 1469 posts

Posted 30 December 2016 - 06:49 PM

though I wonder if disk order is *always* correct, i.e. if disk 0 is *always* the boot drive

No, disk 0 is not always the boot drive.
E.g. the BIOS dosn't know a disk, dosn't support booting from this device. Grub4dos mapping a disk, ...
I would not rely at fixed numbers.
There is Type : USB: it's not the target disk
There is Type : IDE and Type : SATA. Could be a target disk

Booting a PE (e.g. boot at CSM from USB, next boot from internal UEFI):
there is no way to detect the boot device at next boot.
BIOS / UEFI may remap the drives.

Given one internal hard disk at running PE only: should be the target disk.
Assuption is wrong, if the PE is missing drivers for another mass storage controller.

#24 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 30 December 2016 - 07:26 PM

Good :), I remembered that there were some "queer" disk mappings, 

 

Though in this case it is OK, I mean if the partition/format commands are "hardcoded" to "disk 0" and the "check" prevents the partition/format to take place if the USB stick is disk 0, the only risk maybe that of formatting another disk in case of multi-disk system, but I don't think (unless some BIOS/UEFI mess with mixed MBR/GPT styles, which I never read about) that internal disk can be exchanged in case of "inserting" a USB device (unless they are exhanged in firmware intentionally by the user).

 

I mean, normal operation:

Disk 0 is first and boot disk

Disk 1 is second disk

 

Usb booting of a PE (via F12 or similar):

Disk 0 remains first disk <- the detection routine allows the partitioning/formatting

Disk 1 remains second disk

USB device is boot disk

 

Usb booting of a PE (changing boot order in BIOS or UEFI) or for whatever reason:

Usb becomes Disk 0 <- the detection routine prevents the accidental partitioning/formatting

Disk 0 is now Disk 1

Disk 1 is now Disk 2

 

There is not a case:

Disk 0 is now Disk 1

Disk 1 is now Disk 0 <- the detection routine allows the accidental partitioning/formatting of the "wrong" disk (though still not the USB one)

USB device is boot disk

right?

 

:duff:

Wonko



#25 nguyentu

nguyentu

    Frequent Member

  • Advanced user
  • 102 posts
  •  
    Vietnam

Posted 31 December 2016 - 09:31 AM

Disk 0 is not always a boot disk. In all my cases, disk 0 is always hard drive although I boot into WinPE via USB.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users