Batch processing delay Incomplete summary _dos/bat

Source: Internet
Author: User
Tags diff goto sleep function

1, the use of ping command to realize the 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 approximately can pause 2 seconds
The number after-n is the number of packets sent, plus one for the number of seconds to pause. This method has a 0.5% deviation per second, with a time accuracy of 1 seconds.

@echo off 
@ping 127.0.0.1-n 6 >nul 
start Gdh.txt



2, in the VBS script to implement the sleep, sample code is as follows:
Disadvantage: Generate Temporary files
Advantages: Time accuracy of 0.001 seconds, high precision

Example 1, VBS CSCRIPT

Copy Code code as follows:

@echo off
echo Wscript.Sleep wscript.arguments (0) * 1000>delay.vbs
Delay.vbs 2
Del Delay.vbs
Echo ok!

Example 2, VBS START/WAIT

@echo off 
Echo wscript.sleep 5000>sleep.vbs 
start/wait sleep.vbs 
start gdh.txt 
del/f/s/q Sleep.vbs


---------------------------------------
The above program code can be suspended for 2 seconds, after 2 seconds to echo ok!

3, the use of the "dead" cycle : Set a time difference, if the current time and set the gap is not greater than the timing of the set, then do not exit the cycle, so as to achieve the purpose of delay. Sample code:
---------------------------------------------------

Copy Code code as follows:

@echo off
set/a start=%time:~6,2%
:P Rodelay
set/a now=%time:~6,2%
set/a diff=%now%-%start%
rem This sentence prevents the number of seconds from jumping from 59 to 1 o'clock errors.
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 goal of 2 seconds delay.

4:choice
Advantages: Accurate time, low CPU consumption, is the best choice

@echo off 
choice/t 5/d y/n >nul 
start gdh.txt

5:for+set+if, time accuracy is 0.01 seconds
Disadvantage: CPU occupies high, the statement is too long, not commonly used

@echo off 

setlocal enableextensions 

echo%time% call 
:P rocdelay 
echo%time% 
start Gdh.txt 
: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_%-%start_% 
If%diff_% LSS%1 goto _procwaitloop 
endlocal & goto:eof


6. Use at command. But this method is not so much a delay, it is better to say the timing of the good.
5,

Copy Code code as follows:

@echo Off & setlocal enableextensions
echo Wscript.Sleep 1000 >%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 after the program starts running #
Echo # #
Echo ##################################
cscript//nologo%temp%.\tmp$$$.vbs
Goto Timeout
Goto END

: Next
CLS & Echo.
For%%f in (%temp%.\tmp$$$.vbs*) Todo del%%f
Start edit Boot.ini
Exit

The following are additional users:

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

Using for analytic variable%time% coexist to two time points%start% and%now%, then calculate the time difference of two points by using the set/a, and finally use if to judge whether the time lag is up to the set pause.

Copy Code code as follows:

@echo off
Setlocal enableextensions
Echo%time%
Call:P Rocdelay 200
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_%-%start_%
If%diff_% LSS%1 goto _procwaitloop
Endlocal & Goto:eof

The sleep function in the "scheme II" VBS script, with a time precision of 0.001 seconds, using the platform for the WIN9X/WINNT series.

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

Copy Code code 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 & Goto:eof

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

Need your Windows system in the normal installation of the network card TCP/IP protocol, there are two scenarios, Scheme II is not recommended, see the topic [Discussion] Batch programming heterogeneous 12, 13 floor:

(1) Use ping two times to send messages between the time interval. Ping when sending multiple messages, after receiving a response to the last message, it waits another 1 seconds to send the next 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's an immediate response, it's relatively constant, and most other addresses have significant differences. This method waits for a larger time, specifying the number of messages to be sent, plus one, because the first message is not waiting, and the IP address must be 127.0.0.1. The procedure for waiting 2 seconds is as follows:

Copy Code code as follows:

:: According to the principle of tree planting, the number of n is suspended seconds plus a
Ping-n 3 127.0.0.1>nul

(2) Use the maximum wait time 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 ping requests, 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 role because the latency will be shortened by not having the maximum response time for each send wait time. This method to wait for more time, you can directly use-W specified, but need to subtract a certain transmission 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 procedure for waiting 2 seconds is as follows:

Copy Code code as follows:

Ping-n 2-w 0.0.0.1>nul

"Scheme four" choice default selection wait function, the time precision is 1 seconds, suitable platform for MS-DOS/WIN9X/WINNT series.

/t:y,2 Set the default selection character to Y, wait time is 2 seconds,/n suppresses [y,n] prompts on the command line, "rem|" The effect is to prevent choice from receiving keystrokes from the keyboard, so that pauses are not terminated accidentally because of pressing Y or other keys.

Copy Code code as follows:

rem|choice/t:y,2/n >nul

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

German Herbert Kleebauer gives a general scheme to indirectly generate a Sleep.exe program via an ASCII assembler to implement the delay, which is divided into DOS and win two modules, respectively calling DOS system interrupt service and WINDOWSAPI.

Copy Code code 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 Bj@jzh ' 0x-'/ppppppa (DE (DM) ("Do" ("Dh" (Ls () (LX) (lezrr]eeeuyrx2dx=>sleep.com
Echo 0dxfp,0xx.t0p,=xtgsb4o@$? Piyu Wwx0gwuy wv;ovbx2gv0exgiuht6>>sleep.com
echo t}{z~~ @GwkBG @oekcut ' ~} @MqqBsy sehb~_phxr? @zAB ' lrpeyodt@cj?>>sleep.com
Echo pky_jn@qekpet@ij?jysjn@rekpet@jj?jygjn@sekkjtlgunw?p@pjirz>>sleep.com
Echo Lfvaurq? Oyltq@@?~qcool~rdu@?au?@{qoq?@}ikunwpe~fpeqfwh? Vkk>>sleep.com
echo _gsqocvh{ojeoseiqrma@knefb?p?? Mcjnne~b? M?? Qhetlbgbphexh@e=>>sleep.com
Echo Esogwtlblk?sfu '? Ldod@@k@xo? Suuda?_fkj@n? Kd@? Ua?? O}hcqoq?? R>>sleep.com
Echo _oqol? Cla? Ceu?_fu? Uaq? UBD? LOC? ORO? Uol? UOD? OOI? UGL? Lor@yuo?>>sleep.com
Echo Dsmsqswdor[bqaq? Lua?_l_ounusclooulooduo? Uoe@owh? Uoq? Djtsdm>>sleep.com
Echo Qtqrk@kcmsulkpcloouloofuo?hwdtqostdbntqrrdsdftlnbtm ' lthkct>>sleep.com
echo @dmTkRQSoddTT ~? K? OCOQP?o?? Gds?wow? Pgatachqvnntqv_w? A?it/eh>>sleep.com
echo {zpqpkgk? Jbs? Fqokoh{t?jpvp@iqbdfan? 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

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.