Start W Does Not Continue After the Program Completes Batch

  1. Use /WAIT to Wait for a Command to Finish Execution
  2. Use the TIMEOUT Command to Delay the Execution
  3. Use the PAUSE Command to Pause the Execution

There are multiple commands and installation processes in a Batch file that usually take some time to complete. But when a Batch file is run, it does not wait for a command process to finish; it executes all commands line by line.

It is important to make those commands wait for them to finish and then execute the next commands. For a process to wait until it is finished, we use the /wait parameter with the START command.

Instead of starting a command, if there is a need to insert delays in the Batch file for some time interval, we can use commands such as TIMEOUT and PAUSE to stop the execution of the next process for a short interval of time or until a key is pressed.

This tutorial illustrates different ways to wait for a command or a program to finish before executing the next command.

Use /WAIT to Wait for a Command to Finish Execution

When we start a program in a Batch file using the START command, we can wait until the program is finished by adding /wait to the START command. Even if there are multiple commands, /wait can be used for each process to finish and move to the next one.

Also, the parameter /B is used to stay in the same process without creating a new window. The START command without the /B parameter opens the program or command in a new window.

Wait for a Command to Finish Execution

For example, we need to wait for a command to finish execution 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                  

wait for a command to finish

Output:

output cmd

Wait for the .exe File to Finish Execution

Another example is where we need to run a .exe file and wait until the execution is done completely.

          @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                  

wait for an exe to finish

Output:

output cmd waiting for an exe file to finish

As soon as you close the .exe file, the second program will begin execution. cmd /k in the last line is used to prevent the command prompt from exiting after execution.

If there are multiple programs, you can use /WAIT with each command to wait until the execution is finished. The START command with the /WAIT parameter doesn't have any timeout, i.e., it does not matter how long the process will take to finish; it will wait until the process is completed.

          @echo off START /WAIT install1.exe START /WAIT install2.exe                  

The /WAIT can only be used with the START command. We can insert a time delay for other commands by using the TIMEOUT and PAUSE commands.

Use the TIMEOUT Command to Delay the Execution

The TIMEOUT command is used to delay the execution of a command for a few seconds or minutes. It can only be used in a Batch file.

The range for the TIMEOUT command varies between -1 and 100000. If the delay is set to -1, it will act as a pause command to wait until a key is pressed.

As in the above command, we can replace the /wait by inserting the TIMEOUT command with the /t parameter. The syntax for the TIMEOUT command is given below:

          TIMEOUT /t <time>                  

Let's take the above example and add a time delay of 30 seconds after the execution of the first program. The code for the same is shown below.

          @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                  

testfile timeout command

Output:

output timeout command

After 30 seconds, the second program will begin execution. Also, if a user presses a key before the timeout, the second program will begin execution.

output timeout command after execution

To prevent user keystrokes, use the /nobreak parameter with the TIMEOUT command. This will ignore any key presses by the user.

Although, you can stop the delay by pressing the Ctrl+C which will raise the errorlevel1.

Use the PAUSE Command to Pause the 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 until a process is finished.

However, there is no timeout, and it will only continue 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                  

testfile pause command

Output:

output pause command

All the methods mentioned above work fine. If you use the START command, it is recommended to use /wait instead of delay commands as the process may take longer than specified.

wallsbelve1993.blogspot.com

Source: https://www.delftstack.com/howto/batch/batch-file-wait-for-command-to-finish/

0 Response to "Start W Does Not Continue After the Program Completes Batch"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel