Illusion
Illusion

Reputation: 23

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

Upvotes: 2

Views: 516

Answers (1)

paxdiablo
paxdiablo

Reputation: 881293

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.

Upvotes: 1

Related Questions