Board logo

标题: [其他] [已解决]批处理调用wmic获取IP和MAC变量显示不正常 [打印本页]

作者: 不知道是谁    时间: 2021-6-13 01:17     标题: [已解决]批处理调用wmic获取IP和MAC变量显示不正常

  1. @echo off
  2. setlocal EnableDelayedExpansion
  3. for /f "tokens=2 delims==" %%i in ('Wmic Nic Where "NetConnectionID<>Null And PNPDeviceID Like '[PR]%%'" Get MACAddress /Value^|find "="') do (
  4.     set /a Sum+=1
  5.     for /f "tokens=2 delims={,}" %%j in ('Wmic NicConfig Where "IPEnabled=True And MACAddress='%%i'" Get IPAddress /Value 2^>nul^|find "="') do (
  6.         set IP!Sum!=%%~j
  7.         set MAC!Sum!=%%i
  8.     )
  9. )
  10. for /l %%i in (1,1,%Sum%) do echo;!MAC%%i! !IP%%i!
  11. pause
复制代码
改成 for /l %%i in (1,1,%Sum%) do echo;!IP%%i! !MAC%%i! 就正常显示了,这是什么原因?

通过楼下的回答,使用变量截取倒数几位可以证实获取到的MACAddress确实存在其他不可见的字符,原因找到就好办了
  1. @echo off
  2. setlocal EnableDelayedExpansion
  3. for /f "tokens=2 delims==" %%i in ('Wmic Nic Where "NetConnectionID<>Null And PNPDeviceID Like '[PR]%%'" Get MACAddress /Value^|find "="') do (
  4.     set /a Sum+=1
  5.     set MAC=%%i
  6.     set MAC=!MAC::=-!
  7.     for /f "tokens=2 delims={,}" %%j in ('Wmic NicConfig Where "IPEnabled=True And MACAddress='%%i'" Get IPAddress /Value 2^>nul^|find "="') do (
  8.         set IP!Sum!=%%~j
  9.         set MAC!Sum!=!MAC:~,17!
  10.     )
  11. )
  12. for /l %%i in (1,1,%Sum%) do echo;!MAC%%i! !IP%%i!
  13. pause
复制代码

作者: qixiaobin0715    时间: 2021-6-13 08:45

!MAC%%I!有非打印字符CR
将第7行改为
  1. set MAC=%%i
  2. 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