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
    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). – PA. Oct 04 '12 at 08:38

2 Answers2

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.

Mark
  • 6,033
  • 1
  • 19
  • 14
0
ipconfig /all | find "v4" || echo No IP found

This works better for me.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Steve Hull
  • 19
  • 1