我的cmd批处理文件始终输出:
#!/usr/bin/env tclsh package require Thread thread::create { for {set i 0} {$i < 1000} {incr i} { puts "hello T1 $i" } thread::wait } thread::create { for {set j 0} {$j < 1000} {incr j} { puts "hello T2 $j" } thread::wait }
脚本:
ECHO is on.
ECHO is on.
ECHO is on.
如何解决?
答案 0 :(得分:2)
1。。如果WHERE
clause in WQL包含逗号,则会失败。使用
"… WHERE ^(DeviceID^ like^ "Disk #0_ Partition #1"^) …
2。。存在一个已知的wmic
错误,请参见Dave Benham的文章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
,以在返回的值中删除结尾的回车:wmic
行为:每条输出行以0x0D0D0A
(<CR><CR><LF>
结尾而不是常见的0x0D0A
(<CR><LF>
)。答案 1 :(得分:2)
您的问题仅仅是包含逗号的DeviceID
字符串,使用变量将其保留可以轻松解决。为了不返回Echo
状态消息,您需要在Echo
之后添加一个非空格字符,最适合这样做的图表对象是(
,=
和{{1 }}。
尝试一下!
/
如果您不想在文件输出中出现空行,请使用另一个嵌套的@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