【发布时间】:2014-07-25 17:53:44
【问题描述】:
我正在使用以下代码检查批处理文件中的基板信息。
BaseboardCheck.cmd:
@echo off
setlocal EnableDelayedExpansion
for /f "tokens=1,2* delims==" %%a in ('wmic baseboard get /format:list') do (
if ["%%a"] EQU ["Product"] (
set PlatformInfo=%%b
if defined PlatformInfo (
echo.!PlatformInfo!
echo.!PlatformInfo!This overwrites the variable
)
)
if ["%%a"] EQU ["Version"] (
set BaseboardVersion=%%b
if defined BaseboardVersion (
echo.!BaseboardVersion!
echo.!BaseboardVersion!This overwrites the variable
)
)
)
上述问题:当回显出来时,变量会被覆盖而不是附加到。
输出:
DX79SI
This overwrites the variable
AAG28808-600
This overwrites the variable
我想得到的是:
DX79SI
DX79SIThis overwrites the variable
AAG28808-600
AAG28808-600This overwrites the variable
我已经为此花费了几个小时(并将继续这样做),但我希望其他人也遇到了这个问题。我希望以后遇到这个解析问题的任何人都可以避免它。
由此产生的另一个问题是它似乎破坏了条件逻辑。
更新:
在所有的帮助之后,我想出了这个解决方案:
for /f "skip=2 tokens=1,2 delims=," %%a in ('wmic baseboard get Product^,Version^,Width /format:csv') do (
set Platform=%%a
set BaseboardVersion=%%b
)
echo.Platform: %Platform%
echo.Version %BaseboardVersion%
【问题讨论】:
-
好问题。 setlocal enabledelayedexpansion 就是这么复杂。我总是使用调用将参数传递给另一个函数。
标签: windows batch-file for-loop cmd wmic