1

I'm on my very last challenge before wrapping up this project's coding. I am trying to filter the output from diskpart's list partition and list volume commands without using temporary files.

I have the solution:

set /A hdd= 0
rem Use this for the partition listing:
set cmd="echo select disk %hdd%^&list partition| diskpart"

rem and this for the volume listing (doesn't need the select disk):
set cmd="echo select disk %hdd%^&echo list volume | diskpart"

for /F "skip=7 usebackq delims=" %%? in (`!cmd!`) do (
   set line=%%?
   set cut=!line:~0,8!
   if NOT !cut!==DISKPART echo %%?
)

echo.
pause

There is probably a way to combine the 2 set lines inside the for loop into one, perhaps even reduce all 3 lines inside the loop into a single line, but all my attempts failed.

Here is an example of what the output looks like:

Partition ###  Type              Size     Offset
-------------  ----------------  -------  -------
Partition 1    Primary           1000 MB  1024 KB
Partition 2    Primary             78 GB  1001 MB
Partition 0    Extended            29 GB    79 GB

Or for the volume listing:

Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
----------  ---  -----------  -----  ----------  -------  ---------  ------
Volume 0     B                       DVD-ROM         0 B  No Media
Volume 1     D                       DVD-ROM         0 B  No Media
Volume 2     C   Windows 7 U  NTFS   Partition     78 GB  Healthy    System

Seems like a simple enough thing to do conceptually, but there's very little that's simple about the numerous syntactical quirks of batch!

This question boils down to how to reduce the lines within the for loop.

Thanks for any suggestions you may have.

Mofi
  • 46,139
  • 17
  • 80
  • 143
motech man
  • 51
  • 3

2 Answers2

1

Next script shows another approach; displays for /F loop variable %%? surrounded in equals (=%%?=) to show diskpart output fairly incl. leading and trailing white spaces:

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
set "cmd=echo list disk | diskpart"
call :listCmd
call :listPar "echo list disk | diskpart" 
call :listPar "echo list volume | diskpart" 
call :listPar "(echo select disk 1 & echo list partition) | diskpart" 
ENDLOCAL
goto :eof

:listCmd
echo %~0 %*
for /F "skip=7 usebackq delims=" %%? in (`"%cmd%"`) do (
  if "%%?" NEQ "DISKPART> " echo(=%%?=
)
goto :eof

:listPar
echo %~0 %*
for /F "skip=7 usebackq delims=" %%? in (`"%~1"`) do (
  if "%%?" NEQ "DISKPART> " echo(=%%?=
)
goto :eof

Output

==> D:\bat\SO\38166597.bat
:listCmd
=  Disk ###  Status         Size     Free     Dyn  Gpt=
=  --------  -------------  -------  -------  ---  ---=
=  Disk 0    Online          931 GB      0 B         =
=  Disk 1    Online          111 GB      0 B         =
=  Disk 2    Online          465 GB  1024 KB         =
:listPar "echo list disk | diskpart"
=  Disk ###  Status         Size     Free     Dyn  Gpt=
=  --------  -------------  -------  -------  ---  ---=
=  Disk 0    Online          931 GB      0 B         =
=  Disk 1    Online          111 GB      0 B         =
=  Disk 2    Online          465 GB  1024 KB         =
:listPar "echo list volume | diskpart"
=  Volume ###  Ltr  Label        Fs     Type        Size     Status     Info=
=  ----------  ---  -----------  -----  ----------  -------  ---------  --------=
=  Volume 0     E                       DVD-ROM         0 B  No Media           =
=  Volume 1     D   DataDisk     NTFS   Partition    931 GB  Healthy    Pagefile=
=  Volume 2         Rezervováno  NTFS   Partition    350 MB  Healthy    System  =
=  Volume 3     C                NTFS   Partition    111 GB  Healthy    Boot    =
=  Volume 4     F   GOG          FAT32  Partition    465 GB  Healthy            =
:listPar "(echo select disk 1 & echo list partition) | diskpart"
=Disk 1 is now the selected disk.=
=  Partition ###  Type              Size     Offset=
=  -------------  ----------------  -------  -------=
=  Partition 1    Primary            350 MB  1024 KB=
=  Partition 2    Primary            111 GB   351 MB=

==>
JosefZ
  • 28,460
  • 5
  • 44
  • 83
0

It's all a matter of escaping. Particular escaping the escape character of the & operator.

for /F "skip=6 delims=" %%A in ('^(echo select volume E ^^^& echo list partition ^) ^| diskpart') do echo.___%%A___

Output

___DISKPART> ___
___Volume 2 ist jetzt das gewählte Volume.___
___DISKPART> ___
___  Partition ###  Typ               Größe    Offset___
___  -------------  ----------------  -------  -------___
___  Partition 1    System             500 MB  1024 KB___
___  Partition 2    Reserviert         128 MB   501 MB___
___  Partition 5    Primär               9 GB   629 MB___
___  Partition 3    Primär             394 GB    10 GB___
___* Partition 4    Primär             548 GB   404 GB___
___DISKPART> ___
ooLi
  • 63
  • 6