0

I'm having trouble with using an if else statement in a batch command. I'm running Windows XP. I wrote a simple batch file to output my ip address which works fine. However, I would like to include "No ip address found" if there isn't any ip address to be shown. I've looked at similar questions on here but can't find a solution for this.

Here is the code I have (which works fine for when there is an ip address)

@echo off
color 02
mode con:cols=60 lines=3
Title What's my ip?
:start
echo.
echo                   Here is your ip address!
ipconfig > nul
ipconfig > nul
ipconfig > nul
cls
echo.
ipconfig | find "IP Address"
ipconfig > nul
ipconfig > nul
ipconfig > nul
ipconfig > nul

Any help would be much appreciated. Thanks.

1
  • 1
    Just finding "IP Address" on the IPCONFIG output can produce spurious or useless results. In my configuration, it will show three IP addresses, two of them are really useless (vpn loopback and vpn autoconfig). Commented Oct 4, 2012 at 8:38

2 Answers 2

1

find will return with a non-zero return value, if no result is found. Try something like

ipconfig | find "IP Address" || echo No IP found

As a sidenote: Your script doesn't work with my Win7 installation, you'd have to search for the string "IPv4-Adresse" or "IPv6-Adresse". find "IP" might be a little bit more generic.

Sign up to request clarification or add additional context in comments.

Comments

0
ipconfig /all | find "v4" || echo No IP found

This works better for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.