标题: [其他] [已解决]批处理调用wmic获取IP和MAC变量显示不正常 [打印本页]
作者: 不知道是谁 时间: 2021-6-13 01:17 标题: [已解决]批处理调用wmic获取IP和MAC变量显示不正常
- @echo off
- setlocal EnableDelayedExpansion
- for /f "tokens=2 delims==" %%i in ('Wmic Nic Where "NetConnectionID<>Null And PNPDeviceID Like '[PR]%%'" Get MACAddress /Value^|find "="') do (
- set /a Sum+=1
- for /f "tokens=2 delims={,}" %%j in ('Wmic NicConfig Where "IPEnabled=True And MACAddress='%%i'" Get IPAddress /Value 2^>nul^|find "="') do (
- set IP!Sum!=%%~j
- set MAC!Sum!=%%i
- )
- )
- for /l %%i in (1,1,%Sum%) do echo;!MAC%%i! !IP%%i!
- pause
复制代码
改成 for /l %%i in (1,1,%Sum%) do echo;!IP%%i! !MAC%%i! 就正常显示了,这是什么原因?
通过楼下的回答,使用变量截取倒数几位可以证实获取到的MACAddress确实存在其他不可见的字符,原因找到就好办了- @echo off
- setlocal EnableDelayedExpansion
- for /f "tokens=2 delims==" %%i in ('Wmic Nic Where "NetConnectionID<>Null And PNPDeviceID Like '[PR]%%'" Get MACAddress /Value^|find "="') do (
- set /a Sum+=1
- set MAC=%%i
- set MAC=!MAC::=-!
- for /f "tokens=2 delims={,}" %%j in ('Wmic NicConfig Where "IPEnabled=True And MACAddress='%%i'" Get IPAddress /Value 2^>nul^|find "="') do (
- set IP!Sum!=%%~j
- set MAC!Sum!=!MAC:~,17!
- )
- )
- for /l %%i in (1,1,%Sum%) do echo;!MAC%%i! !IP%%i!
- pause
复制代码
作者: qixiaobin0715 时间: 2021-6-13 08:45
!MAC%%I!有非打印字符CR
将第7行改为- set MAC=%%i
- set MAC!Sum!=!MAC:~,1!
复制代码
作者: Batcher 时间: 2021-6-14 12:37
回复 1# 不知道是谁
参考:批处理调用WMIC时如何处理结果中的特殊字符?
http://bbs.bathome.net/thread-2862-1-1.html
http://bbs.bathome.net/thread-28534-1-1.html
http://bbs.bathome.net/thread-28535-1-1.html
http://bbs.bathome.net/thread-12033-1-1.html
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |