Getting disk # before running DiskPart and List Disk
Moderator: DosItHelp
Getting disk # before running DiskPart and List Disk
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,
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,
Re: Getting disk # before running DiskPart and List Disk
Not sure I understand your question, sorry
This uses vbScript but you can query from cmd too
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>
Code: Select all
Set loDiskDrives = oWMI.ExecQuery ( _
"SELECT * " & _
"FROM Win32_DiskDrive" _
)
Re: Getting disk # before running DiskPart and List Disk
Ed Dyreen wrote:Not sure I understand your question, sorryCode: 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
Re: Getting disk # before running DiskPart and List Disk
I see, I have no win8 to test. Maybe a for construct ?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.
Code: Select all
> "file" ( echo.list disk )
for /f "skip=6 tokens=2,7,8" %%a in (
'diskPart.EXE /s "file"'
) do ...
Re: Getting disk # before running DiskPart and List Disk
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
Re: Getting disk # before running DiskPart and List Disk
lol, let's just say windows is very consistent.foxidrive wrote:It does the same thing.
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

Re: Getting disk # before running DiskPart and List Disk
Piping directly to DISKPART works for me under Win7.
Perhaps an additional FINDSTR (even if it's language dependent that way).
Regards
aGerman
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
Re: Getting disk # before running DiskPart and List Disk
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.
Re: Getting disk # before running DiskPart and List Disk
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.
Re: Getting disk # before running DiskPart and List Disk
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
Regards
aGerman
Re: Getting disk # before running DiskPart and List Disk
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.
My code earlier and the other suggestions will, or should work in Windows 7 however.