Batch file 如何将getmac输出转换为批处理文件中的变量并保持其格式
从我在这里发现的其他问题来看,我认为这是可行的Batch file 如何将getmac输出转换为批处理文件中的变量并保持其格式,batch-file,Batch File,从我在这里发现的其他问题来看,我认为这是可行的 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 Adap
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
有什么线索可以更改以保留节之间的实际空行吗?关于在命令提示窗口中运行的帮助输出
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
代码还捕获写入STDERR的GETMAC的错误输出,方法是使用STDOUT复制句柄,该句柄通过管道传输到FINDSTR的STDIN
要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面
echo/?
endlocal/?
findstr/?
获取/?
getmac/?
设置/?
setlocal/?
2>&1
和
的解释
必须在此处转义运算符
、&
和|
,并使用插入符号^
在Windows命令解释器解析命令行时首先将其解释为文字字符
稍后由FOR在命令行后台的单独命令过程中执行:
C:\Windows\System32\getmac.exe /v /fo list 2>&1 | C:\Windows\System32\findstr.exe /N /R "^"
输出中缺少空行,因为忽略空行。用户已经演示了如何通过使用
findstr/N
克服此问题:
@echo关闭
setlocal EnableDelayedExpansion
(左前)=^
%=空行=%
)
设置“输出=”
对于/F“tokens=1*delims=:”%%E in('getmac/V/FO LIST^ | findstr/N/R^')do(
设置“输出=!输出!!LF!%%F”
)
回声!输出!
端部
但是,如果出现感叹号,则此操作将失败,因为启用时,%%F
将展开,这将消耗代码>。此外,前导冒号(尽管在getmac
的输出中不太可能)被删除,因为for/F
将后续分隔符视为一个
为了解决这些问题,可以使用以下代码(参见解释性备注):
@echo关闭
setlocal EnableExtensions DisableDelayedExpansion
(左前)=^
%=空行=%
)
设置“输出=”
对于/F“delims=“%%E in('getmac/V/FO LIST^ | findstr/N/R“^”)do(
rem//Expand`for`variable`%%F`在禁用延迟扩展时:
设置“行=%%F”
setlocal EnableDelayedExpansion
rem/*通过替换子字符串删除前导行号和第一个冒号
rem(如“!line::=!”,请参见下文),因此每隔一个前导冒号都会被保留;
rem,因为延迟扩展在'for/F'循环、变量输出中切换`
rem无法通过“endlocal”障碍,所以让另一个“for/F”循环进行
rem“endlocal”之外的整个赋值字符串,包括变量名;
这样,我们就不必关心空字符串或默认的“eol”:*/
对于/F“delims=“%%A in”(“output=!output!!LF!!行:*:=!”)执行以下操作(
端部
rem//再次展开'for'变量,同时禁用延迟展开:
设置“%%A”
)
)
setlocal EnableDelayedExpansion
回声!输出!
端部
端部
退出/B
为什么需要在变量中包含整个文本?