Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to achieve incomplete delay in batch processing

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how batch processing achieves incomplete latency. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Using ping command to realize delay, the method is ingenious and controllable, but the precision is not high.

For example, ping-n 3 127.0.0.1 > nul can pause for about 2 seconds.

The number after-n is the number of packets sent, plus one for the number of seconds paused. This method has a deviation of 0.5% per second and the time accuracy is 1 second.

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

2. Implement it with sleep in vbs script. The sample code is as follows:

Cons: generate temporary files

Advantages: time accuracy of 0.001 seconds, high accuracy

Example 1, vbs cscript

The code is 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 paused for 2 seconds and echo OK after 2 seconds!

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

The code is as follows:

@ echo off

Set / a Start=%time:~6,2%

: ProDelay

Set / a Now=%time:~6,2%

Set / a Diff=%Now%-%Start%

The following sentence of rem prevents errors caused by jumping from 59 seconds to 1 seconds.

If Diff% lss 0 set / a Diff=%Diff%+60

If% Diff% leq 2 goto: ProDelay

Echo ok!

-

The above program can also achieve the purpose of delay of 2 seconds.

4:choice

Advantages: accurate time, low CPU footprint, is the best choice

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

5:for+set+if, time accuracy is 0.01s.

Disadvantages: high occupancy of CPU, long sentences, not commonly used

@ echo off setlocal enableextensions echo% time% call: ProcDelay 500 echo% time% start gdh.txt: ProcDelay 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. However, this method is better timed than delayed.

5 、

The code is as follows:

@ echo off & setlocal enableextensions

Echo WScript.Sleep 1000 >% temp%.\ tmp$$$.vbs

Set / an I = 5

: Timeout

If% I% = 0 goto Next

Setlocal

Set / an I =% I%-1

Cls

Echo DOS countdown program: alike collection

Echo # #

Echo # #

Echo # [% I%] seconds later the 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 some additions from other netizens:

[scheme 1] for+set+if, the time accuracy is 0.01s, and the platform is WinNT/2K/XP/2003.

Using the for parsing variable% time% to coexist as two time points% start% and% now%, and then using set / a to calculate the time difference between the two time points, and finally using if to determine whether the time difference reaches the set pause time.

The code is as follows:

@ echo off

Setlocal enableextensions

Echo time%

Call: ProcDelay 200

Echo time%

Goto: EOF

: ProcDelay 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

[scheme 2] the sleep function in the VBS script has a time precision of 0.001 seconds and the platform is 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.

The code is as follows:

@ echo off & setlocal enableextensions enabledelayedexpansion

Echo WScript.Sleep 2000 > 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 3] the message sending interval of ping is 1 second, and the platform is Win9x/WinNT series.

You need to install the TCP/IP protocol with a network card in your Windows system. There are two solutions. Solution 2 is not recommended. See the topic [discussion] heterogeneous 12th and 13th floor of batch programming:

(1) the interval between two messages sent by ping. When sending multiple messages, ping waits for 1 second to send the next message after receiving the response from the last message. The response time varies with the model, system and network configuration, and the IP address is especially critical. Only the local echo address 127.0.0.1 is relatively constant because it is an immediate response, and most of the other addresses will produce significant differences. To wait for a longer time, this method specifies that the number of messages sent is added to the waiting time, because the first message does not wait, and the IP address must be 127.0.0.1. The program that waits for 2 seconds is as follows:

The code is as follows:

According to the tree planting principle, the number after n is the number of paused seconds plus one

Ping-n 3 127.0.0.1 > nul

(2) make use of the maximum waiting 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, ping will wait for a certain maximum response time-4 seconds and then stop waiting, and-w can change the maximum response time for each message sent. If you change the IP address to 127.0.0.1 of the immediate response or other IP with a short response time, then-w will have no effect because the wait time for each send will not reach the maximum response time, and the delay will be shortened. For this method to wait for a longer time, you can directly use-w to specify, but you need to subtract a certain transmission delay, which needs to be determined in advance, otherwise there will be an error of less than 1 second; and the IP address must be an address that does not respond to the request. The program that waits for 2 seconds is as follows:

The code is as follows:

Ping-n 2-w 500 0.0.0.1 > nul

[scheme 4] the default selection waiting function of choice is 1 second, and the platform is MS-DOS/Win9x/WinNT series.

The default selection character is set to y, the waiting time is 2 seconds, / n forbids the [y rem n] prompt to appear on the command line, and the function of "rem |" is to prevent the choice from accepting keys from the keyboard, so the pause will not be terminated unexpectedly because of pressing y or other keys.

The code is as follows:

Rem | choice / tnul 2 / n > nul

[scheme 5] ASCII assemble code, delay accuracy is 0.001 seconds, applicable platform is MS-DOS/Win9x/WinNT.

German Herbert Kleebauer gives a general scheme, which indirectly generates a sleep.exe program to achieve delay through ASCII assembler. This program is divided into two modules: DOS and Win, which call DOS system interrupt service and WindowsAPI respectively.

The code is as follows:

:: Sleep.bat-Sleep/Delay/Wait n seconds

:: Herbert Kleebauer (Germany)-2005-05-29

:: Modified by Will Sort-2005-06-02, 07-25

@ echo off

Echo Q | debug > nul

Echo Bj@ jzh`0X-`/ PPPPPPa (DE (DM (DO) Dh (Lu (LX (LeZRR) EEEUYRX2Dx= > sleep.com)

Echo 0DxFP, 0Xx.t0P, XtGsB4odyne, PIyU WwX0GwUY Wv;ovBX2Gv0ExGIuht6 > > sleep.com

Echo T} {zealots 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? hwDTqOsTdbnTQrDsdFTlnBTm`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 00

Del sleep.com

Del sleep.exe

Thank you for reading! This is the end of this article on "how to achieve incomplete delay in batch processing". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report