20秒后停止执行批处理文件,然后移至下一步 [英] Stop Execution of Batch File after 20 Seconds and move to Next

查看:104
本文介绍了20秒后停止执行批处理文件,然后移至下一步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从网上得到的小脚本.该脚本基本上从文件中读取信息(IP地址)并对其进行ping操作,然后将结果插入文本文件中.该脚本完全可以满足我的需要,但问题是,我需要在脚本中更改"ping"而不是ping.问题在于,在路径转换过程中,如果有延迟,则脚本会停留大约3分钟或5分钟,具体取决于响应,然后再移至下一个IP地址.

I have a small script that I got from the net. This script basically reads the information (ip addresses) from a file and pings them and then inserts the results in a text file. This script does exactly what I need but the issue is that instead of ping I need "pathping" which I can change in the script. The issue is that during pathping if there is a delay the script sits for like 3 minutes or maybe 5 minute depending on the response before moving to the next IP address.

我想要的只是某种超时,大概是20秒,无论响应是什么,都移至下一条记录.有人可以修改下面的脚本,以便路径命令等待30秒左右,然后移至下一行信息(与响应内容无关).任何提示或方向将不胜感激...谢谢.

All I want is some kind of time out basically maybe like 20 sec and move to the next record regardless of what the response is. Can someone please modify the script below so that the pathping command waits for like 30 sec and then move to the next row of information (independent of what the response is). Any tips or direction would be appreciated…Thanks.

我已检查超时-w或-p选项不起作用.

I have checked the timeout -w or -p option does not work.

@echo off
cls
echo PathPing test in progress...

for /F %%i in (iplist.txt) do pathping %%i >> result.txt

echo .
echo .
echo Result is ready.    

非常感谢您.

推荐答案

我知道这是一篇过时的文章,但希望以后能对其他人有所帮助.下面的脚本将从iplist.txt文件中获取每个地址,并且将pathping每个地址仅保留20秒钟.然后将每个结果输出到result.txt文件.

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.

现在由于pathping命令的工作方式,停止"它的唯一方法是终止CMD窗口本身.由于批处理代码的原因,一次不能运行两行(例如,与C ++不同),因此我们可以使用start "" cmd /C ""命令召唤两个新窗口.

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.

对于每个地址,我们召唤一个pathping窗口和一个taskkill窗口. Taskill窗口将使用路径窗口的Task PID #,并等待20秒才能终止.

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.

同时,核心批处理文件将等待40秒,所有文件都将关闭并关闭.然后它将所有数据合并到result.txt文件中.

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

这篇关于20秒后停止执行批处理文件,然后移至下一步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆