Several Common Ways to Realize Delay Function in Windows Bat


bat Several common ways to implement delay function in batch processing

1. Use pingcommands 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
  1. -nThe specified pingnumber of times, with an interval of about 1s each time, >nulwill pingecho the process information to the shield
  2. pingDelay of command implementation, time precision: about 1s, memory usage: pingspace occupied by commands
  3. 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!
  4. I have also seen from the Internet -wthat 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 -nhow 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:
verify ping
It can be seen that those -npeople on the Internet who say "set as many seconds as you need to delay" are so misleading. However, when -nthe 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 -nto a larger value to verify.
(This error actually pinghas a lot to do with your address)

Then pass a script to verify -wthe 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 -wvalue 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). -wThe time specified by the parameter will be applied to the delay effect
Test effect

2. Use timeoutcommands 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
  1. /tSpecifies the description of the wait, the valid range -1is up to 99999seconds, -1which means waiting indefinitely
  2. /nobreakIndicates ignoring the key and waiting for the specified time. The only key that can be received is Ctrl+Za combination key. If the waiting time is not up, Ctrl+Zthe key will terminate the batch program and timeoutsubsequent commands will not be executed.
  3. timeoutThe delay implemented by the command, in addition to -1the infinite wait, the maximum delay 99999is seconds, but you can call timeoutthe command multiple times to achieve a delay waiting beyond this limit
  4. timeoutDelay of command implementation, time precision: about 1s, memory usage: timeoutspace occupied by commands

3. Use choicecommands 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
  1. /CSpecify the option list, /Tspecify the waiting time before making the default selection, the acceptable value 0-9999, and /Dthe default selection after the waiting time is exceeded;
  2. choiceThe delay realized by the command is a single maximum delay of 9999seconds, but you can call choicethe command multiple times to achieve a delay waiting beyond this limit;
    3. choiceThe delay achieved by the command, time accuracy: about 1s, memory usage: chocieoccupied by the command space

4. Use fora loop to implement a delay

The delay effect can also be achieved by using forthe loop, to be precise , the combination of forloop + ping/ timeout/ is used choiceto 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
  1. for+ ping// The combination method can flexibly combine the delay time you want, without being limited by timeoutthe maximum delay / time;choicetimeoutchoice
  2. pingIf the command wants to delay mseconds, -nthe parameter value needs to be set to m+1, because the first pingrequest to receive the value is almost instantaneous;
  3. For the delay of this combination command, I suggest forsetting 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 the ping// 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 large timeout. (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
    forpingforping -n 101forping -n 2

5. Use sleepcommands to implement delay

Windows batchThere are no native sleepcommands in , and third-party tools are required.
downloadsleep.exe

curl https://www.computerhope.com/download/utility/sleep.exe > sleep.exe

Put the downloaded sleep.exeprogram in C:\Windows\System32or other PATHpaths 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.sleepimplementation delay

In batch processing, vbscriptthe 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 vbscriptto 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.

  1. cscriptTime accuracy: 1 millisecond; memory usage: the vbs script itself is not large, and the execution / process of vbs wscripttakes up about 1MB of memory;
  2. To call a vbs script, you can also use wscriptcommands, such aswscript delay.vbs 1000 >nul
  3. 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

  1. Except sleep.exefor third-party tools, which need to be downloaded and installed, the rest are built-in with Windows and can be used directly;
  2. Generally speaking, you can use pingcommands or timeoutcommands, but if you want to obtain a more accurate delay time, you can give priority to VBScript.sleepthe solution;
  3. Although pingthe command may be the most commonly used, it will have more and more errors when the delay time is long, timeout/ choicewill be much better than it

Guess you like

Origin blog.csdn.net/B11050729/article/details/131711697