I know this is an old post but I hope I can help others in the future. The script bellow will take each address from the iplist.txt
file and will pathping
each address for only 20 seconds. Each result will then be outputted to the result.txt
file.
Now due to how the pathping
command works, the only way to "stop" it is by terminating the CMD window it's self. Since you cannot have two lines running at a time due to how batch code is (Unlike C++ for example) we can summon two new windows using the start "" cmd /C ""
command.
For each address we summon one pathping
window and one taskkill
window. The taskill window will use the Task PID #
of the pathping window and wait 20 seconds before terminating it.
Mean while, the core batch file will wait 40 seconds for all the files to wright and close. It will then combine all the data into the result.txt
file.
@ECHO OFF
@SETLOCAL enableextensions enabledelayedexpansion
@TITLE PathPing from textfile, cancel request for each x after 20s.
@GOTO :Start
:Start
echo WARN: Starting PathPing Script...
echo WARN: This may take some time...
goto :PathPing
:PathPing
:: Large number is for token gathering
set /a number=123456789
for /f "delims== tokens=1,2" %%G in (iplist.txt) do (
:: Start The pathping's for each %%G
start "!number!" cmd /C "pathping "%%G" >> "!number!.outputfile""
:: Start Kill Window (Let process live for 20 seconds)
FOR /F "tokens=2" %%# in ('tasklist /v ^| find "!number!" ^| find "Console"') do set PID=%%#
start "" cmd /C "(PING localhost -n 20 >nul) & (taskkill /pid !PID! /t /f & goto :eof)"
set /a number+=1
)
:: End of FOR statement. Now copy .outputfile to one file.
:: Wait 60 Seconds for the FOR statement to finish copy there files.
PING localhost -n 30 >nul
:: DEL is optional, remove it if you don't with to over-wright text file.
If exist "%~dp0\result.txt" (goto :Del) ELSE (goto :Skip)
:Del
Del /Q "%~dp0\result.txt"
Goto :Skip
:Skip
For %%A In (*.outputfile) DO (for /f "delims== tokens=1,2" %%G in (%%A) do echo %%G >> result.txt)
:: Clean up all .outputfile files.
For %%A In (*.outputfile) DO (DEL /Q "%~dp0\%%A")
Goto :Finish
:Finish
cls
echo WARN: Result is ready.
echo(
pause
goto :eof