China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-20 18:59
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Are there any more clever techniques for batch processing delays? DigestI View 32,800 Replies 71
Original Poster Posted 2006-09-29 07:08 ·  中国 重庆 合川区 电信
初级用户
Credits 126
Posts 38
Joined 2006-09-26 20:39
19-year member
UID 63726
Status Offline
ping -n can achieve delay, but I recognize this is not a good way, because I checked the CPU usage, and when pinging, it has reached 50%. Such a delay, what program are you going to do?
Floor 2 Posted 2006-09-29 07:41 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
可以变通一下,用 at

Can be flexibly handled, using "at"
Floor 3 Posted 2006-09-29 07:47 ·  中国 重庆 合川区 电信
初级用户
Credits 126
Posts 38
Joined 2006-09-26 20:39
19-year member
UID 63726
Status Offline
It seems that at requires the support of a service. Now many machines, after optimization, have not enabled this service.
Floor 4 Posted 2006-09-29 07:54 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Originally posted by nvflash at 2006-9-29 07:47:
at seems to require service support. Now many machines are optimized and this service is not enabled

You can just enable it on your own machine~ If you want to enable it on someone else's machine, you can execute the following to enable it

sc config schedule start= auto
sc start schedule
Floor 5 Posted 2006-09-29 08:17 ·  中国 重庆 合川区 电信
初级用户
Credits 126
Posts 38
Joined 2006-09-26 20:39
19-year member
UID 63726
Status Offline
If you say it's about task scheduling, I've lost confidence. I've tried multiple task scheduling methods many times, and it inexplicably didn't execute correctly. This thing isn't very reliable, I think it's better not to use it.
Floor 6 Posted 2006-09-29 08:19 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Hehe, it is suggested that you search for "delay" as there have been many discussions about it before
Floor 7 Posted 2006-09-29 08:48 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Summary of Batch Processing Delay Methods:

1. ping

@echo off
:loop
echo %time%
ping 127.1 -n 2 1>nul
echo %time%
goto loop


Memory Usage: cmd.exe 1704k
ping.exe 2920k
Error Evaluation: High
Advantages: Simple code structure
Disadvantages: High memory usage, relatively large error when delay time is long

2. Still ping

@echo off
:loop
echo %time%
ping 1 -n 1 -w 1000 2>nul 1>nul
echo %time%
goto loop

Memory Usage: cmd.exe 1700k
ping.exe 2912k
Error Evaluation: General
Advantages: Simple code structure, smaller error with longer time, higher accuracy (50ms)
Disadvantages: High memory usage


3. call

@echo off
:loop
echo %time%
call :delay 1000
echo %time%
goto loop

:delay
set /a num=num + 1
if %num% geq %1 (set num=) && goto :eof
rem for /l %%i in (1,1,%1) do echo. >nul
goto :eof

Memory Usage: cmd.exe 1744k
cmd.exe 1740k
Error Evaluation: Very high (greatly affected by CPU frequency, almost impossible to accurately grasp overall delay time)
Advantages: Relatively high accuracy
Disadvantages: Not suitable for occasions requiring precise time grasping

4. msg

@echo off
:loop
echo %time%
msg %username% /time:20 /w "Delaying, click OK to cancel delay!"
echo %time%
goto loop


Memory Usage: cmd.exe 1752k
msg.exe 2620k
Error Evaluation: Low
Advantages: Relatively stable, can cancel delay midway, simple code structure
Disadvantages: Very high memory usage, window pops up (advantage? Disadvantage?)

5. vbs

@echo off
echo Wscript.Sleep WScript.Arguments(0) >%tmp%\delay.vbs
:loop
echo %time%
cscript //b //nologo %tmp%\delay.vbs 2000
echo %time%
goto loop

Memory Usage: cscript.exe 4812k
cmd.exe 1708k
Error Evaluation: Very low
Advantages: Highest accuracy, more flexible and convenient to use
Disadvantages: Temporary file generated, high memory usage


End:
1. Limited test conditions, the above data is not authoritative.
2. About accuracy.
Actually, any command with adjustable time has relatively high accuracy, the reason for being affected depends on the number of command executions, the more times, the lower the accuracy. And the reason why the fifth scheme is said to have the highest accuracy can be exemplified: cscript //b //nologo delay.vbs 2036, while the second scheme is still worse than the fifth scheme due to the minimum accuracy.
3. Everyone is welcome to supplement.
Reference Links:
1.Trouble Caused by a GIF File
2. Heterodoxy in Batch Processing Programming


[ Last edited by 3742668 on 2006-9-29 at 08:49 ]
Floor 8 Posted 2006-09-29 08:49 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Can you explain why you need to perform a delay operation?
Floor 9 Posted 2006-09-29 09:11 ·  中国 重庆 合川区 电信
初级用户
Credits 126
Posts 38
Joined 2006-09-26 20:39
19-year member
UID 63726
Status Offline
Excuse me, moderator, besides %time%, what other system-built-in variables are there?
Floor 10 Posted 2006-09-29 09:14 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Originally posted by nvflash at 2006-9-29 09:11:
May I ask the moderator, besides %time%, what are other system-built-in variables?

1. Run the command set without parameters
2. Run the command set /?, at the end.
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
+1 2010-01-27 09:13
newborenee +1 2010-03-10 22:14
Floor 11 Posted 2006-11-03 12:40 ·  中国 上海 虹口区 电信
高级用户
★★
Credits 653
Posts 252
Joined 2006-04-16 19:48
20-year member
UID 53939
Status Offline
Recommend a standard POSIX command: sleep, for example:

Sleep for 10 seconds
sleep 10s

1 minute
sleep 1m

2 hours
sleep 2h

5 days:
sleep 5d

The attachment is the win32 version of sleep. It may be not very convenient for batch processing that needs to be spread around. It's good for personal use.
Attachments
sleep.rar (7.57 KiB, Credits to download 1 pts, Downloads: 381)
Floor 12 Posted 2006-11-03 23:43 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  Hehe, thanks to the person above for providing another small tool for delaying. I'll download it and try it out~~

  I remember there's another tool called sleepx, which can be precise to milliseconds. I wonder if this sleep has relatively high precision~~
Floor 13 Posted 2006-11-04 04:25 ·  中国 上海 杨浦区 电信
高级用户
★★
Credits 653
Posts 252
Joined 2006-04-16 19:48
20-year member
UID 53939
Status Offline
Originally posted by lxmxn at 2006-11-3 23:43:

  Hehe, thank you upstairs for providing another small tool for delay. Download it and try it~~

  I remember there is another tool called sleepx, which can be precise to milliseconds. I wonder this sle ...


Hehe, under win32, except for using the API: timeSetEvent, it is very difficult for other means to achieve precise millisecond-level delays (although Performance Counter can also do it, but the CPU utilization rate is too high).

timeSetEvent will change the time slice granularity in the global system range, and also has a little impact on system performance. If you think it is really necessary, I can write one
Floor 14 Posted 2006-12-26 07:40 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
This is good.
Support
Floor 15 Posted 2006-12-26 19:20 ·  新西兰 奥克兰大区 奥克兰 Microsoft
高级用户
★★
Credits 783
Posts 268
Joined 2006-12-26 17:18
19-year member
UID 74627
Gender Male
Status Offline
@echo off
echo Current seconds: %time:~6,-3%
set /p dalay="Enter the delay in seconds within 60: "%dalay%
:compte
if not "%dalay%"=="%time:~6,-3%" goto compte
echo Time's up!! Pay the bill!
pause>nul
1 2 3 5 Next ›
Forum Jump: