1

I need to figure out how to solely display the IP Address of a computer for my batch file. I don't want the one where it displays IP Address ................."%IP Address%

I need it to say something similar to this.

set time=&time/t
set computername=%computername%
set IP=%IP Address%
echo.
echo.
echo.
%time% %computername% %IP Address%

so if my time was 9:13PM my computername is tracckkk1 and my IP Address was 14.14.14.14 then it would say

9:13PM tracckkk1 14.14.14.14

and not

21:13:51 tracckkk1 IP Address.. ...............14.14.14.14

thanks to anyone that is answering this question!

abatishchev
  • 98,240
  • 88
  • 296
  • 433
cmd
  • 563
  • 3
  • 10
  • 26
  • Sorry if this is a silly question, I would just like to clarify one thing. When you said, *‘I don't want the one where it displays `IP Address ................."%IP Address%’`*, did you mean that you don't want the IP address to be formatted like that or that you want a different IP address (not from that line)? Also, is the line supposed to come from `ipconfig`? – Andriy M Feb 10 '13 at 03:38
  • i dont want the ip address to display that line i want it to only show my ip address so i believe that the answer to your question would be i want it to be formatted differently – cmd Feb 10 '13 at 05:23
  • possible duplicate of [How do I get the IP address into a batch-file variable?](http://stackoverflow.com/questions/5898763/how-do-i-get-the-ip-address-into-a-batch-file-variable) – UnhandledExcepSean Feb 11 '13 at 00:57

2 Answers2

4

Following will work

    @echo off
    setlocal enabledelayedexpansion
    set /p hostname=Enter hostname: 
    for /f "tokens=3" %%a in ('ping -4 %hostname% ^| find "Pinging"') do (
    set ip=%%a
    set ip=!ip:[=!
    set ip=!ip:]=!
    echo %date% %computername% !ip! 
    )
Shirulkar
  • 484
  • 2
  • 4
0

The following will capture the IP address of the computer in an environment variable named IP_ADDRESS.

(Note: the value of IP_ADDRESS will have a leading space.)

for /f "usebackq delims=: tokens=2" %%i in (`ipconfig ^| findstr Address`) do (
set IP_ADDRESS=%%i
)

Here is documentation for the For keyword.

Update: There may be problems with this approach. See this question.

Community
  • 1
  • 1
DavidRR
  • 18,291
  • 25
  • 109
  • 191