2017-07-26 31 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 "^" 
相关问题