我试图制作批处理脚本来找出计算机制造商,模型,然后执行我已经创建的另一个批处理脚本。
到目前为止,我有这个让计算机制造商:
FOR /F "tokens=2 delims='='" %%A in ('wmic ComputerSystem Get Manufacturer /value') do SET manufacturer=%%A
这是为了获得模型:
FOR /F "tokens=2 delims='='" %%A in ('wmic ComputerSystem Get Model /value') do SET model=%%A
现在我不确定如何在IF语句中使用make和models作为变量。
答案 0 :(得分:1)
第一个问题:如何将wmic
输出分配给环境变量:
for /f "tokens=1* delims==" %%a in (
'wmic computersystem get model /value'
) do for /f "delims=" %%c in ("%%~b") do set "model=%%c"
echo %model%
for循环的位置
%%a
检索模型(在第二个标记中,%%b
)%%c
删除返回值中的结束回车符(wmic
行为:每个输出行以0x0D0D0A
结尾而不是公共0x0D0A
)另一个潜在问题:如果在括号(
中的命令块)
内分配,如何引用环境变量:
@ECHO OFF >NUL
SETLOCAL enableextensions disabledelayedexpansion
set "model=nothing assigned yet"
for /f "tokens=1* delims==" %%a in (
'wmic computersystem get model /value'
) do for /f "delims=" %%c in ("%%~b") do (
set "model=%%c"
echo %model% :referenced using percent signs within a command block
SETLOCAL enabledelayedexpansion
echo !model! :referenced using exclamation marks within a command block
ENDLOCAL
)
echo %model% :referenced using percent signs out of a command block
ENDLOCAL
goto :eof
运行以上脚本以查看EnableDelayedExpansion效果:
==>D:\bat\StackOverflow\29893432.bat
nothing assigned yet :referenced using percent signs within a command block
System Product Name :referenced using exclamation marks within a command block
System Product Name :referenced using percent signs out of a command block
==>
按照Stephan关于使用If
statement的评论。