我正在尝试制作一个批处理脚本来查找计算机制造商、模型,然后执行我已经创建的另一个批处理脚本。
到目前为止,我得到了计算机制造商:
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 作为变量。
最佳答案
第一个问题:如何将 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
检索模型(在第二个 token 中,%%b
)%%c
删除返回值中的结尾回车符(wmic
行为:每个输出行以0x0D0D0A
而不是常见的结尾0x0D0A
)
另一个潜在问题:如何引用环境变量,如果在命令 block 中分配(
在括号中)
:
@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 的评论.
关于batch-file - 用于查找系统品牌和型号的批处理脚本,然后运行另一个脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29893432/