Aug 8, 2012

Function: Wait/Sleep

The good? Few lines not using any external utilities to measure the time.
The bad? Processor-intensive snippet of code (aka busy wait), and, if running from a pen-drive (or any other removable medium), may work jerky (cmd.exe reads batch files line by line).
First four lines are an example, trim them down to get pure "procedure" that you can use in your scripts. There is only one parameter to be given - integer, number of seconds.
@echo off
call:wait 60
pause
exit
:wait
call:wait_end
:wait_l0
set var0=%time:~-5,2%
:wait_l1
set var1=%time:~-5,2%
if "%var0%"=="%var1%" (goto wait_l1)
set/a var2+=1
if not "%var2%"=="%1" (goto wait_l0)
:wait_end
for /l %%G in (0,1,2) do (set var%%G=)
exit/b0

Here is another variant of this function. This one counts down instead, and shows seconds left in the window title.
:wait
setlocal
set var2=%1
:wait_l0
set var0=%time:~-5,2%
:wait_l1
set var1=%time:~-5,2%
if [%var0%]==[%var1%] (goto wait_l1)
set/a var2-=1
title [%var2%]
if [%var2%] GTR [0] (goto wait_l0)
endlocal&title %comspec%&exit/b0

No comments:

Post a Comment