Batch delay does not fully summarize "go"

Source: Internet
Author: User
Tags sleep function

Original address: http://www.jb51.net/article/18055.htm

1, the use of ping command to achieve delay, the method is clever and can be controlled, but the accuracy is not high. such as: Ping-n 3 127.0.0.1>nul can pause for 2 seconds after-n number is the number of packets sent, to pause the number of seconds plus one. This method has a 0.5% deviation per second and a time accuracy of 1 seconds.
2. With the sleep implementation in the VBS script, the sample code is as follows:
--------------------------------------

Copy CodeThe code is as follows: @echo off Echo Wscript.Sleep wscript.arguments (0) * 1000>delay.vbs delay.vbs 2 del delay.vbs
Echo ok!

---------------------------------------
The above program code can be suspended for 2 seconds, 2 seconds after the Echo ok!
3, the use of "dead" cycle : Set a time difference, if the current time and set the difference between the timing is not greater than the set difference, then do not exit the cycle, so as to achieve the purpose of delay. Sample code:
---------------------------------------------------

Copy CodeThe code is as follows: @echo off set/a start=%time:~6,2%
:P rodelay set/a now=%time:~6,2% set/a diff=%now%-%start% REM The following sentence prevents the number of seconds from jumping from 59 to 1 o'clock error. If%diff% LSS 0 set/a diff=%diff%+60 if%diff% leq 2 goto:P rodelay echo ok!

---------------------------------------------
The above program can also achieve the purpose of 2 seconds delay. 4, the use at command. But this method is not so much a delay, it is better to say timing.
5.

Copy CodeThe code is as follows: @echo off & setlocal enableextensions
echo wscript.sleep >%temp%.\tmp$$$.vbs set/a i = 5
: Timeout If%i% = = 0 goto Next setlocal set/a i =%i%-1
CLS Echo DOS Countdown program: Alike collection echo ##################################
echo # # echo # [%i%] seconds program starts running # echo # # echo ################################## cscript//nologo%temp%.\tmp$$$.vbs
Goto Timeout Goto END
: Next CLS & Echo. For%%f in (%temp%.\tmp$$$.vbs*) Do del%%f start edit boot. ini exit

Here are the other users to add:

"Scheme One" for+set+if, the time precision is 0.01 seconds, the applicable platform is winnt/2k/xp/2003.

Using the for-parse variable%time% coexist as two time points%start% and%now%, then use set/a to calculate the time difference of two points, and finally use if to determine whether the time difference reached the set pause.

Copy CodeThe code is as follows: @echo off setlocal enableextensions echo%time% call:P Rocdelay, echo%time% goto:eof

:P rocdelay delaymsec_ setlocal enableextensions for/f "tokens=1-4 delims=:."%%h in ("%time%") do set start_=%%h%%i%%j%% K: _procwaitloop for/f "tokens=1-4 delims=:."%%h in ("%time%") do set now_=%%h%%i%%j%%k set/a Diff_=%now_%-%star t_% if%diff_% LSS%1 goto _procwaitloop endlocal & goto:eof

The sleep function in the "scenario two" VBS script with a time accuracy of 0.001 seconds, using the platform as the Win9x/winnt series.

Dynamically create a VBS script that calls the sleep () function and then invoke it with the command-line version cscript of the Windows Script Host.

Copy CodeThe code is as follows: @echo off & setlocal enableextensions enabledelayedexpansion echo wscript.sleep >%temp%/tmp$$$.vbs echo %time% cscript//nologo%temp%/tmp$$$.vbs echo%time% for%%f in (%temp%/tmp$$$.vbs) do if exist%%f del%%f endlocal &am P Goto:eof

"Scenario three" ping message sending interval, time accuracy of 1 seconds, using the platform for the WIN9X/WINNT series.

Requires the TCP/IP protocol of your Windows system to properly install the network card, there are two scenarios, scenario two is not recommended, see topic [Discussion] heterogeneous 12, 13 Floor of batch programming:

(1) The interval between sending messages using pings two times. When a ping sends multiple messages, it waits 1 seconds to send the next message after receiving a response from the last message, which differs depending on the model, system, and network configuration, where the IP address is particularly critical, with only the local loopback address 127.0.0.1 Because it is an immediate response, it is relatively constant, and most of the other addresses will have significant differences. This method waits for a larger time, which is to specify the number of messages to send as the wait time plus one, because the first message is not waiting, and the IP address must be 127.0.0.1. The program that waits 2 seconds is as follows:

Copy CodeThe code is as follows::: According to the tree planting principle, the number after n is the pause seconds plus one ping-n 3 127.0.0.1>nul

(2) The maximum wait time to take advantage of ping. Ping sends an "Internet Message Control Protocol (ICMP)" Echo request message to 0.0.0.1 because 0.0.0.1 is an IP address that cannot respond to a ping request, so by default, the ping waits for a certain maximum response time--4 seconds to stop waiting. And-W can change the maximum response time for each message that is sent. If you change the IP address to an immediate response 127.0.0.1 or other IP with a short response time, then-W will lose its effect, because each send wait time will not reach the maximum response time, when the delay is shortened. This method to wait for a larger time, you can directly use the-W designation, but need to subtract a certain send delay, this delay needs to be measured in advance, otherwise there will be less than 1 seconds of error, and the IP address must not respond to the requested address. The program that waits 2 seconds is as follows:

Copy CodeThe code is as follows: Ping-n 2-w 0.0.0.1>nul

"Scheme four" choice default selection wait function, time accuracy of 1 seconds, applicable platform for the MS-DOS/WIN9X/WINNT series.

/t:y,2 sets the default selection character to Y, the wait time is 2 seconds, and/n prohibits the [y,n] prompt from appearing on the command line, "rem|" Prevents the choice from accepting keystrokes from the keyboard, thus suspending it from being accidentally terminated by pressing Y or another key.

Copy CodeThe code is as follows: rem|choice/t:y,2/n >nul

"Scheme five" ASCII assembly code, the delay accuracy of 0.001 seconds, the applicable platform for Ms-dos/win9x/winnt.

German Herbert Kleebauer gives a generic scheme that indirectly generates a Sleep.exe program through the ASCII assembler to implement the delay, which is divided into DOS and win two modules, respectively called DOS System Interrupt service and WINDOWSAPI.

Copy Code The code is as follows::: sleep.bat-sleep/delay/wait n seconds:: Herbert Kleebauer (Germany)-2005/05/29:: Modified by would Sort-2005 /06/02, 07-25 @echo off echo q | Debug>nul Echo [email protected] ' 0x-'/ppppppa (The DE (DM (Ls ( 0dxfp,0xx.t0p,[email protected]$? Piyu Wwx0gwuy wv;ovbx2gv0exgiuht6>>sleep.com Echo t}{[email protected] @OEKcUt ' ~}@[email protected] ' [Email protected]?>>sleep.com Echo [email protected]@[email protected]@[email  protected][email protected]>>sleep.com Echo [email protected]@[email protected][email  Protected]{[email protected]}ikunwpe~fpeqfwh? vkk>>sleep.com echo _gsqocvh{[email protected][email protected]=>>sleep.com echo EsOgwTLbLK? SFU ' [email protected]@[email protected][email protected][email protected]? Ua?? O}hcqoq?? r>>sleep.com echo [email protected]?>>sleep.com echo Dsmsqswdor[[email pRotected]? Uoq? djtsdm>>sleep.com echo [EMAIL PROTECTED]?HWDTQOSTDBNTQRRDSDFTLNBTM ' lthkct>>sleep.com echo @ Dmtkrqsoddtt~? K? OCOQP?o?? Gds?wow? Pgatachqvnntqv_w? a?it/eh>>sleep.com echo {zpqpkgk? Jbs? Fqokoh{[email protected]? Ohrol? Kj?? Pd~an? Ohrod? g>>sleep.com echo Q?? Pgt~b?? Oc~?ipo? T?~u?p~cuo0x>>sleep.com sleep.com>sleep.exe echo wait%1 seconds:sleep.exe%1000 del sleep.com del Sleep.exe

Batch delay does not fully summarize "go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.