Article directory
bat
Several common ways to implement delay function in batch processing
1. Use ping
commands to implement delay
Use the ping command to achieve the delay effect, the command is as follows:
ping 127.0.0.1 -n 11 >nul
-n
The specifiedping
number of times, with an interval of about 1s each time,>nul
willping
echo the process information to the shieldping
Delay of command implementation, time precision: about 1s, memory usage:ping
space occupied by commands- Pay attention to the closed pit: ping is almost instantaneous from the first request to receive the echo data, so if you want to achieve a delay of 10 seconds, you need to set -n to 11 (at least for the loopback address of ping 127.0.0.1 ), many on the Internet say how many seconds it takes, and the -n parameter value is set as many seconds, which is really a bit misleading!
- I have also seen from the Internet
-w
that parameters are used to specify the timeout waiting time as the delay time. This is not possible, unless the specified address cannot be pinged, otherwise the delay implementation is not reliable at all.
You can use the script below to verify -n
how much should be set
@echo off
echo s_time:%time%
ping -n 1 127.0.0.1 > nul
echo e_time:%time%
echo s_time2:%time%
ping -n 2 127.0.0.1 > nul
echo e_time2:%time%
pause
Output result:
It can be seen that those -n
people on the Internet who say "set as many seconds as you need to delay" are so misleading. However, when -n
the specified value is larger, the final delay error will be larger, and you may not care about the 1 second error in the end. You can try to set it -n
to a larger value to verify.
(This error actually ping
has a lot to do with your address)
Then pass a script to verify -w
the parameters and whether the corresponding delay is valid
@echo off
echo s_time:%time%
ping -n 10 -w 5000 127.0.0.1 > nul
echo e_time:%time%
pause
According to the error on the Internet, the result here should be a delay of 10*5000/1000=50 seconds; from the test results, the actual delay is only about (10-1) seconds; the specified -w
value does not take effect, if you can guarantee The address being pinged will time out every time (note that it is not a ping failure). -w
The time specified by the parameter will be applied to the delay effect
2. Use timeout
commands to implement delay
The delay effect can also be achieved by using the timeout command. The command is as follows:
timeout /t 5 /nobreak >nul
/t
Specifies the description of the wait, the valid range-1
is up to99999
seconds,-1
which means waiting indefinitely/nobreak
Indicates ignoring the key and waiting for the specified time. The only key that can be received isCtrl+Z
a combination key. If the waiting time is not up,Ctrl+Z
the key will terminate the batch program andtimeout
subsequent commands will not be executed.timeout
The delay implemented by the command, in addition to-1
the infinite wait, the maximum delay99999
is seconds, but you can calltimeout
the command multiple times to achieve a delay waiting beyond this limittimeout
Delay of command implementation, time precision: about 1s, memory usage:timeout
space occupied by commands
3. Use choice
commands to implement delay
The delay effect can also be achieved by using the choice command, the command is as follows:
choice /C yn /T 10 /D y >nul
/C
Specify the option list,/T
specify the waiting time before making the default selection, the acceptable value0-9999
, and/D
the default selection after the waiting time is exceeded;choice
The delay realized by the command is a single maximum delay of9999
seconds, but you can callchoice
the command multiple times to achieve a delay waiting beyond this limit;
3.choice
The delay achieved by the command, time accuracy: about 1s, memory usage:chocie
occupied by the command space
4. Use for
a loop to implement a delay
The delay effect can also be achieved by using for
the loop, to be precise , the combination of for
loop + ping
/ timeout
/ is used choice
to achieve the delay effect.
To achieve 100s delay, the command is as follows:
::for+ping组合
for /l %%i in (1,1,10) do @echo %%i & ping -n 11 127.0.0.1 >nul
::for+timeout组合
for /l %%i in (1,1,10) do @echo %%i & timeout /t 10 >nul
::for+choice组合
for /l %%i in (1,1,10) do @echo %%i & choice /C yn /T 10 /D y >nul
for
+ping
// The combination method can flexibly combine the delay time you want, without being limited bytimeout
the maximum delay / time;choice
timeout
choice
ping
If the command wants to delaym
seconds,-n
the parameter value needs to be set tom+1
, because the firstping
request to receive the value is almost instantaneous;- For the delay of this combination command, I suggest
for
setting the number of cycles as small as possible, and setting the parameter value corresponding to the command that specifically implements the delay task in the loop body as large as possible. Because I understand that each loop is equivalent to restarting theping
// program, and each startup of the program will inevitably take a certain amount of time. When the number of loops is large, the impact of this startup time will be relatively largetimeout
. (In my local test, use the + combination command to achieve a 100s delay, loop 1 time + the actual time-consuming is about 101s, and loop 100 times + the actual time-consuming is about 102s, the latter is about 1s longer than the former)choice
for
ping
for
ping -n 101
for
ping -n 2
5. Use sleep
commands to implement delay
Windows batch
There are no native sleep
commands in , and third-party tools are required.
downloadsleep.exe
curl https://www.computerhope.com/download/utility/sleep.exe > sleep.exe
Put the downloaded sleep.exe
program in C:\Windows\System32
or other PATH
paths in the environment variable, and it can be used globally.
To achieve a delay of 10s, the use is relatively simple:
sleep 10
Time accuracy: 1 millisecond
Memory usage: the sleep command itself is very small
6. Use VBScript.sleep
implementation delay
In batch processing, vbscript
the delay function can be realized by calling
1) Create a vbs script file (such as delay.vbs), the content is as follows:
WScript.Sleep WScript.Arguments.Item(0)
2) Call this in batch script vbscript
to achieve delay
cscript //nologo delay.vbs 1000 >nul
It means that the parameter 1000 is passed in when calling the delay.vbs script, which means waiting for 1000 milliseconds, that is, 1 second.
cscript
Time accuracy: 1 millisecond; memory usage: the vbs script itself is not large, and the execution / process of vbswscript
takes up about 1MB of memory;- To call a vbs script, you can also use
wscript
commands, such aswscript delay.vbs 1000 >nul
- Need to pay attention to the path of script files and batch script files
You can also use a temporary vbscript script to avoid path problems, as follows:
echo WScript.Sleep WScript.Arguments.Item(0) > delay.vbs
cscript //nologo delay.vbs 1000 >nul && del delay.vbs
That is, generate vbs under the current path of the batch file, and delete it when it is used up.
Summarize
- Except
sleep.exe
for third-party tools, which need to be downloaded and installed, the rest are built-in with Windows and can be used directly; - Generally speaking, you can use
ping
commands ortimeout
commands, but if you want to obtain a more accurate delay time, you can give priority toVBScript.sleep
the solution; - Although
ping
the command may be the most commonly used, it will have more and more errors when the delay time is long,timeout
/choice
will be much better than it