Windows7 Batch, how to break after first For loop

2019-02-20 15:48发布

I'm writing a simple script to retreive my localhost address given as IP.

To get my IPv4 address (Win7) I've written simple FOR loop, but as a result i get the IP from last loop instead of first one.

Here is the batch code:

cls
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do (
  set ip=%%b
)
set ip=%ip:~1%
echo %ip%

this returns:

(set ip= 192.168.1.101 )
(set ip= 192.168.88.1 )
(set ip= 192.168.137.1 )
set ip=192.168.137.1
echo 192.168.137.1
192.168.137.1

What i need is the result of first loop:

192.168.1.101

2条回答
不美不萌又怎样
2楼-- · 2019-02-20 16:37

Also code with @aschipfl :SKIP solution

for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do (
  set ip=%%b
  goto :SKIP
)
:SKIP
set ip=%ip:~1%
echo %ip%
查看更多
孤傲高冷的网名
3楼-- · 2019-02-20 16:41

And what about this code without any loop ?

@Echo off
For /f "tokens=2 delims= " %%a in ('arp -a ^|findstr /i "Interface"') do (set IP=%%a)
Echo The IP adress is : %IP%
pause
查看更多
登录 后发表回答