FOR /F issue running from a text file
I seem to experience an issue with the code underneath:
@echo off
FOR /F "tokens=1" %%a IN (logs\reachable.txt) do echo %%a && set ip=%%a && call :process
:process
FOR /F "usebackq tokens=2 delims=[]" %%i in (`ping -n 1 %ip%`) 开发者_运维知识库do set myip=%%i && set ip=
echo %myip%
call :end
:end
exit /b
The code is working fine but the last one in reachable.txt (ping) is displayed twice.
HOME
192.168.1.108
HIVE
192.168.1.100
HIVE-FS
192.168.1.110
Illusion-PC
::1
Ken-PC
192.168.1.20
192.168.1.20
Does anybody have an idea how to resolve this?
Thanks in advance.
Regards, Illusion
It's because, once your first for
loop is done, it's dropping through to the code beneath.
Replace:
@echo off
FOR /F "tokens=1" %%a IN (logs\reachable.txt) do echo %%a && set ip=%%a && call :process
:process
rem will be called from 'for' above AND when 'for' is done
with something like:
@echo off
FOR /F "tokens=1" %%a IN (logs\reachable.txt) do echo %%a && set ip=%%a && call :process
goto :eof
:process
rem will be called from 'for' above but NOT after 'for' is done
so that it doesn't drop through.
精彩评论