Rob van der Woude's Scripting Pages

WAIT

To make a batch file wait for a number of seconds there are several options available:

Note: Click a script file name to expand and view its source code; click the file name again, or the expanded source code, to hide the source code again.
To view the source code on its own, right-click the file name and choose Open or Open in separate tab or window.

PAUSE

The most obvious way to pause a batch file is of course the PAUSE command. This will stop execution of the batch file until someone presses "any key". Well, almost any key: Ctrl, Shift, NumLock etc. won't work.
This is fine for interactive use, but sometimes we just want to delay the batch file for a fixed number of seconds, without user interaction.

SLEEP

SLEEP was included in some of the Windows Resource Kits.
It waits for the specified number of seconds and then exits.

SLEEP 10

will delay execution of the next command by 10 seconds.

There are lots of SLEEP clones available, including the ones mentioned in the UNIX Ports paragraph at the end of this page.

TIMEOUT

TIMEOUT was included in some of the Windows Resource Kits, but is a standard command as of Windows 7.
It waits for the specified number of seconds or a keypress, and then exits.
So, unlike SLEEP, TIMEOUT's delay can be "bypassed" by pressing a key.

TIMEOUT 10

or

TIMEOUT /T 10

will delay execution of the next command by 10 seconds, or until a key is pressed, whichever is shorter.

D:\>TIMEOUT /T 10

Waiting for 10 seconds, press a key to continue ...

You may not always want to abort the delay with a simple key press, in which case you can use TIMEOUT's optional /NOBREAK switch:

D:\>TIMEOUT /T 10 /NOBREAK

Waiting for 10 seconds, press CTRL+C to quit ...

You can still abort the delay, but this requires Ctrl+C instead of just any key, and will raise an ErrorLevel 1.

PING

For any MS-DOS or Windows version with a TCP/IP client, PING can be used to delay execution for a number of seconds.

PING localhost -n 6 >NUL

will delay execution of the next command for (a little over) 5 seconds seconds (default interval between pings is 1 second, the last ping will add only a minimal number of milliseconds to the delay).
So always specify the number of seconds + 1 for the delay.

The PING time-out technique is demonstrated in the following examples:

  PMSleep.bat for Windows NT

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Check if a valid timeout period is specified
  6. IF     "%~1"=="" GOTO Syntax
  7. IF NOT "%~2"=="" GOTO Syntax
  8. ECHO.%*| FINDSTR /R /X /C:"[0-9][0-9]*" >NUL || GOTO Syntax
  9. IF %~1 LSS    1 GOTO Syntax
  10. IF %~1 GTR 3600 GOTO Syntax
  11.  
  12. :: Use local variable
  13. SETLOCAL
  14.  
  15. :: Add 1 second for IPv4
  16. SET /A seconds = %1 + 1
  17.  
  18. :: The actual command: try IPv4 first, if that fails try IPv6
  19. PING -n %seconds% 127.0.0.1 >NUL 2>&1 || PING -n %1 ::1 >NUL 2>&1
  20.  
  21. :: Done
  22. ENDLOCAL
  23. GOTO:EOF
  24.  
  25.  
  26. :Syntax
  27. ECHO.
  28. ECHO PMSleep.bat
  29. ECHO Poor Man's SLEEP utility,  Version 3.00 for Windows NT 4 and later.
  30. ECHO Wait for a specified number of seconds.
  31. ECHO.
  32. ECHO Usage:  CALL  PMSLEEP  seconds
  33. ECHO.
  34. ECHO Where:        seconds  is the number of seconds to wait (1..3600)
  35. ECHO.
  36. ECHO Notes:  The script uses PING for the delay, so an IP stack is required.
  37. ECHO         The delay time will not be very accurate.
  38. ECHO.
  39. ECHO Written by Rob van der Woude
  40. ECHO http://www.robvanderwoude.com
  41.  
  42. IF "%OS%"=="Windows_NT" EXIT /B 1
  43.  

  PMSlpW9x.bat for Windows 95/98

  1. @ECHO OFF
  2. :: Check if a timeout period is specified
  3. IF "%1"=="" GOTO Syntax
  4.  
  5. :: Filter out slashes, they make the IF command crash
  6. ECHO.%1 | FIND "/" >NUL
  7. IF NOT ERRORLEVEL 1 GOTO Syntax
  8.  
  9. :: Check for a non-existent IP address
  10. :: Note: this causes a small extra delay!
  11. IF "%NonExist%"=="" SET NonExist=10.255.255.254
  12. PING %NonExist% -n 1 -w 100 | FIND "TTL=" >NUL
  13. IF ERRORLEVEL 1 GOTO Delay
  14. SET NonExist=1.1.1.1
  15. PING %NonExist% -n 1 -w 100 | FIND "TTL=" >NUL
  16. IF NOT ERRORLEVEL 1 GOTO NoNonExist
  17.  
  18. :Delay
  19. :: Use PING time-outs to create the delay
  20. PING %NonExist% -n 1 -w %1000 >NUL
  21.  
  22. :: Show online help on errors
  23. IF ERRORLEVEL 1 GOTO Syntax
  24.  
  25. :: Done
  26. GOTO End
  27.  
  28. :NoNonExist
  29. ECHO.
  30. ECHO This batch file needs an invalid IP address to function
  31. ECHO correctly.
  32. ECHO Please specify an invalid IP address in an environment
  33. ECHO variable named NonExist and run this batch file again.
  34.  
  35. :Syntax
  36. ECHO.
  37. ECHO PMSlpW9x.bat
  38. ECHO Poor Man's SLEEP utility,  Version 2.10 for Windows 95 / 98
  39. ECHO Wait for a specified number of seconds.
  40. ECHO.
  41. ECHO Written by Rob van der Woude
  42. ECHO http://www.robvanderwoude.com
  43. ECHO Corrected and improved by Todd Renzema and Greg Hassler
  44. ECHO.
  45. ECHO Usage:    CALL PMSLPW9X nn
  46. ECHO.
  47. ECHO Where:    nn is the number of seconds to wait
  48. ECHO.
  49. ECHO Example:  CALL PMSLPW9X 10
  50. ECHO           will wait for 10 seconds
  51. ECHO.
  52. ECHO Note:     Due to "overhead" the actual delay may
  53. ECHO           prove to be up to a second longer
  54.  
  55. :End
  56.  

💾   Download the PMSleep sources

NETSH

NETSH may seem an unlikely choice to generate delays, but it is actually much like using PING:

NETSH Diag Ping Loopback

will ping localhost, which takes about 5 seconds — hence a 5 seconds delay.

NETSH is native in Windows XP Professional and later versions.
Unfortunately however, this trick will only work in Windows XP/Server 2003.

CHOICE

In MS-DOS 6, Windows 9*/ME and NT 4

REM | CHOICE /C:AB /T:A,10 >NUL

will add a 10 seconds delay.
By using REM | before the CHOICE command, the standard input to CHOICE is blocked, so the only "way out" for CHOICE is the time-out specified by the /T parameter.
This idea was borrowed from Laurence Soucy, I added the /C parameter to make it language independent (the simpler REM | CHOICE /T:N,10 >NUL will work in many but not all languages).

  The CHOICE delay technique is demonstrated in the following example, Wait.bat:

  1. @ECHO OFF
  2. IF "%1"=="" GOTO Syntax
  3. ECHO.
  4. ECHO Waiting %1 seconds
  5. ECHO.
  6. REM | CHOICE /C:AB /T:A,%1 > NUL
  7. IF ERRORLEVEL 255 ECHO Invalid parameter
  8. IF ERRORLEVEL 255 GOTO Syntax
  9. GOTO End
  10.  
  11. :Syntax
  12. ECHO.
  13. ECHO WAIT for a specified number of seconds
  14. ECHO.
  15. ECHO Usage:  WAIT  n
  16. ECHO.
  17. ECHO Where:  n  =  the number of seconds to wait (1 to 99)
  18. ECHO.
  19.  
  20. :End
  21.  
Note: The line ECHO Invalid parameter ends with an "invisible" BELL character, which is ASCII character 7 (beep) or ^G (Ctrl+G).

In Windows 10 the REM trick no longer works, and the default option is no longer specified with the /T switch, but with a separate /D switch:

CHOICE /C:AB /D:A /T:10 >NUL

This means that, unlike in DOS, in Windows 10 you can skip the delay by pressing one of the choices specified with the /C switch.

  The CHOICE delay technique is demonstrated in the following example, Wait.cmd:

  1. @ECHO OFF
  2. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  3. IF "%~1"=="" GOTO Syntax
  4. ECHO.
  5. ECHO Waiting %~1 seconds
  6. ECHO.
  7. CHOICE /C:AB /D:A /T:%1 > NUL
  8. IF ERRORLEVEL 255 (
  9. 	ECHO Invalid parameter
  10. 	GOTO Syntax
  11. )
  12. GOTO:EOF
  13.  
  14. :Syntax
  15. ECHO.
  16. ECHO WAIT for a specified number of seconds
  17. ECHO.
  18. ECHO Usage:  WAIT  n
  19. ECHO.
  20. ECHO Where:  n  =  the number of seconds to wait (1 to 99)
  21. ECHO.
  22. EXIT /B 1
  23.  

💾   Download the Wait.bat and Wait.cmd source code

CountDown

For longer delay times especially, it would be nice to let the user know what time is left.
That is why I wrote CountDown.exe (in C#): it will count down showing the number of seconds left.
Pressing any key will skip the remainder of the count down, allowing the batch file to continue with the next command.

You may append the counter output to a custom text, like this (@ECHO OFF required):

@ECHO OFF
SET /P \"=Remaining seconds to wait: \" < NUL
CountDown.exe 20

💾   Download CountDown.exe and its C# source code

SystemTrayMessage

SystemTrayMessage.exe is a program I wrote to display a tooltip message in the system tray's notification area.
By default it starts displaying a tooltip which will be visible for 10 seconds (or any timeout specified), but the program will terminate immediately after starting the tooltip. The icon will remain in the notification area after the timeout elapsed, until the mouse pointer hovers over it.
By using its optional /W switch, the program will wait for the timeout to elapse and then hide the icon before terminating.

Display a tooltip message for 60 seconds while continuing immediately:

SystemTrayMessage.exe "Your daily backup has been started" /T:"Backup Time" /V:60 /S:186
REM Insert your backup command here

Display a tooltip message and wait for 60 seconds:

SystemTrayMessage.exe "It is time for your daily backup, please save and close all documents" /T:"Backup Time" /V:60 /S:186 /W
REM Insert your backup command here

  Or more sophisticated (requires CountDown.exe too):

  1. @ECHO OFF
  2. SET Message=It is time for your daily backup.\nPlease save and close all documents,\nor press any key to skip the backup.
  3. START /B SystemTrayMessage.exe "%Message%" /T:"Backup Time" /V:20 /S:186 /W
  4. ECHO Press any key to skip the backup . . .
  5. SET /P "=Seconds to start of backup: " < NUL
  6. CountDown.exe 20
  7. IF ERRORLEVEL 2 (
  8. 	ECHO.
  9. 	ECHO Backup has been skipped . . .
  10. 	EXIT /B 1
  11. )
  12. SystemTrayMessage.exe "Your daily backup has been started" /T:"Backup Running" /V:20 /S:186
  13. REM Insert your backup command here

SystemTrayMessage screenshot

💾   Download SystemTrayMessage.exe and its C# source code

Non-DOS Scripting

  In PowerShell you can use Start-Sleep when you need a time delay.
The delay can be specified either in seconds (default) or in milliseconds.

  1. Start-Sleep -Seconds 10 # wait 10 seconds
  2. Start-Sleep 10 # wait 10 seconds
  3. Start-Sleep -Seconds 2.7 # wait 3 seconds, rounded to integer
  4. Start-Sleep -MilliSeconds 500 # wait half a second

  The following batch code uses PowerShell to generate a delay:

  1. @ECHO OFF
  2. REM %1 is the number of seconds for the delay, as specified on the command line
  3. powershell.exe -Command "Start-Sleep -Seconds %1"

  Or if you want to allow fractions of seconds:

  1. @ECHO OFF
  2. REM %1 is the number of seconds (fractions allowed) for the delay, as specified on the command line
  3. powershell.exe -Command "Start-Sleep -MilliSeconds ( 1000 * %1 )"

Note that starting PowerShell.exe in a batch file may add an extra second to the specified delay.

 

Use the SysSleep function whenever you need a time delay in Rexx scripts.
SysSleep is available in OS/2's (native) RexxUtil module and in Patrick McPhee's RegUtil module for 32-bits Windows.

 

Use the Sleep command for time delays in KiXtart scripts.

 

Use WScript.Sleep, followed by the delay in milliseconds in VBScript and JScript (unfortunately, this method is not available in HTAs).

  The following batch code uses a temporary VBScript file to generate an accurate delay:

  1. @ECHO OFF
  2. REM %1 is the number of seconds for the delay, as specified on the command line
  3. > "%Temp%.\sleep.vbs" ECHO WScript.Sleep %~1 * 1000
  4. CSCRIPT //NoLogo "%Temp%.\sleep.vbs"
  5. DEL "%Temp%.\sleep.vbs"

  Or if you want to allow the user to skip the delay:

  1. @ECHO OFF
  2. REM %1 is the number of seconds for the delay, as specified on the command line
  3. >  "%Temp%.\sleep.vbs" ECHO Set wshShell = CreateObject( "WScript.Shell" )
  4. >> "%Temp%.\sleep.vbs" ECHO ret = wshShell.Popup( "Waiting %~1 seconds", %~1, "Please Wait", vbInformation )
  5. >> "%Temp%.\sleep.vbs" ECHO Set wshShell = Nothing
  6. CSCRIPT //NoLogo "%Temp%.\sleep.vbs"
  7. DEL "%Temp%.\sleep.vbs"

UNIX Ports

Compiled versions of SLEEP are also available in these Unix ports:


page last modified: 2023-09-12; loaded in 0.0277 seconds