batch-file - 用于查找系统品牌和型号的批处理脚本,然后运行另一个脚本

标签 batch-file

我正在尝试制作一个批处理脚本来查找计算机制造商、模型,然后执行我已经创建的另一个批处理脚本。

到目前为止,我得到了计算机制造商:

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/

相关文章:

java - 如何从 Intellij IDEA 项目(Selenium 和 TestNG)创建 java 批处理文件

windows - 使用当前路径位置设置变量的批处理脚本

Windows 批处理变量

windows - 批量拆分一个文本文件

java - 在 Eclipse Mars 中通过命令行创建一个 war 文件

batch-file - 重定向批处理文件的输出,包括命令

batch-file - 如何禁用 `eol` 的 `delims` 和 `for/F` 选项?

windows - 如何在批处理文件中执行多个 git 命令而不在第一个命令后终止?

windows - 带有 for 循环和管道的批处理脚本

php - 第 47 行警告 : mysql_query(): supplied argument is not a valid MySQL-Link resource in/home/vinem/www/batch/batch_stock. php