This article shows you how to delay the execution of a batch file and manage the timing of various actions without it being executed immediately after being opened. Depending on the programmer's needs, there are several commands that can be used to delay the execution of a batch file. Before attempting to use the commands described in this article, you should be fully familiar with how to code a batch file.
Steps
Step 1. Enter the "Start" menu by clicking the
| techicon | x30px]. It features the Windows logo and is located in the lower left corner of the desktop.
If the batch file you want to delay the execution already exists, select its icon with the right mouse button, then choose the option Edit from the contextual menu that will appear to view the code in the "Notepad" program. In this case, skip the next two steps.
Step 2. Start the "Notepad" program
Type notepad keywords into the "Start" menu. Your computer will search your computer for the Windows "Notepad" program. Now select the relevant icon from the results list.
Step 3. Create a batch file
Start coding your file. Normally it starts with the following line of text
@echo off
then add the rest of the code as needed.
Step 4. Determine how you want to manage the timing of the batch file execution
You can refer to three main commands:
- PAUSE - the execution of the file will be interrupted until the user presses a key on the keyboard (for example the space bar);
- TIMEOUT - the execution of the code inside the batch file will be interrupted for the indicated number of seconds (or until a key is pressed), after which it will resume automatically;
- PING - program execution will be interrupted until a response is received from the computer address indicated in the code. Under normal conditions, using this command, program execution is interrupted for a few milliseconds, provided that the address of the computer indicated in the "ping" command is working.
Step 5. Select the point in the code where to insert the break execution command
You can delay or interrupt the execution of the program at any point (for example after the command "Exit" if present). Scroll through the code to the point where you need to enter your chosen command, then create a blank line where the code execution will stop and before the point where it should resume.
Step 6. Type the command you have chosen to use
According to your needs, choose one of the following options:
- PAUSE - type the command pause. In this case you don't need to add any parameters;
-
TIMEOUT - type the command timeout [time] where the parameter "time" indicates the number of seconds that the program will have to wait before being able to resume normal execution. For example, by entering the following timeout code 30, the execution of the batch file will be temporarily interrupted for 30 seconds;
If you want to prevent users from resuming program execution by pressing a key on the keyboard use the following code timeout [time] / nobreak (where the "time" parameter indicates the number of seconds the program will have to wait before it can resume normal execution)
- PING - type in the ping command [address] where the "address" parameter represents the IP address of the computer or website to be contacted by the "ping" command.
Step 7. Save the document as a batch file
If it is a newly created program follow these instructions:
- Access the menu File, then choose the option Save with name…;
- Rename the file by adding the.bat extension at the end (for example "file_batch_di_test.bat").
- Select the "Save as type" drop-down menu, then select the option All files (*. *).
- At this point choose the folder in which to store it and press the button Save.
Advice
- Batch files can only be run on Windows platforms by simply selecting them with a double click of the mouse.
- The use of the "PAUSE" command is recommended only when user intervention is required, who must choose the action to be performed based on those proposed. While the "TIMEOUT" command is ideal for managing the automatic execution of a batch file.
Warnings
- The "SLEEP" command does not work when run on computers with Windows 10.
- Batch files cannot be run on Macs.