Looping batch ping with results

April 16, 2011 at 07:52:06
Specs: Windows 7
I have a batch program that seems to be giving me false possitives and doesn't appear to be looping. Can anyone please evaluate and tell me what is worong with the following:

:: “Set Variables”
for /f “tokens=*” %%I in (IPlist.txt) do @Set $IP=%%I
@Set OutputFile=PingResults.log

:: “Set Time stamp”
@FOR /F “tokens=*” %%* IN (’date/t’) Do @(set $dt=%%*)
@FOR /F “tokens=*” %%* IN (’time/t’) Do @(set $tm=%%*)

:: “Ping Computer”
@start/b/w PING %1 -a %$IP% -n 2 -w 100>”temp.txt”
@FOR /F “tokens=2,3 delims= ” %%A IN (%tmpFile%) DO @IF “%%B”=="[%$IP%]” SET $PC=%%A ||@rem

:: “Get Status”
@Set $Status=Off-Line
@IF EXIST “temp.txt” @GoTo READSTATUS
@Set $Status=Ping-Error
@GoTo RESULTS

:READSTATUS
@Set errorlevel=
@Type “temp.txt” | Find “TTL=" >nul
@IF Not errorlevel = 1 @Set $Status=On-Line

:RESULTS
@echo results: %$dt% %$tm% %$PC%[%$IP%] ^= %$Status% >>”%OutputFile%”


See More: Looping batch ping with results

Report •


#1
April 16, 2011 at 20:22:29
for /f “tokens=*” %%I in (IPlist.txt) do @Set $IP=%%I
This, for all practical purposes, sets $IP to the last line. The easiest fix would be to change the line to the following lines:
for /f “tokens=*” %%I in (IPlist.txt) do call :loop %%I
goto :EOF
:loop
Set $IP==%1

@start/b/w PING %1 -a %$IP% -n 2 -w 100>”temp.txt”
@FOR /F “tokens=2,3 delims= ” %%A IN (%tmpFile%) DO @IF “%%B”=="[%$IP%]” SET $PC=%%A ||@rem

You're trying to read and write from the file at the same time. Strange things happen when you do that.

If you're using this script in Win7, it'd be easier to just use PowerShell to ping.

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question