我在批量比较变量中的字符串与实际字符串时遇到很多麻烦。这是脚本:
set failure=0
for /F "usebackq delims=" %%L in (`"wmic PRINTER where name='10.146.2.52 - HP Color LaserJet CP5225n' GET Name 2>&1"`) do (
::Quotes around variable seem to have no effect. Same result w/ or w/o them.
if /I "%%L"=="No Instance(s) Available." set failure=1
echo %failure% %%L
)
这是输出:
为什么 if 语句失败?这些值明显匹配。请帮忙!!
最佳答案
这里存在三个不同的问题。
1) Wmic 有时会在行尾附加一个额外的 CR
字符,该字符将成为字符串的一部分。
2) 您无法通过 block 内的百分比扩展访问失败
。
3) %%L
试试这个
setlocal EnableDelayedExpansion
set failure=0
for /F "usebackq delims=" %%L in (`"wmic PRINTER where name='10.146.2.52 - HP Color LaserJet CP5225n' GET Name 2>&1"`) do (
set "line=%%L"
set "head=!line:~0,9!"
if "!head!"=="No Instan" set failure=1
echo 123-%%L-456
)
echo %failure%
关于batch-file - 批量比较字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19275130/