1

在结束这个项目的编码之前,我正在接受最后一个挑战。我正在尝试在不使用临时文件的情况下过滤来自 diskpart 的列表分区和列表卷命令的输出。

我有解决方案:

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

可能有一种方法可以将 for 循环内的 2 行组合为一个,甚至可以将循环内的所有 3 行减少为一行,但我所有的尝试都失败了。

以下是输出的示例:

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

或者对于卷列表:

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

从概念上讲,这似乎是一件足够简单的事情,但是对于批处理的众多语法怪癖来说,几乎没有什么是简单的!

这个问题归结为如何减少 for 循环中的行数。

感谢您提出的任何建议。

4

2 回答 2

1

下一个脚本显示了另一种方法;显示用等号 ( ) 括起来的for /F循环变量,以公平地显示输出,包括。前导和尾随空格:%%?=%%?=diskpart

@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

输出

==> 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=

==>
于 2016-07-03T07:10:21.357 回答
0

这都是逃避的问题。特别是转义 & 运算符的转义字符。

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

输出

___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> ___
于 2022-01-27T06:59:58.227 回答