2017-07-26 67 views
1

從其他的問題,我已經在這裏找到了格式,我想這會工作怎樣才能getmac輸出成批處理文件的變量,並保持

SET LF=^ 


SET output= 
SET getmac_cmd=getmac /v /fo list 
FOR /F "USEBACKQ tokens=*" %%F in (`!getmac_cmd!`) DO (
    set output=!output!!LF!%%F 
) 
ECHO !output! 

該命令的輸出直接看起來像

Connection Name: Local Area Connection 
Network Adapter: Intel Something 
Physical Address: 00-00-00-00-00-00 
Transport Name: Media disconnected 

Connection Name: Bluetooth Network Connection 
Network Adapter: Bluetooth Something 
Physical Address: 00-00-00-00-00-00 
Transport Name: Media disconnected 

但是,當通過批處理腳本運行,我得到

Connection Name: Local Area Connection 
Network Adapter: Intel Something 
Physical Address: 00-00-00-00-00-00 
Transport Name: Media disconnected 
Connection Name: Bluetooth Network Connection 
Network Adapter: Bluetooth Something 
Physical Address: 00-00-00-00-00-00 
Transport Name: Media disconnected 

任何線索,我可以改變它去保留段之間的實際空白行?

+0

爲什麼你需要在一個變量整個文本? – aschipfl

回答

2

空行缺少在輸出中,因爲for /F忽略空行。用戶Mofihis answer已經演示瞭如何使用findstr /N解決此問題:

@echo off 
setlocal EnableDelayedExpansion 
(set LF=^ 
%= empty line =% 
) 
set "output=" 
for /F "tokens=1* delims=:" %%E in ('getmac /V /FO LIST ^| findstr /N /R "^"') do (
    set "output=!output!!LF!%%F" 
) 
echo/!output! 
endlocal 

然而,這種失敗如果出現感嘆號,因爲當delayed expansion啓用,消耗!%%F成爲擴大。此外,前導冒號(儘管getmac的輸出中不太可能)被刪除,因爲for /F將後續分隔符視爲一個。

爲了解決這些問題,下面的代碼可以使用(見解釋性發言):

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 

(set LF=^ 
%= empty line =% 
) 

set "output=" 
for /F "delims=" %%E in ('getmac /V /FO LIST ^| findstr /N /R "^"') do (
    rem // Expand `for` variable `%%F` while delayed expansion is disabled: 
    set "line=%%F" 
    setlocal EnableDelayedExpansion 
    rem /* Remove the leading line number and the first colon by sub-string replacement 
    rem (like `!line:*:=!`, see below) so every other leading colons are maintained; 
    rem since delayed expansion is toggled within the `for /F` loop, variable `output` 
    rem would not survive the `endlocal` barrier, so let another `for /F` loop carry 
    rem the whole assignment string, including the variable name, beyond `endlocal`; 
    rem that way, we do not have to care about empty strings or the default `eol`: */ 
    for /F "delims=" %%A in ("output=!output!!LF!!line:*:=!") do (
     endlocal 
     rem // Again the `for` variable is expanded while delayed expansion is disabled: 
     set "%%A" 
    ) 
) 

setlocal EnableDelayedExpansion 
echo/!output! 
endlocal 

endlocal 
exit /B 
1

在命令提示符窗口for /?中運行的多個頁面上的幫助輸出解釋了命令FOR忽略空白行。

的溶液使用命令FINDSTR與選項/N以輸出每個找到的線,其可以簡單地認爲所有行包括空行,並從該輸出中刪除的行號之前的行號。

getmac /v /fo list | findstr /R /N "^"的輸出是:

1:Connection Name: Local Area Connection 
2:Network Adapter: Intel Something 
3:Physical Address: 00-00-00-00-00-00 
4:Transport Name: Media disconnected 
5: 
6:Connection Name: Bluetooth Network Connection 
7:Network Adapter: Bluetooth Something 
8:Physical Address: 00-00-00-00-00-00 
9:Transport Name: Media disconnected 

批處理文件來處理此輸出並將其分配給環境變量output而不行號是:

@echo off 
setlocal EnableDelayedExpansion 
set LF=^ 


set output= 
for /F "tokens=1* delims=:" %%A in ('%SystemRoot%\System32\getmac.exe /v /fo list 2^>^&1 ^| %SystemRoot%\System32\findstr.exe /N /R "^"') DO set "output=!output!!LF!%%B" 
echo !output! 
endlocal 

該代碼還捕捉錯誤輸出GETMAC寫入STDERR通過複製手柄STDOUT這是管道到STDINFINDSTR

爲了解所使用的命令及其工作方式,請打開命令提示符窗口,在其中執行以下命令,並仔細閱讀爲每個命令顯示的所有幫助頁面。

  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • getmac /?
  • set /?
  • setlocal /?

請閱讀微軟有關Using Command Redirection Operators的文章,以獲得對2>&1|的解釋。

運營商>&|必須在這裏與插入符號^轉義首先被解釋爲文字字符在解析FOR由Windows命令解釋命令行。

後來由FOR在一個單獨的命令處理在後臺命令行執行:

C:\Windows\System32\getmac.exe /v /fo list 2>&1 | C:\Windows\System32\findstr.exe /N /R "^" 
相關問題