1

Main Problem:

The ECHO command ignores all characters before the token argument and inserts all text after the token argument before the token in the output:

Here's the script:

@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

Here's the expected output:

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

Here's the actual output:

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

Why do I care?

My intention is to replace the DO () block in the example above with this block:

:: ...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 "^$"
)

As described above, all characters before %%a get cut off and all characters after %%a show up before %%a when the script is processed. Somebody please help, I'm figuratively crying here.

Rhyknowscerious
  • 260
  • 6
  • 12
  • Can you narrow it down to a simpler script? – Lasse V. Karlsen Aug 26 '17 at 20:31
  • Well, I replaced backquotes part with (\`ECHO TEST\`) and the output was [TEST] as expected. So it seems this issue may be specific to WMIC output. – Rhyknowscerious Aug 26 '17 at 20:36
  • I also replaced the beginning part with FOR /F `"USEBACKQ TOKENS=*" %%a IN (\`WMIC NIC GET MACAddress\`)` and observed the same output issue as in the first example. – Rhyknowscerious Aug 26 '17 at 20:42
  • That's... weird. Just off the top of my head without actually testing anything, I'm guessing that it's probably related to the regex string you're using. `%` symbols need to be doubled up inside of batch scripts, so try `findstr /V /R "^%%"` instead. – SomethingDark Aug 26 '17 at 21:09
  • 1
    wmic has a known bug to output lines with a cr/cr/lf sequence. Here I'd not use wmic but the program `Getmac.exe` –  Aug 26 '17 at 21:34
  • LotPings, can you tell us where we can read more about the WMIC output bug? I don't want to use GETMAC but it seems I have no choice. Also, if you propose your comment as an answer within the next few hours here, I'll mark it as the solution. – Rhyknowscerious Aug 26 '17 at 22:53

2 Answers2

1
@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

I could find no reason for the findstr - as posted, the code gave me no output at all. Removing the findstr provided the reported output.

The wmic quirk can be observed by redirecting wmic output to a file and examining the result with a hex-editor. The cure is to simply mechanically remove the last character of the value assigned to the token.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Brilliant, I would have never thought to do that. This allows for me to avoid GETMAC and use all WMIC. I used `FINDSTR /V /R "^$"` because I didn't use quotes in my SET command, which lead to newline output (showing `ECHO is off.`) in my loop. I should have used `SET "_MACAddress=%%a"` instead of `SET _MACAddress=%%a`. – Rhyknowscerious Aug 27 '17 at 23:23
1

I simply use Getmac command like in this script :

@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 
Hackoo
  • 18,337
  • 3
  • 40
  • 70