Find IPv4, help me understand!?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
joakim
Posts: 24
Joined: 07 Mar 2012 12:08

Find IPv4, help me understand!?

#1 Post by joakim » 22 Aug 2012 12:08

Hi, i need help with this code/script. i havent done it, my self, i just want help to undesstand it a bit more

My code/Script:

@echo off
cls
title Internet Protocol (IP)
del IPv4.txt
echo.



for /F "TOKENS=2* DELIMS=:" %%a in ('IPCONFIG ^| find "IPv4"') do for %%b in (%%a) Do set MyIP=%%b

echo ============================= >> IPv4.txt
echo Your PC,s Internet Protocol,s >> IPv4.txt
echo ============================= >> IPv4.txt

echo Internet Protocol version 4: %MyIP% >> IPv4.txt

exit



Its this line i need help with: for /F "TOKENS=2* DELIMS=:" %%a in ('IPCONFIG ^| find "IPv4"') do for %%b in (%%a) Do set MyIP=%%b

The only thing i know is that its trying to find the word "IPv4", and i know that it creates an variable that saves in to an .txt file.

PLZ help me to understand the code =)

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Find IPv4, help me understand!?

#2 Post by abc0502 » 22 Aug 2012 16:02

Hi, Joakem

The for command execute the commands between the two ( ) and any output will be assigned to a variable %%a or whatever charachter you put there.

So the command

Code: Select all

IPCONFIG ^| find "IPv4"

It has two commands, the first is "IPCONFIG", If you execute that in the command line you will get somthing like that
Windows IP Configuration


Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 50.1.1.15
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 10.10.10.10


and this code " ^|" redirect that out put to the command "Find" that search for the word "IPv4" in the previout output.

so if you use this command in the cmd window

Code: Select all

IPCONFIG |find "IPv4"
it should give you the whole line that contain the word "IPv4"

Note, when i run that code it give me nothing, casue as you see in the out put, there is no words like "IPv4"
but if i change that word to "IP" it will give me two lines that has the word "IP".
Windows IP Configuration
IP Address. . . . . . . . . . . . : 50.1.1.15


BUt note that the for loop uses a delimt which is : so the out put for the for loop in a command line should give you just the IP "50.1.1.15" not the whole two lines.

joakim
Posts: 24
Joined: 07 Mar 2012 12:08

Re: Find IPv4, help me understand!?

#3 Post by joakim » 29 Aug 2012 01:36

Tnx for the help i think i get it now :D

Post Reply