为什么 FOR LOOP 只输出 echo is on in txt 文件?
Why FOR LOOP only output Echo is on in txt file?
我的cmd批处理文件总是输出:
ECHO is on.
ECHO is on.
ECHO is on.
脚本:
SET "InputFile=abc.txt"
IF EXIST "%InputFile%" (DEL /f /q "%InputFile%")
SETLOCAL EnableExtensions EnableDelayedExpansion
FOR /F Tokens^=* %%G IN ('
"WMIC PATH Win32_DiskPartition WHERE ^(DeviceID^="Disk #0, Partition #1"^) Assoc:list /AssocClass:Win32_LogicalDiskToPartition 2>NUL"
') DO (CALL ECHO %%G>> "%InputFile%")
ENDLOCAL
如何解决?
1. 如果包含逗号,WHERE
clause in WQL 将失败。使用
"… WHERE ^(DeviceID^ like^ "Disk #0_ Partition #1"^) …
2. 有一个已知的 wmic
错误,请参阅 Dave Benham's article WMIC
and FOR /F
: A fix for the trailing <CR>
problem。修复它,例如如下:
@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
SET "InputFile=abc56715419.txt"
IF EXIST "%InputFile%" (DEL /f /q "%InputFile%")
REM SETLOCAL EnableExtensions EnableDelayedExpansion
(
FOR /F Tokens^=* %%G IN ('
"WMIC PATH Win32_DiskPartition WHERE ^(DeviceID^ like^ "Disk #0_ Partition #1"^) Assoc:list /AssocClass:Win32_LogicalDiskToPartition 2>NUL"
') DO ( for /F "delims=" %%g in ("%%G") do (CALL ECHO(%%g))
)>>"%InputFile%"
REM debugging output
type "%InputFile%"
ENDLOCAL
这里 for
循环是
%%G
检索 WMIC
输出;
%%g
删除值 return 结尾的 carriage returned: wmic
行为:每个输出行以 0x0D0D0A
(<CR><CR><LF>
) 而不是常见的 0x0D0A
(<CR><LF>
). 结尾
您的问题只是 DeviceID
字符串包含逗号,使用变量保存它可以轻松解决。为了不显示 return Echo
状态消息,您需要在 Echo
之后添加一个非 space 字符,最好的字符是 (
,=
和 /
.
试试这个!
@Echo Off
Set "InputFile=abc.txt"
Set "SearchStr=Disk #0, Partition #1"
If Exist "%InputFile%" Del /F "%InputFile%"
For /F Tokens^=* %%A In ('
"WMIC Partition Where (DeviceID="%%SearchStr%%") Assoc:List /AssocClass:Win32_LogicalDiskToPartition 2>NUL"
')Do >>"%InputFile%" Echo(%%A
如果您不想在文件输出中出现空行,请使用另一个嵌套的 For
循环:
@Echo Off
Set "InputFile=abc.txt"
Set "SearchStr=Disk #0, Partition #1"
If Exist "%InputFile%" Del /F "%InputFile%"
For /F Tokens^=* %%A In ('
"WMIC Partition Where (DeviceID="%%SearchStr%%") Assoc:List /AssocClass:Win32_LogicalDiskToPartition 2>NUL"
')Do For /F Tokens^=* %%B In ("%%A")Do >>"%InputFile%" Echo(%%B
然而,当您输出所有内容时,根本不需要 For
循环:
@Set "InputFile=abc.txt"
@Set "SearchStr=Disk #0, Partition #1"
@If Exist "%InputFile%" Del /F "%InputFile%"
@WMIC /Output:"%InputFile%" Partition Where (DeviceID="%SearchStr%") Assoc:List /AssocClass:Win32_LogicalDiskToPartition 2>NUL
[编辑/]
由于这是看似都基于同一任务的众多问题之一,如果您只是想确定与 Disk #0, Partition #1
关联的驱动器号,您可以类似地进行操作:
@Set "SearchStr=Disk #0, Partition #1"
@For /F Skip^=2Tokens^=2Delims^=^" %%A In ('"WMIC Partition Where (DeviceID="%%SearchStr%%") Assoc /AssocClass:Win32_LogicalDiskToPartition 2>NUL"')Do @Echo %SearchStr% = %%A
我的cmd批处理文件总是输出:
ECHO is on. ECHO is on. ECHO is on.
脚本:
SET "InputFile=abc.txt"
IF EXIST "%InputFile%" (DEL /f /q "%InputFile%")
SETLOCAL EnableExtensions EnableDelayedExpansion
FOR /F Tokens^=* %%G IN ('
"WMIC PATH Win32_DiskPartition WHERE ^(DeviceID^="Disk #0, Partition #1"^) Assoc:list /AssocClass:Win32_LogicalDiskToPartition 2>NUL"
') DO (CALL ECHO %%G>> "%InputFile%")
ENDLOCAL
如何解决?
1. 如果包含逗号,WHERE
clause in WQL 将失败。使用
"… WHERE ^(DeviceID^ like^ "Disk #0_ Partition #1"^) …
2. 有一个已知的 wmic
错误,请参阅 Dave Benham's article WMIC
and FOR /F
: A fix for the trailing <CR>
problem。修复它,例如如下:
@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
SET "InputFile=abc56715419.txt"
IF EXIST "%InputFile%" (DEL /f /q "%InputFile%")
REM SETLOCAL EnableExtensions EnableDelayedExpansion
(
FOR /F Tokens^=* %%G IN ('
"WMIC PATH Win32_DiskPartition WHERE ^(DeviceID^ like^ "Disk #0_ Partition #1"^) Assoc:list /AssocClass:Win32_LogicalDiskToPartition 2>NUL"
') DO ( for /F "delims=" %%g in ("%%G") do (CALL ECHO(%%g))
)>>"%InputFile%"
REM debugging output
type "%InputFile%"
ENDLOCAL
这里 for
循环是
%%G
检索WMIC
输出;%%g
删除值 return 结尾的 carriage returned:wmic
行为:每个输出行以0x0D0D0A
(<CR><CR><LF>
) 而不是常见的0x0D0A
(<CR><LF>
). 结尾
您的问题只是 DeviceID
字符串包含逗号,使用变量保存它可以轻松解决。为了不显示 return Echo
状态消息,您需要在 Echo
之后添加一个非 space 字符,最好的字符是 (
,=
和 /
.
试试这个!
@Echo Off
Set "InputFile=abc.txt"
Set "SearchStr=Disk #0, Partition #1"
If Exist "%InputFile%" Del /F "%InputFile%"
For /F Tokens^=* %%A In ('
"WMIC Partition Where (DeviceID="%%SearchStr%%") Assoc:List /AssocClass:Win32_LogicalDiskToPartition 2>NUL"
')Do >>"%InputFile%" Echo(%%A
如果您不想在文件输出中出现空行,请使用另一个嵌套的 For
循环:
@Echo Off
Set "InputFile=abc.txt"
Set "SearchStr=Disk #0, Partition #1"
If Exist "%InputFile%" Del /F "%InputFile%"
For /F Tokens^=* %%A In ('
"WMIC Partition Where (DeviceID="%%SearchStr%%") Assoc:List /AssocClass:Win32_LogicalDiskToPartition 2>NUL"
')Do For /F Tokens^=* %%B In ("%%A")Do >>"%InputFile%" Echo(%%B
然而,当您输出所有内容时,根本不需要 For
循环:
@Set "InputFile=abc.txt"
@Set "SearchStr=Disk #0, Partition #1"
@If Exist "%InputFile%" Del /F "%InputFile%"
@WMIC /Output:"%InputFile%" Partition Where (DeviceID="%SearchStr%") Assoc:List /AssocClass:Win32_LogicalDiskToPartition 2>NUL
[编辑/]
由于这是看似都基于同一任务的众多问题之一,如果您只是想确定与 Disk #0, Partition #1
关联的驱动器号,您可以类似地进行操作:
@Set "SearchStr=Disk #0, Partition #1"
@For /F Skip^=2Tokens^=2Delims^=^" %%A In ('"WMIC Partition Where (DeviceID="%%SearchStr%%") Assoc /AssocClass:Win32_LogicalDiskToPartition 2>NUL"')Do @Echo %SearchStr% = %%A