如果没有在 x 秒内完成,如何使 bat 文件自行中止

How to make a bat file to abort itself if not finished in x seconds

如何使 bat 文件在 x 秒内未完成时自行中止。

在这个脚本中,有些操作有时会卡住,如果超过x秒我希望整个脚本退出(1)

在您的 Bat 启动时生成一个监控进程。 您必须为将在 X 秒后终止的进程设置 Title

@echo on

::The Endtime in seconds
set "$Time=5"

::The title of the process to kill after [Endtime]
set "$Title=test"

title %$Title%

::Generating the monitoring process
(
echo timeout %$Time%
echo taskkill /F /FI "WINDOWTITLE eq %$Title%" /T) >timer.bat

::Running the monitoring Process in another thread
start timer.bat


::Here come your code

Pause

我只是把 pause 用来模拟你的代码,用你的代码替换它。