Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait in a batch script? [duplicate]

Tags:

batch-file

People also ask

How do you wait 1 second in a batch file?

Type in your command. TIMEOUT — Type timeout time where "time" is replaced by the number of seconds to delay. For example, typing in timeout 30 will delay your batch file for 30 seconds.

Is there a wait command in batch?

The Sleep/Wait Command is a very useful command that is used to pause for a set amount of time while a batch script is being executed. To put it another way, this command makes it easier to run a command for a set amount of time.

How do you delay in CMD?

Syntax TIMEOUT delay [/nobreak] Key delay Delay in seconds (between -1 and 100000) to wait before continuing. The value -1 causes the computer to wait indefinitely for a keystroke (like the PAUSE command) /nobreak Ignore user key strokes.

How do you run a batch file and wait until it finishes?

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.


You can ping an address that doesn't exist and specify the desired timeout:

ping 192.0.2.2 -n 1 -w 10000 > nul

And since the address does not exist, it'll wait 10,000 ms (10 seconds) and return.

  • The -w 10000 part specifies the desired timeout in milliseconds.
  • The -n 1 part tells ping that it should only try once (normally it'd try 4 times).
  • The > nul part is appended so the ping command doesn't output anything to screen.

You can easily make a sleep command yourself by creating a sleep.bat somewhere in your PATH and using the above technique:

rem SLEEP.BAT - sleeps by the supplied number of seconds

@ping 192.0.2.2 -n 1 -w %1000 > nul

NOTE (September 2002): The 192.0.2.x address is reserved as per RFC 3330 so it definitely will not exist in the real world. Quoting from the spec:

192.0.2.0/24 - This block is assigned as "TEST-NET" for use in documentation and example code. It is often used in conjunction with domain names example.com or example.net in vendor and protocol documentation. Addresses within this block should not appear on the public Internet.


You'd better ping 127.0.0.1. Windows ping pauses for one second between pings so you if you want to sleep for 10 seconds, use

ping -n 11 127.0.0.1 > nul

This way you don't need to worry about unexpected early returns (say, there's no default route and the 123.45.67.89 is instantly known to be unreachable.)


I actually found the right command to use.. its called timeout: http://www.ss64.com/nt/timeout.html


I used this

:top
cls
type G:\empty.txt
type I:\empty.txt
timeout /T 500
goto top