WAIT : make a batch file wait for a number of seconds

WAIT : make a batch file wait for a number of seconds

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

PING
For any MS-DOS or Windows version wit a TCP/IP client, PING can be used to delay execution for a number of seconds.
If specified (-w switch), PING will wait for a number of milliseconds between two PINGS before giving a time-out.

PING 1.1.1.1 -n 1 -w 60000 >NUL
will delay execution of the next command 60 seconds, provided 1.1.1.1 is not a valid IP address (I previously used -n 60 -w 1000 which should theoretically result in the same delay, but as Greg Hassler pointed out this may be highly inaccurate on some computers).

My previous information that you should PING to localhost using 1 extra ping was incorrect for intervals other than 1 second, as Todd Renzema pointed out to me.
He found out that if the PING does not time out, as is the case when PINGing to localhost, the next PING will be about 1 second later, no matter what time-out interval is specified.
To use the time-out specified, PING to a non-existent IP address, and do not add an extra PING. Do not PING to a non-existent host name, since that will only result in a “Unknown host” error message within a second.

And, as Les Ferch pointed out, your computer should be connected to the network for the PING delay to work, otherwise you’ll immediately get an error message, and no delay.

Summarizing: probably the safest, though not the most accurate PING based delay uses 127.0.0.1, and the standard 1 second PING interval instead of a specified time-out interval, i.e. PING 127.0.0.1 -n 6  for a 5 seconds delay.

The PING time-out technique is demonstrated in the following examples, PMSLEEP.BAT for Windows NT/2000 and PMSLPW9X.BAT for Windows 95/98:

@ECHO OFF
:: Use local environment
SETLOCAL

:: Check if a timeout period is specified
IF [%1]==[] GOTO Syntax

:: Filter out slashes, they make the IF command crash
ECHO.%1 | FIND “/” >NUL
IF NOT ERRORLEVEL 1 GOTO Syntax

:: Check if specified timeout period is within limits
IF %1 LSS 1 GOTO Syntax
IF %1 GTR 3600 GOTO Syntax

:: Check for a non-existent IP address
:: Note: this causes a small extra delay!
IF NOT DEFINED NonExist SET NonExist=10.255.255.254
PING %NonExist% -n 1 -w 100 2>NUL | FIND “TTL=” >NUL
IF NOT ERRORLEVEL 1 (
 SET NonExist=1.1.1.1
 PING 1.1.1.1 -n 1 -w 100 2>NUL | FIND “TTL=” >NUL
 IF NOT ERRORLEVEL 1 GOTO NoNonExist
)

:: Use PING time-outs to create the delay
PING %NonExist% -n 1 -w %1000 2>NUL | FIND “TTL=” >NUL

:: Show online help on errors
IF NOT ERRORLEVEL 1 GOTO NoNonExist

:: Done
GOTO End

:NoNonExist
ECHO.
ECHO This batch file needs an invalid IP address to function
ECHO correctly.
ECHO Please specify an invalid IP address in an environment
ECHO variable named NonExist and run this batch file again.

:Syntax
ECHO.
ECHO PMSleep.bat
ECHO Poor Man’s SLEEP utility,  Version 2.11 for Windows NT 4 / 2000 / XP
ECHO Wait for a specified number of seconds.
ECHO.
ECHO Usage:  CALL  PMSLEEP  nn
ECHO.
ECHO Where:  nn  is the number of seconds to wait
ECHO         nn  can range from 1 to 3600
ECHO.
ECHO Note:   Due to “overhead” the actual delay may
ECHO         prove to be up to a second longer
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO Corrected and improved by Todd Renzema, Greg Hassler and Joe Christl

:End
ENDLOCAL

 

@ECHO OFF
:: Check if a timeout period is specified
IF “%1″==”” GOTO Syntax

:: Filter out slashes, they make the IF command crash
ECHO.%1 | FIND “/” >NUL
IF NOT ERRORLEVEL 1 GOTO Syntax

:: Check for a non-existent IP address
:: Note: this causes a small extra delay!
IF “%NonExist%”==”” SET NonExist=10.255.255.254
PING %NonExist% -n 1 -w 100 | FIND “TTL=” >NUL
IF ERRORLEVEL 1 GOTO Delay
SET NonExist=1.1.1.1
PING %NonExist% -n 1 -w 100 | FIND “TTL=” >NUL
IF NOT ERRORLEVEL 1 GOTO NoNonExist

:Delay
:: Use PING time-outs to create the delay
PING %NonExist% -n 1 -w %1000 >NUL

:: Show online help on errors
IF ERRORLEVEL 1 GOTO Syntax

:: Done
GOTO End

:NoNonExist
ECHO.
ECHO This batch file needs an invalid IP address to function
ECHO correctly.
ECHO Please specify an invalid IP address in an environment
ECHO variable named NonExist and run this batch file again.

:Syntax
ECHO.
ECHO PMSlpW9x.bat
ECHO Poor Man’s SLEEP utility,  Version 2.10 for Windows 95 / 98
ECHO Wait for a specified number of seconds.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO Corrected and improved by Todd Renzema
ECHO.
ECHO Usage:    CALL PMSLPW9X nn
ECHO.
ECHO Where:    nn is the number of seconds to wait
ECHO.
ECHO Example:  CALL PMSLPW9X 10
ECHO           will wait for 10 seconds
ECHO.
ECHO Note:     Due to “overhead” the actual delay may
ECHO           prove to be up to a second longer

:End

 

CHOICE
The next batch file, WAIT.BAT, uses choice to wait for a specified number of seconds.
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 (idea borrowed from Laurence Soucy, I added the /C parameter to make it language independent).

@ECHO OFF
IF “%1″==”” GOTO Syntax
ECHO.
ECHO Waiting %1 seconds
ECHO.
REM | CHOICE /C:AB /T:A,%1 > NUL
IF ERRORLEVEL 255 ECHO Invalid parameter<BELL>
IF ERRORLEVEL 255 GOTO Syntax
GOTO End

:Syntax
ECHO.
ECHO WAIT for a specified number of seconds
ECHO.
ECHO Usage:  WAIT  n
ECHO.
ECHO Where:  n  =  the number of seconds to wait (1 to 99)
ECHO.

:End

 

Note the <BELL> character in the “Invalid parameter” message, which is ASCII character 7 (beep) or ^G (Ctrl+G).

Non-DOS Scripting
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:

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

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

@ECHO OFF
REM %1 is the number of seconds for the delay, as specified on the command line
>  “%Temp%.\sleep.vbs” ECHO Set WshShell = WScript.CreateObject( “WScript.Shell” )
>> “%Temp%.\sleep.vbs” ECHO ret = WshShell.Popup( “Waiting %~1 seconds”, %~1, “Please Wait”, vbInformation )
CSCRIPT //NoLogo “%Temp%.\sleep.vbs”
DEL “%Temp%.\sleep.vbs”

Leave a Reply

Your email address will not be published. Required fields are marked *

seven + eleven =

This site uses Akismet to reduce spam. Learn how your comment data is processed.