# DOS command line sleep or wait



## heli2reg (Jan 11, 2009)

Some folks here have been asking for a DOS command that will hold a DOS batch program for a few seconds. Some are looking for sleep or wait alternatices. While there is CHOICE, PING and other tricks that can be used in DOS, they may not always work e.g. if pulling them in for a PostBuild process in Visual Studio .Net or other applications.

Today, I have come up with a DOS script that should work pretty much universal. The basic functionality is to remain in a loop while waiting for a certain amount of seconds to expire - comparing the start time with the current time.


```
@ECHO OFF
REM --- Set desired wait time right here ---
SET timeWait=1
 
ECHO.Waiting for %timeWait% second(s), giving the service an opportunity starting up all the way.
SET timeStart=%TIME:~6,2%
IF "%timeStart:~0,1%"=="0" SET timeStart=%timeStart:~1,1%
:TIMER
SET timeNow=%TIME:~6,2%
IF "%timeNow:~0,1%"=="0" SET timeNow=%timeNow:~1,1%
SET /A timeDiff=%timeStart%-%timeNow%
IF %timeDiff% LSS 0 (
SET /A timeDiff=%timeDiff% * -1
)
IF %timeDiff% LSS %timeWait% (
REM echo.So far only %timeDiff% second^(s^) passed.
GOTO TIMER
)
echo.%timeWait% second(s) passed. Done.
```
Have fun with it!
/Wolfgang


----------



## Squashman (Apr 4, 2003)

Going to assume you are still using XP because Vista and Windows 7 have the TIMEOUT command.


----------



## Squashman (Apr 4, 2003)

Your batch file also is flawed. Going to assume you didn't test it using higher wait times or when the seconds on the TIME variable was at higher or lower numbers.


----------

