如何在可执行文件运行时让 Windows 批处理文件暂停?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/348849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:49:38  来源:igfitidea点击:

How do I get a windows batch file to pause while an executable runs?

windowsbatch-file

提问by aphoria

I am writing a batch file, in which I call an EXE to execute. Now, statements after the call to the EXE should not execute till the EXE completes its execution. How can I do it in the batch file (on Windows)?

我正在编写一个批处理文件,在其中调用一个 EXE 来执行。现在,调用 EXE 之后的语句在 EXE 完成执行之前不应执行。如何在批处理文件中(在 Windows 上)执行此操作?

回答by aphoria

START /WAIT First.exe
START /WAIT Second.exe

回答by kdmurray

It depends on how the .exe works. I'm afraid I don't have all the technical details or terminology, but some .exe files will return control of the session immediately after they've started, while others won't return control until after the program has terminated.

这取决于 .exe 的工作方式。恐怕我没有所有的技术细节或术语,但一些 .exe 文件会在启动后立即返回对会话的控制,而其他文件则在程序终止后才会返回控制。

The second case is easy as the commands late in the file won't execute until the former have completed, so I'll assume you're facing case #1.

第二种情况很简单,因为文件后面的命令在前者完成之前不会执行,所以我假设您面临的是第 1 种情况。

An easy workaround/hack if the execution takes (approximately) the same amount of time every time it runs is to use a ping command with a delay.

如果执行每次运行花费(大约)相同的时间,一个简单的解决方法/黑客是使用延迟的 ping 命令。

PING 127.0.0.1 -n 1 -w 120000 >NUL

This will force the ping command to run once with a 120000ms (2 min) delay.

这将强制 ping 命令以 120000 毫秒(2 分钟)的延迟运行一次。

There's also a good article on a more complex (but more reliable method) on fpschultze.dewith a much more detailed explanation. In short you query the task list searching for the executable you're waiting for. As soon as it's not there you carry on with the batch file. It also uses the ping method, but in a different manner.

fpschultze.de上还有一篇关于更复杂(但更可靠的方法)的好文章,其中有更详细的解释。简而言之,您查询任务列表,搜索您正在等待的可执行文件。只要它不在那里,您就可以继续处理批处理文件。它也使用 ping 方法,但方式不同。

回答by Toon Krijthe

The statements in a batch file are executed sequentially. So if your batch file looks like:

批处理文件中的语句按顺序执行。因此,如果您的批处理文件如下所示:

first.exe
next.exe

Next is executed if first is completed.

如果 first 完成,则执行 next。

回答by Toon Krijthe

You could use the "start" command with parameter /wait use start /? on commandline for details.

您可以使用带有参数的“start”命令 /wait use start /? 在命令行上了解详细信息。

回答by Raghu

Here is the very simple process. Just keep the following line to wait for 10 secs and then continue the other commands...

这是非常简单的过程。只需保留以下行等待 10 秒,然后继续其他命令...

TIMEOUT /T 10

TIMEOUT /T 10

That's all you have to do. I hope you enjoy my technique.

这就是你所要做的。我希望你喜欢我的技术。

回答by Dean Rather

You can use

您可以使用

PAUSE

In batch scripting, but I don't understand your question.

在批处理脚本中,但我不明白您的问题。