我想让下面的 For /f 函数的输出变成红色,但不起作用。有什么简单的方法可以做到这一点。
for /f %%i in ('getmac^|find "-"') do @echo %ESC%[91m%%i%ESC%[0m && set result=%%i
修订-2
@echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
for /f %%i in ('getmac^|find "-"') do (
call :colorEcho 06 "%%i"
set result=%%i
)
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~
2">空 2>&1
修订 3
获取活动连接的MAC地址
@echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
for /f "skip=2 delims=, tokens=1,2,3,4" %%L in ('wmic nic where "netenabled=true" get macaddress^,index^,netconnectionid^,productname /format:csv') do @for /f "skip=2 delims={}, tokens=2" %%A in ('wmic nicconfig where "index=%%M" get ipaddress^,ipsubnet ^/format:csv') do (
echo %%N
set result=%%N
)
call :colorEcho 06 %result%
goto end
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
:end
pause
exit
答案1
要在批处理文件中为字符串着色,您可以尝试添加
@echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
在非常批处理脚本的顶部,然后
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
在非常它的底部(或者您也可以创建一个单独的 .bat 文件来调用)。
然后,您可以使用以下方式打印任何文本:
call :colorEcho 0c "Hi, I'm red"
笔记:特殊字符尚未“修补”(您无法打印它们)。我会找到一种方法来修补它们。
您可以通过这种方式打印命令的结果:
for /f %%i in ('getmac^|find "-"') do (
call :colorEcho 0c "%%i"
set result=%%i
)
如果你觉得还不错的话请告诉我!:)
答案2
@echo off
for /f skip^=4 %%a in ('"echo;prompt;$E|%ComSpec%"
')do for /f skip^=3 %%G in ('"%__AppDir__%getmac.exe"
')do echo/%%~a[31m%%~G %%~a[0;m && set "_result=%%~G"
- 在双 for 循环中:
@echo off
for /f skip^=4 %%a in ('echo;prompt;$E^|%ComSpec%"'
)do set "_esq=%%~a"
for /f skip^=3 %%G in ('"%__AppDir__%getmac.exe"')do =;(
echo/%_esq%[31m%%~G%_esq%[0m && set "_result=%%~G" );=
// 编辑参考:修订 3
echo off
for %%i in (NetConnectionID,ProductName,MacAddress,Index,IPSubNet,IPAddress)do set "_%%~i="
for /f usebackq^tokens^=4delims^=^<^> %%G in (
`wmic nic where netenabled^='true' get netconnectionid^,productname^,macaddress^,index /format:xml ^| find "VALUE&" ^| sort /r`
)do if not defined _netconnectionid =;(
set "_netconnectionid=%%~G"
)else if not defined _productname =;(
set "_productname=%%~G"
)else if not defined _macaddress =;(
set "_macaddress=%%~G"
)else for /f usebackq^tokens^=4delims^=^<^> %%i in =;(
`wmic nicconfig where index^='%%~G' get ipsubnet^,ipaddress /format:xml ^| find "VALUE&" ^| findstr "[1-9].\." ^| sort /r`
)do if not defined _ipsubnet =;(set "_index=%%~G" && set "_ipsubnet=%%~i")else set "_ipaddress=%%~i"
for /f skip^=4 %%a in ('echo;prompt;$E^|%ComSpec%')do for %%G in =;(
NetConnectionID,MacAddress,IPAddress,IPSubNet,Index
)do if "%%~G" == "NetConnectionID" =;(
%ComSpec% /v /e /c "echo/%%~a[33;4m%%~G:%%~a[0;m %%~a[93m!_%%~G! %%~a[0;m"
)else if "%%~G" == "MacAddress" =;(
%ComSpec% /v /e /c "echo/ %%~a[33;4m%%~G:%%~a[0;m %%~a[93m!_%%~G! %%~a[0;m"
)else if "%%~G" == "IPAddress" =;(
%ComSpec% /v /e /c "echo/ %%~a[33;4m%%~G:%%~a[0;m %%~a[93m!_%%~G! %%~a[0;m"
)else if "%%~G" == "IPSubNet" =;(
%ComSpec% /v /e /c "echo/ %%~a[33;4m%%~G:%%~a[0;m %%~a[93m!_%%~G! %%~a[0;m"
)else %ComSpec% /v /e /c "echo/ %%~a[33;4m%%~G:%%~a[0;m %%~a[93m!_%%~G! %%~a[0;m"