Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

how to display local IP using a batch file?

Writer Sebastian Wright

So far I have the following in my batch file:

%comspec% /c ipconfig | find "IPv4" > %HOMEPATH%\desktop\MyIP.txt
%comspec% /c start notepad %HOMEPATH%\desktop\MyIP.txt

However, when I run the batch file, MyIP.txt is empty when displayed.

On the other hand, when I run each line one after another from Windows-Start search field it works.

Any ideas how to fix it?

4 Answers

Works fine from a batch file here.

Couple suggestions:

  • Try wrapping the HOMEPATH file paths in quotes to make up for any potential spaces:
    %comspec% /c ipconfig | find "IPv4" > "%HOMEPATH%\desktop\MyIP.txt"
  • Ensure you are running the batch file as a user who has a valid homepath and desktop (and not as say, the "System" account).
  • Try echo %HOMEPATH% alone in another batch file and launch it the same way, check the output to see if %HOMEPATH% expands to what you are expecting (tip add "pause" to the end of the batch to have it wait for you to hit a key).
7

Save the below given code as batch file in system32 folder.
For Windows 7 keep batch file in C:\Windows\System32 .

@echo.
@echo IP INFORMATION
@echo By:Aswin Sha
@ipconfig/all | find "Subnet Mask"
@ipconfig/all | find "IPv4"
@ipconfig/all | find "Default Gateway"
@ipconfig/all | find "Host Name"
@ipconfig/all | find "DNS Suffix Search List"
@ipconfig/all | find "Physical Address"
@ipconfig/all | find "DHCP Enabled"
@ipconfig/all | find "DHCP Server"
@ping 127.0.0.1
@echo.
@pause

This works fine with Windows 10:

@echo off
for /f "skip=1 delims={}, " %%A in ('wmic nicconfig get ipaddress') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B"
for /f "tokens=1 delims=:" %%j in ('ping %computername% -4 -n 1 ^| findstr Reply') do ( set localip=%%j
)
echo Public IP is: %IP%
echo Local IP is: %localip:~11%

It returns both public and private IP addresses.

Do you have to use a batch file? As per your requirements, the following code saved as GetIP.vbs and executed will launch Notepad and print your IP address(es) in it without saving as a text file first:

Set colItems = GetObject("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=TRUE", , 48)
For Each objItem In colItems If InStr(objItem.IPAddress(0), ":") = 0 Then strIP = "Description: " & objItem.Description & vbCR & "IP Address : " & objItem.IPAddress(0) & vbNewLine End If
Next
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "Notepad"
WScript.Sleep 100
WshShell.AppActivate "Notepad"
WshShell.SendKeys strIP
4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy