Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 20秒后停止批处理文件的执行,并移动到下一步_Windows_Batch File_Command - Fatal编程技术网

Windows 20秒后停止批处理文件的执行,并移动到下一步

Windows 20秒后停止批处理文件的执行,并移动到下一步,windows,batch-file,command,Windows,Batch File,Command,我有一个从网上得到的小脚本。该脚本基本上从文件中读取信息(ip地址)并ping它们,然后将结果插入文本文件中。这个脚本正是我所需要的,但问题是,我需要的不是ping,而是可以在脚本中更改的“pathping”。问题是,在路径选择过程中,如果存在延迟,脚本将根据移动到下一个IP地址之前的响应而停留大约3分钟或5分钟 我想要的只是一些暂停时间,基本上可能是20秒,然后移动到下一个记录,不管响应是什么。是否有人可以修改下面的脚本,使pathping命令等待30秒,然后移动到下一行信息(与响应无关)。任

我有一个从网上得到的小脚本。该脚本基本上从文件中读取信息(ip地址)并ping它们,然后将结果插入文本文件中。这个脚本正是我所需要的,但问题是,我需要的不是ping,而是可以在脚本中更改的“pathping”。问题是,在路径选择过程中,如果存在延迟,脚本将根据移动到下一个IP地址之前的响应而停留大约3分钟或5分钟

我想要的只是一些暂停时间,基本上可能是20秒,然后移动到下一个记录,不管响应是什么。是否有人可以修改下面的脚本,使pathping命令等待30秒,然后移动到下一行信息(与响应无关)。任何提示或指导将不胜感激…谢谢

我已经检查了timeout-w或-p选项不起作用

@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
文件中获取每个地址,并将
pathfing
每个地址仅持续20秒。然后将每个结果输出到
result.txt
文件

@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
现在,由于
pathping
命令的工作方式,要“停止”它,唯一的方法是终止CMD窗口,它本身就是这样。由于批处理代码是不同的,所以不能同时运行两行(例如C++),所以我们可以使用<代码>开始“CMD/C”“命令”/< P>来调用两个新窗口。 对于每个地址,我们调用一个
pathping
窗口和一个
taskkill
窗口。taskill窗口将使用路径窗口的Task
PID
,并等待20秒后终止

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

@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

当你使用
-w
,比如
-w 30000
,持续30秒时会发生什么情况。@BaliC:在内置的帮助中,它说:“-w timeout Wait timeout毫秒,等待每个回复。”(我的重点)我怀疑
-w 30000
会使实际延迟更长。@amirsd:这是一个非常粗略的想法。使用
start
命令运行
pathping
,然后立即运行另一个内部循环,检查
pathping
是否已完成(使用类似
tasklist | find“pathping”
)或已过30秒(您只需设置一个计数器并在检查之间使用1秒延迟)。我相信最简单的方法是监视批处理过程是否在运行期间。因此,创建一个新批处理,即“ping localhost-n 20>nul”,然后“tasklist/v | find cmd.exe | find(cmd ex.batch1的标题)”,然后使用“ERRORLEVEL”so“if%ERRORLEVEL%LSS 1 taskkill cmd.exe”循环该批处理