Link to home
Start Free TrialLog in
Avatar of Tonygret
TonygretFlag for United States of America

asked on

Need Batch File to return IP Address Windows 7

We have a batch file that we have on the desktop of every XP client. It's a simple batch file that when executed displays the name and IP address of the PC. However the file does not work with Windows 7 and returns some strange results.  Can anyone help me with creating a new file for windows 7?

The orignal XP verion is below along with a screen shot. If you try it on a Win7  machine you will see what I mean.  User generated image
@echo off
REM myip.bat
REM script to return just the IP address of the current host.
REM Works on Windows 2000 / Windows XP
COLOR 4f
REM TYPE "FOR /?" AT THE COMMAND PROMPT TO GET DETAILS REGARDING THIS COMMAND LINE.
REM NOTE THE SINGLE BACK QUOTES ARE THE ONES BELOW THE ~ KEY ON THE KEYBOARD.
FOR /F "usebackq tokens=2 delims=[]" %%i in (`ping -n 1 %computername%`) do set myip=%%i
ECHO.
Echo IP Finder
ECHO -----------------------------------

ECHO My IP Address Is: %MYIP%
Echo.
ECHO My Computer Name Is: %COMPUTERNAME% 
ECHO -----------------------------------
@pause

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of maytrix
maytrix
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Qlemo
When I try it returns my IPv6 address (a very long hexadecimal number). To correct that, you need to force ping to act on IPv4 only.
@echo off
REM myip.bat
REM script to return just the IP address of the current host.
REM Works on Windows 2000 / Windows XP
COLOR 4f
REM TYPE "FOR /?" AT THE COMMAND PROMPT TO GET DETAILS REGARDING THIS COMMAND LINE.
REM NOTE THE SINGLE BACK QUOTES ARE THE ONES BELOW THE ~ KEY ON THE KEYBOARD.
FOR /F "usebackq tokens=2 delims=[]" %%i in (`ping -4 -n 1 %computername%`) do set myip=%%i
ECHO.
Echo IP Finder
ECHO -----------------------------------

ECHO My IP Address Is: %MYIP%
Echo.
ECHO My Computer Name Is: %COMPUTERNAME% 
ECHO -----------------------------------
@pause

Open in new window

Try this one....
@echo off
REM myip.bat
REM script to return just the IP address of the current host.
REM Works on Windows 2000 / Windows XP
COLOR 4f
REM TYPE "FOR /?" AT THE COMMAND PROMPT TO GET DETAILS REGARDING THIS COMMAND LINE.
REM NOTE THE SINGLE BACK QUOTES ARE THE ONES BELOW THE ~ KEY ON THE KEYBOARD.
FOR /F "usebackq tokens=2 delims= " %%i in (`nslookup %computername%`) do set myip=%%i
ECHO.
Echo IP Finder
ECHO -----------------------------------

ECHO My IP Address Is: %MYIP%
Echo.
ECHO My Computer Name Is: %COMPUTERNAME% 
ECHO -----------------------------------
@pause

Open in new window

The nslookup change could work but if there were any dns issues it could fail.  Just tested it myself and I am on a domain that I am not a member of, so it actually fails for me.  Most cases it would probably be fine, unless DNS didn't get updated or was outdated for some reason in which case it could give an incorrect address.  

The change I suggested above works since this causes it to ping the IPv4 address not the IPv6 address which is what it does by default.
Question - why not load BGINFO on all PCs so that information is always available on the Desktop?

If you insist on using a batch file, then I'd suggest replacing your FOR line with mine:
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "IPv4"') do @set myip=%%a

Open in new window

maytrix,
I agree in that the nslookup variant is more prone to failures.
Your approach is resulting in the correct output, put that is more out of luck. -v uses a specific Type Of Service (and TOS is no type of service) for ICMP, e.g. to test traffic priorisation of VoIP over other traffic. Incidentally the TOS field usage forces IPv4 to be used. However, the correct ping parameter is -4.

leew,
That is another good approach, not needed a ping. However, I don't think there are any advantages in using either approach. But the ipconfig way is more straight forward.
Youre both right... Was having a brain fart on the ':' as a delim, and it would get past the error, so I improvised......   :-)



Thanks for the correction Qlemo..  

Another option I thought of was "ipconfig /all | find "IPv4""  
Only problem is it returns multiple IP addresses if they exist and doesn't look quite as nice on the output.  But it is effective if the goal is to have someone run it to read off the address.
Avatar of Tonygret

ASKER

Worked perfectly and also works with Win 7 and XP so I don't need two versions. Thank you very much!
Did you read my comment http:#a34251183, too?