3 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個(gè)贊
是的,您有問(wèn)題,但這不是您的想法。
wmic 具有特殊的行為:在每行輸出的末尾有一個(gè)附加的回車(chē)符,即每行以 0x0d 0x0d 0x0a
這個(gè)附加的回車(chē)符存儲(chǔ)在變量中,當(dāng)回顯到控制臺(tái)時(shí),您將獲得數(shù)據(jù)和回車(chē)符,因此,如果變量后面跟有更多文本,則將光標(biāo)定位在行的開(kāi)頭(回車(chē)),此文本將在先前回顯的數(shù)據(jù)之上回顯。
怎么解決?
@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命令刪除附加回車(chē)符

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
正如已經(jīng)回答的那樣,問(wèn)題出在的行尾wmic。
如果不使用行尾,則可以解決此問(wèn)題:wmic baseboard get Product,Version,Width使用三個(gè)標(biāo)記:Product,Version和Width(在大多數(shù)情況下為empy)。因此輸出將是:DX79SI,AAG28808-600,我們正在使用令牌1和令牌2,而忽略令牌3(這將有問(wèn)題)
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=,了萬(wàn)一字符串包含空格的情況(不太可能出現(xiàn)product)
- 3 回答
- 0 關(guān)注
- 645 瀏覽
添加回答
舉報(bào)