在 bat 文件中的给定时间后结束命令
End a command after a given time in a bat file
我有一个bat文件执行多个程序,有可能一个程序一直在循环。我想在 1 分钟后终止该程序的执行并执行下一个程序。
我要执行的程序 gif_0.exe
、gif_1.exe
、...从 txt 接收输入并将输出写入另一个 txt。
gif_0.exe input1.txt output1_0.txt
timeout /t 20
gif_0.exe input2.txt output2_0.txt
timeout /t 20
gif_1.exe input3.txt output3_0.txt
timeout /t 20
gif_2.exe input4.txt output4_0.txt
timeout /t 20
gif_3.exe input5.txt output5_0.txt
start "Title goes here" gif_0.exe input1.txt output1_0.txt
timeout /t 60 /nobreak>nul
taskkill /F /IM gif_0.exe
应该可以解决问题。根据您的需要重复。
解释:
start
您的应用程序具有给定的参数
在倒计时之前等待 60 秒,没有停止计时器的选项,也没有任何输出。
使用您的应用程序的图像名称终止任务。如果您可能有多个此并行实例 运行 这将杀死所有实例!
不过,您可以使用过滤器来缩小范围;看看 taskkill /?
我不确定你的问题是否有错字,但假设你的应用程序 gif_n.exe
参数 inputn+1.exe outputn+1.exe
您可以执行以下操作:
@echo off
setlocal EnableDelayedExpansion
for /l %%n in (0, 1, n) do (
set /a foo=%%n+1
start gif_%%n.exe input!foo!.txt output!foo!_0.txt
timeout /t 60 /nobreak>nul
taskkill /F /IM gif_%%n.exe
)
这将在单个步骤中从 0 到 n,并在循环中执行与上面相同的事情。
此处新增 2 条:
需要行 setlocal EnableDelayedExpansion
才能激活在封闭的括号块(如 for 循环或 if 条件)中更改变量值后使用变量值的可能性。
set /a foo=%%n+1
会将变量 foo
的值设置为 %%n+1
的值。需要开关 /a
来执行实际计算而不是附加字符串。
要在我们的 for 循环中使用变量,我们必须使用 DelayedExpasion
(见上文):
只需将您通常使用的 %
更改为 !
即可。
为确保一切正常,您可能需要在三个主线前放置一个 echo。
如有不明之处欢迎提问:)
我的想法和Geisterfurz007类似。
但是我的批处理并行启动 exe,并且还以启动的 exe 的名称作为 arg 的另一个批处理实例。新实例检查 arg 并跳转到等待超时的子程序并尝试终止 exe。
@echo off
setlocal EnableDelayedExpansion
Set Wait=60
If "%1" Neq "" Goto :KillTask
for /l %%n in (0, 1, n) do (
set /a foo=%%n+1
start gif_%%n.exe input!foo!.txt output!foo!_0.txt
Start %~f0 gif_%%n.exe
)
Goto :Eof
:KillTask %1
timeout /t %Wait% /nobreak>nul
taskkill /F /IM %1
我有一个bat文件执行多个程序,有可能一个程序一直在循环。我想在 1 分钟后终止该程序的执行并执行下一个程序。
我要执行的程序 gif_0.exe
、gif_1.exe
、...从 txt 接收输入并将输出写入另一个 txt。
gif_0.exe input1.txt output1_0.txt
timeout /t 20
gif_0.exe input2.txt output2_0.txt
timeout /t 20
gif_1.exe input3.txt output3_0.txt
timeout /t 20
gif_2.exe input4.txt output4_0.txt
timeout /t 20
gif_3.exe input5.txt output5_0.txt
start "Title goes here" gif_0.exe input1.txt output1_0.txt
timeout /t 60 /nobreak>nul
taskkill /F /IM gif_0.exe
应该可以解决问题。根据您的需要重复。
解释:
start
您的应用程序具有给定的参数
在倒计时之前等待 60 秒,没有停止计时器的选项,也没有任何输出。
使用您的应用程序的图像名称终止任务。如果您可能有多个此并行实例 运行 这将杀死所有实例!
不过,您可以使用过滤器来缩小范围;看看 taskkill /?
我不确定你的问题是否有错字,但假设你的应用程序 gif_n.exe
参数 inputn+1.exe outputn+1.exe
您可以执行以下操作:
@echo off
setlocal EnableDelayedExpansion
for /l %%n in (0, 1, n) do (
set /a foo=%%n+1
start gif_%%n.exe input!foo!.txt output!foo!_0.txt
timeout /t 60 /nobreak>nul
taskkill /F /IM gif_%%n.exe
)
这将在单个步骤中从 0 到 n,并在循环中执行与上面相同的事情。
此处新增 2 条:
需要行 setlocal EnableDelayedExpansion
才能激活在封闭的括号块(如 for 循环或 if 条件)中更改变量值后使用变量值的可能性。
set /a foo=%%n+1
会将变量 foo
的值设置为 %%n+1
的值。需要开关 /a
来执行实际计算而不是附加字符串。
要在我们的 for 循环中使用变量,我们必须使用 DelayedExpasion
(见上文):
只需将您通常使用的 %
更改为 !
即可。
为确保一切正常,您可能需要在三个主线前放置一个 echo。
如有不明之处欢迎提问:)
我的想法和Geisterfurz007类似。 但是我的批处理并行启动 exe,并且还以启动的 exe 的名称作为 arg 的另一个批处理实例。新实例检查 arg 并跳转到等待超时的子程序并尝试终止 exe。
@echo off
setlocal EnableDelayedExpansion
Set Wait=60
If "%1" Neq "" Goto :KillTask
for /l %%n in (0, 1, n) do (
set /a foo=%%n+1
start gif_%%n.exe input!foo!.txt output!foo!_0.txt
Start %~f0 gif_%%n.exe
)
Goto :Eof
:KillTask %1
timeout /t %Wait% /nobreak>nul
taskkill /F /IM %1