3 回答

TA貢獻1829條經驗 獲得超4個贊
是的,您有問題,但這不是您的想法。
wmic 具有特殊的行為:在每行輸出的末尾有一個附加的回車符,即每行以 0x0d 0x0d 0x0a
這個附加的回車符存儲在變量中,當回顯到控制臺時,您將獲得數據和回車符,因此,如果變量后面跟有更多文本,則將光標定位在行的開頭(回車),此文本將在先前回顯的數據之上回顯。
怎么解決?
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,* delims==" %%a in ('wmic baseboard get /format:list') DO (
if ["%%a"] EQU ["Product"] (
for /f "delims=" %%c in ("%%b") do set "PlatformInfo=%%c"
if defined PlatformInfo (
echo(!PlatformInfo!
echo(!PlatformInfo!This does not overwrite the variable
)
)
if ["%%a"] EQU ["Version"] (
for /f "delims=" %%c in ("%%b") do set "BaseboardVersion=%%c"
if defined BaseboardVersion (
echo(!BaseboardVersion!
echo(!BaseboardVersion!This does not overwrite the variable
)
)
)
在這種情況下,可以在不更改代碼邏輯的情況下使用附加for /f命令刪除附加回車符

TA貢獻1785條經驗 獲得超8個贊
正如已經回答的那樣,問題出在的行尾wmic。
如果不使用行尾,則可以解決此問題:wmic baseboard get Product,Version,Width使用三個標記:Product,Version和Width(在大多數情況下為empy)。因此輸出將是:DX79SI,AAG28808-600,我們正在使用令牌1和令牌2,而忽略令牌3(這將有問題)
set Platform=undefined
set BaseboardVersion=undefined
for /f "tokens=1,2 delims=," %%a in ('wmic baseboard get Product^,Version^,Width^|findstr "."') do (
set Platform=%%a
set BaseboardVersion=%%b
)
echo it is a %Platform% with Version %BaseboardVersion%. No overwriting
我還添加delims=,了萬一字符串包含空格的情況(不太可能出現product)
- 3 回答
- 0 關注
- 647 瀏覽
添加回答
舉報