I'm trying to make a batch program that fetches variables from base CMD commands. For example:
c:\Users> ipconfig
.....
IPv4 Address ....... 123.456.7.89
.....
Say I wanted to make a batch program that prints only the IP address on the screen such as:
@echo off
echo (IP Address Variable Here)
pause;
What would I need to do?
Appreciate your time.
Use the following batch file.
GetIPAddress.cmd:
@echo off
setlocal enabledelayedexpansion
rem throw away everything except the IPv4 address line
for /f "usebackq tokens=*" %%a in (`ipconfig ^| findstr /i "ipv4"`) do (
rem we have for example "IPv4 Address. . . . . . . . . . . : 192.168.42.78"
rem split on : and get 2nd token
for /f delims^=^:^ tokens^=2 %%b in ('echo %%a') do (
rem we have " 192.168.42.78"
set _ip=%%b
rem strip leading space
set _ip=!_ip:~1!
)
)
echo %_ip%
endlocal
Notes:
_ipExample usage and output:
F:\test>ipconfig | findstr /i "ipv4"
IPv4 Address. . . . . . . . . . . : 192.168.42.78
F:\test>GetIPAddress
192.168.42.78
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With