Getting disk # before running DiskPart and List Disk

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rantic
Posts: 1
Joined: 09 Apr 2013 13:54

Getting disk # before running DiskPart and List Disk

#1 Post by rantic » 09 Apr 2013 14:03

Hi everyone,

I'm trying to learn how to write batch scripts by automating whatever I can (no matter how needless it may be). As it stands I'm trying to automate part of the process in deploying Windows 7 using sysprep.

I am trying to place the steps related to diskpart into a batch file but I won't always know what the disk # is before I run this script. I am looking for a way to show a user the list of drive numbers and then I think I know enough to pipe their input into a text file for disk part to use.

Anyone have any idea or hint on how this can be accomplished?

Thank you,

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Getting disk # before running DiskPart and List Disk

#2 Post by Ed Dyreen » 09 Apr 2013 19:51

Not sure I understand your question, sorry

Code: Select all

DISKPART> list disk

  Schf ###  Status      Grootte  Vrij     Dyn  GPT
  --------  ----------  -------  -------  ---  ---
  Schijf 0    On line      466 GB      0 B
  Schijf 1    On line      373 GB      0 B
  Schijf 2    On line      932 GB      0 B

DISKPART>
This uses vbScript but you can query from cmd too

Code: Select all

Set loDiskDrives = oWMI.ExecQuery (    _
   "SELECT * "                       & _
   "FROM Win32_DiskDrive"              _
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Getting disk # before running DiskPart and List Disk

#3 Post by foxidrive » 09 Apr 2013 20:34

Ed Dyreen wrote:Not sure I understand your question, sorry

Code: Select all

DISKPART> list disk

  Schf ###  Status      Grootte  Vrij     Dyn  GPT
  --------  ----------  -------  -------  ---  ---
  Schijf 0    On line      466 GB      0 B
  Schijf 1    On line      373 GB      0 B
  Schijf 2    On line      932 GB      0 B

DISKPART>


I think the OP wants to present the list to the user and have them select the drive number.

I tried to redirect the diskpart output in Windows 8 with a diskpart script but diskpart popped up in a separate window and the output didn't go into the text file.

This demonstrates what didn't work in Windows 8.


Code: Select all

@echo off
> script.tmp echo list disk
>> script.tmp echo exit

diskpart /s "%cd%\script.tmp" >"list.txt"
del script.tmp

:: use timeout if you are using another diskpart command.
:: timeout /t 15

type list.txt
set /p "disk=Enter the disk number: "
set disk
pause

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Getting disk # before running DiskPart and List Disk

#4 Post by Ed Dyreen » 09 Apr 2013 20:47

foxidrive wrote:I tried to redirect the diskpart output in Windows 8 with a diskpart script but diskpart popped up in a separate window and the output didn't go into the text file.
I see, I have no win8 to test. Maybe a for construct ?

Code: Select all

> "file" ( echo.list disk )
for /f "skip=6 tokens=2,7,8" %%a in (

   'diskPart.EXE /s "file"'

) do   ...
Maybe opt for WMIC !alias! >file

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Getting disk # before running DiskPart and List Disk

#5 Post by foxidrive » 09 Apr 2013 21:04

Ed Dyreen wrote:I see, I have no win8 to test. Maybe a for construct ?

Code: Select all

> "file" ( echo.list disk )
for /f "skip=6 tokens=2,7,8" %%a in (

   'diskPart.EXE /s "file"'

) do   ...


It does the same thing. For some reason diskpart also changes the working directory so %cd% is needed too.

This opens diskpart and lists the info to the screen, but then diskpart closes along with the info. The for loop echos nothing.

Code: Select all

@echo off
> "file" ( echo.list disk )
for /f "delims=" %%a in (

   'diskPart.EXE /s "%cd%\file"'

) do   echo %%a
pause

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Getting disk # before running DiskPart and List Disk

#6 Post by Ed Dyreen » 09 Apr 2013 21:22

foxidrive wrote:It does the same thing.
lol, let's just say windows is very consistent.

The OP is using win7 so maybe it works maybe not.
Forgot about that %cd% that was introduced since winVista.
WMIC will be slower because it takes a while to get a connection, but it should work.

Now I get back into my cave ( still struggling with win3.1 :D )

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Getting disk # before running DiskPart and List Disk

#7 Post by aGerman » 10 Apr 2013 10:55

Piping directly to DISKPART works for me under Win7.

Code: Select all

(echo list volume&echo exit)|diskpart

Perhaps an additional FINDSTR (even if it's language dependent that way).

Code: Select all

(echo list volume&echo exit)|diskpart|findstr /i "\<volume\> ---"

Regards
aGerman

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Getting disk # before running DiskPart and List Disk

#8 Post by Squashman » 10 Apr 2013 16:16

aGerman wrote:Piping directly to DISKPART works for me under Win7.

Code: Select all

(echo list volume&echo exit)|diskpart

Perhaps an additional FINDSTR (even if it's language dependent that way).

Code: Select all

(echo list volume&echo exit)|diskpart|findstr /i "\<volume\> ---"

Regards
aGerman

That works if I run it from an elevated cmd prompt.
A normal cmd window will spawn a new cmd instance and give you a UAC prompt.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Getting disk # before running DiskPart and List Disk

#9 Post by foxidrive » 10 Apr 2013 20:09

aGerman wrote:Piping directly to DISKPART works for me under Win7.

Code: Select all

(echo list volume&echo exit)|diskpart



In Windows 8 this opens a diskpart.exe session in a new window and you see the DISKPART > prompt. Nothing is acted upon.

Diskpart apparently doesn't output text through stdout or stderr, and just writes to it's own window.

As Squashman says, it generates a UAC prompt too, unless you have UAC turned off.

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Getting disk # before running DiskPart and List Disk

#10 Post by aGerman » 11 Apr 2013 07:44

Of course I started the batch file via right click -> Run as admin. I thuoght that would be OK since rantic wrote that he needs DISKPART also for further action (where admin rights are probably required as well). Maybe I misunderstood.

Regards
aGerman

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Getting disk # before running DiskPart and List Disk

#11 Post by foxidrive » 11 Apr 2013 07:56

I was just confirming that there's a UAC prompt. The OP will need to handle that however they like.

My code earlier and the other suggestions will, or should work in Windows 7 however.

Post Reply