Can anyone explain this ONE line of code? (ipconfig text into variables)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
esuhl
Posts: 9
Joined: 04 Feb 2017 22:03

Can anyone explain this ONE line of code? (ipconfig text into variables)

#1 Post by esuhl » 14 Feb 2017 10:04

Hi. I've written some very simple batch files, but I'm a bit of a newbie.

I want to run "ipconfig", and put the gateway IP address into a variable. I found this solution on another forum:

Code: Select all

set "ip="
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "Default"') do if not defined ip set ip=%%b

http://stackoverflow.com/questions/2236 ... batch-file

It works perfectly for me, but I simply don't understand it! :? Can anyone explain it to me (very slowly)?

Ideally, I want the script to work on any version of Windows (and in any region). Will this line of code work on any version of Windows (or at least XP onwards)? And, am I right in thinking that searching for the text "Default" would only work if the language was set to English? If so, are there any other ways to get the gateway IP? Ooh -- and will this work if the gateway uses IPv6 instead of IPv4?

Sorry -- so many questions! Any little tips or hints would be greatly appreciated! :D

EDIT: I just found this link, which goes a long way to explain what's happening. I'm going to need to re-read it a dozen times to wrap my head round it, I think!

https://blogs.msdn.microsoft.com/oldnew ... 00/?p=7003

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Can anyone explain this ONE line of code? (ipconfig text into variables)

#2 Post by aGerman » 14 Feb 2017 12:15

esuhl wrote:It works perfectly for me, but I simply don't understand it! :? Can anyone explain it to me (very slowly)?

Have a look at the output of IPCONFIG in a CMD window to understand how it works.
ipconfig|find "Default" is a filter that let pass only lines that contain "Default". The extra caret (^) in front of the pipe (|) is necessary because the line will be parsed twice. 1st in the batch code, 2nd as command line in a separate cmd.exe instance run in the FOR loop.
The output of this command line will be processed in the FOR /F loop where the first and the second substring (see tokens) separated by a colon (see delims) will be accessible in the FOR variables. The first substring will be saved in %%a. Thus, the second substring (that is, the IP address) will be saved in %%b. The reason why variable ip was undefined in the first line is to make sure only the first found IP address is set to variable ip (after that ip is defined and the if not defined protects it from overwriting).

esuhl wrote:Ideally, I want the script to work on any version of Windows (and in any region).

No way. The output of IPCONFIG is highly language-dependent. Default gateway IP address if I run it:

Code: Select all

   Standardgateway . . . . . . . . . : 192.168.178.1


You may try ...

Code: Select all

for /f "delims={}," %%i in ('wmic nicconfig where ipenabled^=true get defaultipgateway^|find "{"') do set "ip=%%~i"

... that should be language-independent. Although WMIC was not suported on some XP versions.

Steffen

esuhl
Posts: 9
Joined: 04 Feb 2017 22:03

Re: Can anyone explain this ONE line of code? (ipconfig text into variables)

#3 Post by esuhl » 14 Feb 2017 13:08

Excellent! Thanks for the links and explanation. I'll have a proper read through and another play around. That's just the kind of stuff I wanted to know!

One quick question... I saw that there are some functions (code snippets) on this site. One is a function called :getIPConfig, which puts IPCONFIG /ALL data into an array:
http://www.dostips.com/DtCodeCmdLib.php ... etIPConfig

This is probably a really stupid question, but... I can't figure out how to use it. :oops:

What is the name of the array that the function creates? Or do I need to somehow create an array, and pass it to the function to populate with IPCONFIG data...?

Thanks again :-)

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Can anyone explain this ONE line of code? (ipconfig text into variables)

#4 Post by Squashman » 14 Feb 2017 13:50

esuhl wrote:One quick question... I saw that there are some functions (code snippets) on this site. One is a function called :getIPConfig, which puts IPCONFIG /ALL data into an array:
http://www.dostips.com/DtCodeCmdLib.php ... etIPConfig

This is probably a really stupid question, but... I can't figure out how to use it. :oops:

What is the name of the array that the function creates? Or do I need to somehow create an array, and pass it to the function to populate with IPCONFIG data...?

Thanks again :-)


Well I don't know how useful it is. It creates a pseduo array of environmental variables.

Code: Select all

@echo off

call :getIPConfig arr
set arr
pause
goto :eof


:getIPConfig arr -- return IPCONFIG /ALL data in array variable
::               -- arr [out] - target array variable for IPCONFIG data
:$created 20091111 :$changed 20091111 :$categories Network,Array
:$source http://www.dostips.com
if "%~1" NEQ "" for /f "delims==" %%A in ('set %~1[ 2^>NUL') do @set "%%A="
SETLOCAL ENABLEDELAYEDEXPANSION
set "data="
set /a n=0
for /f "tokens=1,* delims=:" %%A in ('ipconfig /all^|find ":"') do (
    set "v=%%~A "
    if "!v:~0,8!" NEQ "        " (
        rem it's a new section
        set /a n+=1
        set "data=!data!&set %~1[!n!].DisplayName=%%A"
    ) ELSE (
        set "v=!v:~8!"
        set "v=!v:.=!"
        set "v=!v: =!"
        set "x=%%~B"
        set "data=!data!&set %~1[!n!].!v!=!x:~1!"
    )
)
set "data=rem.%data:)=^)%"
( ENDLOCAL & REM RETURN VALUES
    %data%
    SET "%~1[#]=%n%"
)
EXIT /b

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Can anyone explain this ONE line of code? (ipconfig text into variables)

#5 Post by aGerman » 14 Feb 2017 14:05

This function doesn't even work for me. There are only three preceding blanks in my output of IPCONFIG /ALL

Code: Select all

@echo off
call :getIPConfig arr
for /l %%i in (1 1 %arr[#]%) do set arr[%%i]
pause
exit /b

:getIPConfig arr -- return IPCONFIG /ALL data in array variable
::               -- arr [out] - target array variable for IPCONFIG data
:$created 20091111 :$changed 20091111 :$categories Network,Array
:$source http://www.dostips.com
if "%~1" NEQ "" for /f "delims==" %%A in ('set %~1[ 2^>NUL') do @set "%%A="
SETLOCAL ENABLEDELAYEDEXPANSION
set "data="
set /a n=0
for /f "tokens=1,* delims=:" %%A in ('ipconfig /all^|find ":"') do (
    set "v=%%~A "
    if "!v:~0,3!" NEQ "   " (
        rem it's a new section
        set /a n+=1
        set "data=!data!&set %~1[!n!].DisplayName=%%A"
    ) ELSE (
        set "v=!v:~3!"
        set "v=!v:.=!"
        set "v=!v: =!"
        set "x=%%~B"
        set "data=!data!&set %~1[!n!].!v!=!x:~1!"
    )
)
set "data=rem.%data:)=^)%"
( ENDLOCAL & REM RETURN VALUES
    %data%
    SET "%~1[#]=%n%"
)
EXIT /b

In %arr[#]% you'll find the number of sections read. Because it doesn't make sense to tell you the names of the other variables (they are language dependent) I used the SET command to display the Name=Value pairs.
If this doesn't work for you try to use the original function (where I replaced the 8s with 3s and 8 blanks with 3 blanks) or count the number of preceding blanks in your output of IPCONFIG /ALL in order to adapt the function by your own.

Steffen

Post Reply