1、暂停(Pause)、延迟(Delay)、等待(Wait)、睡眠(Sleep)
1)for+set+if,时间精度为0.01秒,适用平台为WinNT/2K/XP/2003。
利用for解析变量%time%并存为两个时间点%start%和%now%,再利用set /a计算两个时间点的时间差,最后用if判断时间差是否达到设定的暂停时间。
@echo off
setlocal enableextensions
echo %time%
call <img src="images/smilies/face-raspberry.png" align="absmiddle" border="0">rocDelay 200
echo %time%
goto :EOF
<img src="images/smilies/face-raspberry.png" align="absmiddle" border="0">rocDelay delayMSec_
setlocal enableextensions
for /f "tokens=1-4 delims=:. " %%h in ("%time%"<img src="images/smilies/face-wink.png" align="absmiddle" border="0"> do set start_=%%h%%i%%j%%k
:_procwaitloop
for /f "tokens=1-4 delims=:. " %%h in ("%time%"<img src="images/smilies/face-wink.png" align="absmiddle" border="0"> do set now_=%%h%%i%%j%%k
set /a diff_=%now_%-%start_%
if %diff_% LSS %1 goto _procwaitloop
endlocal & goto :EOF
2)VBS脚本中的sleep函数,时间精度为0.001秒,使用平台为Win9x/WinNT系列。
动态创建一个调用sleep()函数的VBS脚本,然后用Windows脚本宿主的命令行版本cscript调用它。
@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
3)ping的消息发送间隔,时间精度为1秒,使用平台为Win9x/WinNT系列。
需要你的Windows系统中正常安装了网卡的TCP/IP协议,有两种方案,
方案二不建议采用,见主题
批处理编程的异类 12、13楼:
(1) 利用ping两次发送消息之间的间隔时间。ping在发送多个消息时,在得到上一次消息的回应后,它会再等待1秒的时间才发送下一次消息,而这个回应时间因机型、系统和网络配置而不同,其中IP地址尤其关键,只有本地的回送地址 127.0.0.1 因为是立即回应所以才是相对恒定的,其他地址大多会产生显著的差异。此种方法要等待更大的时间,是指定发送的消息数为等待的时间加一,因为第一个消息是不等待的,IP 地址必须是 127.0.0.1 。其等待2秒的程序如下:
::根据植树原则,n后的数字为暂停秒数加一
ping -n 3 127.0.0.1>nul
(2) 利用ping的最大等待时间。ping向0.0.0.1发送一次“网际消息控制协议 (ICMP)”回响请求消息,因为 0.0.0.1 是个无法响应 ping 请求的IP地址,所以默认情况下,ping会等待一定的最大响应时间——4秒后便停止等待,而-w可以改变这个每发送一次消息所等待的最大响应时间。而如果将 IP 地址改为立即响应的127.0.0.1或其他响应时间很短的IP ,那 -w 便失去了作用,因为每次发送等待时间都不会达到最大的响应时间,此时延时会缩短。此种方法要等待更大的时间,可以直接使用 -w 指定,但是需要减去一定的发送延时,这个延时需要自己事先测定一下,否则会有不到1秒的误差;而且 IP 地址必须是不响应请求的地址。其等待2秒的程序如下:
ping -n 2 -w 500 0.0.0.1>nul
4)choice的缺省选择等待功能,时间精度为1秒,适用平台为MS-DOS/Win9x/WinNT系列。
/t:y,2 将缺省选择字符设置为y,等待时间是2秒,/n禁止提示出现在命令行上,“rem|”的作用是禁止choice从键盘接受按键,从而暂停不会因为按下y或者其它键而被意外终止。
rem|choice /t:y,2 /n >nul
5)ASCII汇编代码,延迟精度为0.001秒,适用平台为MS-DOS/Win9x/WinNT。
德国人Herbert Kleebauer给出了一个通用方案,通过ASCII汇编器间接产生一个sleep.exe程序来实现延迟,这个程序分为DOS和Win两个模块,分别调用DOS系统中断服务和WindowsAPI。
:: 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(Ls(Lu(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
Last edited by willsort on 2005-12-4 at 21:37 ]
1、Pause, Delay, Wait, Sleep
1) for+set+if, with a time precision of 0.01 seconds, applicable platforms: WinNT/2K/XP/2003.
Use for to parse the variable %time% and store it as two time points %start% and %now%, then use set /a to calculate the time difference between the two time points, and finally use if to judge whether the time difference has reached the set pause time.
@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
2) The sleep function in VBS scripts, with a time precision of 0.001 seconds, applicable platforms: Win9x/WinNT series.
Dynamically create a VBS script that calls the sleep() function, and then use the command-line version of the Windows Script Host, cscript, to call it.
@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
3) The message sending interval of ping, with a time precision of 1 second, applicable platforms: Win9x/WinNT series.
Your Windows system needs to have the TCP/IP protocol of the network card normally installed. There are two schemes.
Scheme two is not recommended. See the theme
The Oddities of Batch Programming on floors 12 and 13:
(1) Use the interval time between two ping message sends. When ping sends multiple messages, after getting the response of the previous message, it will wait for 1 second before sending the next message. And this response time varies due to the model, system, and network configuration. Among them, the local loopback address 127.0.0.1 is relatively constant because it responds immediately. Other addresses mostly have significant differences. This method needs to wait a longer time. It is to specify the number of messages sent as one more than the waiting time, because the first message is not waited for. The IP address must be 127.0.0.1. The program to wait for 2 seconds is as follows:
:: According to the tree-planting principle, the number after n is one more than the pause seconds
ping -n 3 127.0.0.1>nul
(2) Use the maximum waiting time of ping. ping sends an "Internet Control Message 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, by default, ping will wait for a certain maximum response time - it stops waiting after 4 seconds, and -w can change this maximum response time for each message send. And if the IP address is changed to 127.0.0.1 or other IP addresses with very short response times that respond immediately, then -w loses its effect because the waiting time for each send will not reach the maximum response time, and the delay will be shortened. This method needs to wait a longer time. You can directly use -w to specify, but you need to subtract a certain sending delay. This delay needs to be measured 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 requests. The program to wait for 2 seconds is as follows:
ping -n 2 -w 500 0.0.0.1>nul
4) The default selection waiting function of choice, with a time precision of 1 second, applicable platforms: MS-DOS/Win9x/WinNT series.
/t:y,2 sets the default selection character to y, the waiting time is 2 seconds, /n prohibits the prompt from appearing on the command line, and the role of "rem|" is to prohibit choice from accepting key presses from the keyboard, so that the pause will not be accidentally terminated by pressing y or other keys.
rem|choice /t:y,2 /n >nul
5) ASCII assembly code, with a delay precision of 0.001 seconds, applicable platforms: MS-DOS/Win9x/WinNT.
German Herbert Kleebauer gave a general solution. By indirectly generating a sleep.exe program through an ASCII assembler to achieve delay. This program is divided into DOS and Win modules, which respectively call DOS system interrupt services and Windows APIs.
:: 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(Ls(Lu(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
Last edited by willsort on 2005-12-4 at 21:37 ]