@echo off
setlocal enableextensions disabledelayedexpansion
for /f "tokens=2 delims==" %%a in (
'wmic computersystem get model /value'
) do for /f "delims=" %%b in ("%%~a") do for %%m in (
"model1" "model2" "model3" "model4"
) do if /i "%%~b"=="%%~m" (
set "model=%%~m"
goto start
)
echo un-compatible system
goto :eof
:start
echo Start of script for model [%model%]
for 循环在哪里
-
%%a 检索模型
-
%%b 删除返回值中的结束回车符(wmic 行为)
-
%%m 遍历允许的模型列表
如果任何允许的模型与使用wmic 检索到的模型匹配,则代码跳转到开始标签,否则,内部for 循环结束,并且找不到匹配项,脚本结束。
这可以简化为
>nul (wmic computersystem get model |findstr /i /l /c:"model1" /c:"model2" /c:"model3")||(
echo un-compatible system
goto :eof
)
echo compatible system
其中条件执行操作用于判断findstr命令是否找不到任何模型并取消执行。
当然,你可以使用if /else的级联,但语法有点不同
for /f "tokens=2 delims==" %%a in (
'wmic computersystem get model /value'
) do for /f "delims=" %%b in ("%%~a") do (
if /i "%%~b"=="test1" goto start
if /i "%%~b"=="test2" goto start
if /i "%%~b"=="test4" goto start
)
echo un-compatible system
goto :eof
或
for /f "tokens=2 delims==" %%a in (
'wmic computersystem get model /value'
) do for /f "delims=" %%b in ("%%~a") do (
if /i "%%~b"=="test1" ( goto start
) else if /i "%%~b"=="test2" ( goto start
) else if /i "%%~b"=="test3" ( goto start
) else (
echo un-compatible system
goto :eof
)
)
或
for /f "tokens=2 delims==" %%a in (
'wmic computersystem get model /value'
) do for /f "delims=" %%b in ("%%~a") do (
if /i "%%~b"=="test1" (
goto start
) else if /i "%%~b"=="test2" (
goto start
) else if /i "%%~b"=="test3" (
goto start
) else (
echo un-compatible system
goto :eof
)
)
或任何其他更适合您的代码的组合/样式,但您必须考虑括号的位置很重要。 if 左括号需要与if 命令位于同一行。 if 右括号需要与else 子句(如果存在)位于同一行。 else 左括号需要与else 子句位于同一行。