DOS批处理延时执行方案总结

偶尔会用到,今天就总结几个批处理延时执行命令的方案。

需求:延时5秒打开test.txt这个文件

方案1:ping命令
缺点:时间精度约为1秒,延时越久误差越大,不够精确
@echo off 
@ping 127.0.0.1 -n 6 >nul
start test.txt


方案2-(1):VBScript和start /wait命令
缺点:附带生成临时文件
有点:时间精度约为0.001秒,精度高
@echo off
echo wscript.sleep 5000>%tmp%\sleep.vbs
start /wait sleep.vbs
start test.txt 
del /f /s /q %tmp%\sleep.vbs


方案2-(2):VBScript
@echo off 
echo wscript.sleep 5000>%tmp%\sleep.vbs
@cscript sleep.vbs >nul
start test.txt 
del /f /s /q %tmp%\sleep.vbs


方案3:choice命令
优点:时间精确,CPU占用低。最佳选择 
@echo off 
choice /t 5 /d y /n >nul
start test.txt


方案4:call命令
缺点:准确度受CPU频率影响很大,不够精确,占用内存较多
@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
start test.txt


方案5:msg命令
缺点:内存占用非常大,有窗口弹出
@echo off
msg %username% /time:5000 /w "正在延时,点确定可以取消延时!"
start test.txt

你可能感兴趣的:(Windows)