Waiting for commands in a Windows batch file to finish executing
There are multiple commands and installation processes in a batch file, which usually takes some time to complete. However, when a batch file is run, it does not wait for the command process to complete; it executes all the commands line by line.
It is very important to have these commands wait for them to complete and then execute the next command. For the process to wait for completion, we /wait
use the parameter with the START command.
If we need to insert a delay at a certain interval in a batch file, instead of starting a command, we can use commands such as TIMEOUT and PAUSE to stop the execution of the next process for a short period of time or until a key is pressed.
This article explains different ways to wait for a command or program to complete before executing the next command.
Use /WAIT to wait for the command to finish executing
When we start a program in a batch file using the START command, we can wait for the program to finish by adding /wait to the START command. Even if there are multiple commands, /wait can be used to let each process finish and move on to the next one.
Additionally, the /B parameter is used to stay in the same process and not create a new window. The START command without the /B parameter opens the program or command in a new window.
Wait for the command to finish executing
For example, we need to wait for one command to finish before running the next one.
@echo off
echo starting first program.
START /B /WAIT cmd /c "C:\Users\Aastha Gas Harda\Desktop\testfile1.bat" > output.txt
echo The first program is executed successfully.
START /B systeminfo >> output.txt
echo All the programs are executed successfully
cmd /k
Output:
Wait for the .exe file to finish executing
Another example is when we need to run an .exe file and wait for the execution to fully complete.
@echo off
echo starting first program.
START /B /WAIT JRuler.exe
echo The first program is executed successfully.
START /B systeminfo >> output.txt
echo All the programs are executed successfully
cmd /k
Output:
Once you close the .exe file, the second program will start executing. The cmd /k in the last line prevents the command prompt from exiting after execution.
If you have multiple programs, you can use /WAIT in each command to wait for execution to complete. The START command with the /WAIT parameter does not have any timeout, i.e. it does not matter how long the process takes to complete; it will wait for the process to complete.
@echo off
START /WAIT install1.exe
START /WAIT install2.exe
/WAIT
Can be used only with START command. We can use TIMEOUT and PAUSE commands to insert time delay for other commands.
Delaying execution using the TIMEOUT command
TIMEOUT
The command is used to delay the execution of a command for a few seconds or minutes. It can be used only in batch files.
TIMEOUT
The command ranges from -1 to 100000. If the delay is set to -1, it will act as a pause command, waiting for a key press.
As in the above command, we can replace /wait by inserting the TIMEOUT command with the /t parameter. The syntax of the TIMEOUT command is as follows:
TIMEOUT /t <time>
Let's take the above example and add a 30 second delay after the first program is executed. The code is as follows.
@echo off
echo starting first program.
START /B JRuler.exe
TIMEOUT /t 30
echo The first program is executed successfully.
START /B systeminfo >> output.txt
echo All the programs are executed successfully
cmd /k
Output:
After 30 seconds, the second program will begin executing. Also, if the user presses a key before the timeout, the second program will begin executing.
To prevent the user from typing, use the TIMEOUT parameter with the command /nobreak
. This will ignore any keys pressed by the user.
However, you can Ctrl+C
stop the delay by pressing , which raises errorlevel 1.
Use the PAUSE command to pause execution
The PAUSE command is used to pause the execution of a batch file until a key is pressed. It is useful if the user wants to read the output text or wait for the process to complete.
However, there is no timeout, and it only lasts until the user presses a key.
@echo off
echo starting first program.
START /B cmd /c "C:\Users\Aastha Gas Harda\Desktop\testfile1.bat" > output.txt
echo The first program is executed successfully.
PAUSE
START /B systeminfo >> output.txt
echo All the programs are executed successfully
cmd /k
Output:
All the methods mentioned above will work fine. If you use the START command, it is recommended to use /wait
instead of the DELAY command because the process may take longer than the specified time.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
在 JavaScript 中等待 X 秒
Publish Date:2024/03/17 Views:84 Category:JavaScript
-
本教程介绍了如何在 javascript 中等待 x 秒。
在 PowerShell 中等待每个命令完成
Publish Date:2024/03/03 Views:284 Category:编程语言
-
本文将解释如何在 PowerShell 中继续执行下一个命令行之前等待每个命令完成的多种方法。
等待线程在 C# 中完成
Publish Date:2024/01/20 Views:94 Category:编程语言
-
等待 C# 中的线程完成的主要方法有两种:Task.WaitAll()和 Thread.Join()函数。用 C# 中的 Task.WaitAll() 方法等待线程完成 C# 中的 [Task.WaitAll() 方法)用于等待 Task 类的所有对象的完成。
如何在 Python 中等待用户输入
Publish Date:2023/12/23 Views:214 Category:Python
-
本文演示了如何在 Python 中等待用户按下一个键。本教程向你展示了如何在 Python 中等待按键后再进行其他操作。在 Python 中使用 input() 等待输入
等待 Windows 批处理文件中的命令完成执行
Publish Date:2023/08/16 Views:2346 Category:操作系统
-
本文说明了在执行下一个命令之前等待命令或程序完成的不同方法。使用 /WAIT 等待命令完成执行 当我们使用START命令在批处理文件中启动程序时,我们可以通过在START命令中添加/wait来等待程序
Python 中使用 Selenium 隐式等待
Publish Date:2023/07/03 Views:78 Category:Python
-
selenium 包用于使用 Python 脚本进行自动化和测试。 我们可以使用它来访问网页中的各个元素并使用它们。Python 中使用 Selenium 隐式等待 如果我们尝试获取不可用的元素,则会引发 ElementNotVisibl
在Python中添加Selenium Web Driver等待
Publish Date:2023/07/03 Views:151 Category:Python
-
本文将介绍在Python中在Selenium Web驱动程序中添加等待的示例。Python Selenium Web 驱动程序等待 大多数 Web 应用程序都使用 AJAX 技术。