Check if a variable begins with .....

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
djokoss22
Posts: 2
Joined: 10 Dec 2016 11:17

Check if a variable begins with .....

#1 Post by djokoss22 » 10 Dec 2016 11:31

Hi guys,
i am trying to script a code that find the ip address of a network card and check if that ip is an APIPA (169.254.x.x)

How can i check if a variable start with "169.254."

Here is what i did but it does not work:

Code: Select all

for /f "tokens=2 delims=:" %%a in ('ipconfig ^|find "IPv4"') do set ipadd=%%a
if "%ipadd:~0,8%"=="169.254." (
echo ERROR: APIPA address found
) else (
ping -n 4 %ipadd%
)

It pings the ip event if it start with 169.254.

Could somebody explain me what i should do?
Thx a lot ;)

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Check if a variable begins with .....

#2 Post by Compo » 10 Dec 2016 17:21

If I remember correctly there is a space after the :
So your solution is likely to be "change your ~0,8 to ~1,8"

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

Re: Check if a variable begins with .....

#3 Post by aGerman » 10 Dec 2016 17:26

You use the colon as delimiter. Check the raw output of IPCONFIG again. I assume there is at least one space between colon and the beginning of the address. Try

Code: Select all

if "%ipadd:~0,9%"==" 169.254." (

Steffen

djokoss22
Posts: 2
Joined: 10 Dec 2016 11:17

Re: Check if a variable begins with .....

#4 Post by djokoss22 » 10 Dec 2016 18:47

Compo wrote:If I remember correctly there is a space after the :
So your solution is likely to be "change your ~0,8 to ~1,8"


In fact, there is a space before the ip address ! The solution is ~1,8
Thx for the help. :wink:

Post Reply