Echo在我的Windows批处理脚本中无法正常工作

主要问题:

ECHO命令将忽略令牌参数前面的所有字符,并在输出令牌之前的令牌参数后面插入所有文本:

这是脚本:

@ECHO OFF FOR /F "USEBACKQ TOKENS=2 DELIMS==" %%a IN (` WMIC NIC WHERE "MACAddress IS NOT NULL" GET MACAddress /FORMAT:LIST ^| FINDSTR /V /R "^%" `) DO ( ECHO [%%a] ECHO 0123456789%%a9876543210 ) PAUSE > NUL 

这是预期的输出:

 [AA:BB:CC:11:22:33] 0123456789AA:BB:CC:11:22:339876543210 

这是实际的输出:

 ]AA:BB:CC:11:22:33 9876543210AA:BB:CC:11:22:33 

为什么我在乎

我的意图是用这个块代替上面例子中的DO()块:

 :: ...FOR LOOP STUFF... `) DO ( WMIC NIC WHERE ^ "MACAddress=%%a" ^ GET ^ NetConnectionID ^ /FORMAT:LIST | FINDSTR /V /R "^$" WMIC NICCONFIG WHERE ^ "MACAddress=%%a" ^ GET ^ DefaultIPGateway,^ DNSServerSearchOrder,^ IPAddress,^ IPSubnet,^ MacAddress ^ /FORMAT:LIST | FINDSTR /V /R "^$" ) 

如上所述,%% a之前的所有字符被截断,%% a之后的所有字符在处理脚本之前出现在%% a之前。 有人请帮忙,我在这里象征性地哭泣。

 @ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION FOR /F "TOKENS=2 DELIMS==" %%a IN (' WMIC NIC WHERE "MACAddress IS NOT NULL" GET MACAddress /FORMAT:LIST ') DO ( SET "macaddr=%%a" SET "macaddr=!macaddr:~0,-1!" ECHO [!macaddr!] ECHO 0123456789!macaddr!9876543210 ) GOTO :EOF 

我发现没有理由为发现,代码给了我没有输出。 删除findstr提供了报告的输出。

可以通过将wmic输出重定向到一个文件并用十六进制编辑器检查结果来观察wmic 。 解决办法是简单地机械地移除分配给令牌的值的最后一个字符。

我简单地使用Getmac命令就像在这个脚本中一样:

 @echo off mode 80,5 & Color 9E Title Get (LAN ,Public) (IP) and MAC Addresses by Hackoo 2017 cls echo( & echo( echo Please Wait a While ... Searching for (LAN ,Public)(IP) and MAC addresses ... @for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do ( set "LAN_IP=%%a" ) for /f "tokens=2 delims=: " %%A in ( 'nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"' ) Do set ExtIP=%%A @For /f %%a in ('getmac /NH /FO Table') do ( @For /f %%b in ('echo %%a') do ( If /I NOT "%%b"=="N/A" ( Set "MY_MAC=%%b" ) ) ) Cls echo( echo My Private LAN IP : %LAN_IP% echo My External Public IP : %ExtIP% echo MAC Address : %MY_MAC% Timeout /T 5 /NoBreak>nul & Exit