Reputation: 33
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.
Upvotes: 3
Views: 569
Reputation: 7921
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:
_ip
Example usage and output:
F:\test>ipconfig | findstr /i "ipv4"
IPv4 Address. . . . . . . . . . . : 192.168.42.78
F:\test>GetIPAddress
192.168.42.78
Upvotes: 3