在完成这个项目的编码之前,我正处于我的最后一个挑战。我试图过滤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循环中的行。
感谢您提出的任何建议。
答案 0 :(得分: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=
==>