繁体   English   中英

在Windows Batch for Loop中使用带引号和变量的引号不可靠的结果

[英]Unreliable results using quotes with variables in windows batch for loop

我正在运行一个for循环来浏览要停止的服务列表,但是我在引号和相当不正常的结果上遇到了麻烦。

for /f "tokens=* delims= " %%a in ('wmic service where ^(displayname like "%%tsm%%"^) get name ^| findstr "TSM"') do echo "%%a"

我的结果是这样的:

"Service Name
"Service Name

如果我切换到:

for /f "tokens=* delims= " %%a in ('wmic service where ^(displayname like "%%tsm%%"^) get name ^| findstr "TSM"') do echo ""%%a""

结果是:

""Service Name
""Service Name

而且,如果我切换到:

for /f "tokens=* delims= " %%a in ('wmic service where ^(displayname like "%%tsm%%"^) get name ^| findstr "TSM"') do echo ^"%%a^"

结果:

"Service Name
"Service Name

显然发生了一些我不理解的事情,因为我根本无法获取它来将结果封装在引号中,因此我可以运行诸如net stop或sc delete之类的命令。

尝试这个:

for /f "tokens=* delims= " %%a in ('wmic service where ^(displayname like "%%tsm%%"^) get name ^| findstr "TSM"') do (
for /f "delims=" %%i in ("%%a") do echo "%%i"
)

这是由于wmic( CRLFCR )的“行尾”符号引起的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM