user1718443
user1718443

Reputation: 1

If else batch command with ipconfig

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.

Upvotes: 0

Views: 1405

Answers (2)

Steve Hull
Steve Hull

Reputation: 19

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

This works better for me.

Upvotes: 0

Mark
Mark

Reputation: 6071

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.

Upvotes: 1

Related Questions