我正在尝试创建一个从基本CMD命令中获取变量的批处理程序。 例如:
c:\Users> ipconfig
.....
IPv4 Address ....... 123.456.7.89
.....
假设我想制作一个仅在屏幕上打印IP地址的批处理程序,例如:
@echo off
echo (IP Address Variable Here)
pause;
我需要做什么?
感谢您的时间。
答案 0 :(得分:3)
使用以下批处理文件。
<强> 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
注意:
_ip
示例用法和输出:
F:\test>ipconfig | findstr /i "ipv4"
IPv4 Address. . . . . . . . . . . : 192.168.42.78
F:\test>GetIPAddress
192.168.42.78