MBanerjee
MBanerjee

Reputation: 207

Batch file not saving result in logs

I am using below batch file to query a list of IPs and then save it to LOG.txt.

@echo off 
cls 
for /f "tokens=*" %%x in (IP.txt) do ( 
echo Checking %%x 
ping -n 1 %%x > nul 
if not errorlevel 1 ( 
echo %%x >> LOG.txt 
) 
)

But I am seeing only first enrty of IP.txt in LOG.txt

Although while running batch file I am seeing

Checking 1.2.3.4
Checking 1.2.3.5

etc.. So its implying that batch file is reading the IP.txt line by line.

Can anyone help to make this batch file such that output in LOG.txt is working as expected.

Upvotes: 0

Views: 302

Answers (1)

dbenham
dbenham

Reputation: 130829

What Andriy M is alluding to in his comment is your code will only write the IP address if PING was successful.

Because of your IF statement, the IP address will not be written if there was an error. PING will generate an error if there was a timeout, or if PING could not find the host.

You need to change your logic if you want all addresses to be written.

Upvotes: 1

Related Questions