Waiting (or “sleeping”) in a batch file

Commands like timeout.exe were added to Windows to make waiting in a batch file easier. Unfortunately timeout.exe still leaves much to be desired. There are several alternatives people use to simulate a delay in the Windows Command Prompt (cmd.exe).

Below you can see an analysis of the suggestions from this StackOverflow post, along with one additional suggestion I have that will increase precision (method 1 below).

The key factors I’m analyzing is the time precision of a command and I attempt to determine the compatibility with past versions of Windows. I also want this to work out-of-the-box with Windows and not require an external executable to be loaded.

My test consisted of the following methods:

  • Method 1: Custom Function
    Command: call :waitfor 5000>nul (see below for implementation details)
  • Method 2: Ping.exe (dummy IP, timeout)
    Command: ping.exe -n 1 -w 5000 >nul 2>&1
  • Method 3: Ping.exe (localhost, 1 sec between pings)
    Command: ping.exe -n 6 -w 1 >nul 2>&1
  • Method 4: Powershell.exe
    Command: powershell.exe -command "Start-Sleep -Milliseconds 5000">nul
  • Method 5: Timeout.exe
    Command: timeout.exe /t 5 /nobreak >nul 2>&1
  • Method 6: VBScript (cscript.exe)
    Command: echo WScript.Sleep^(WScript.Arguments^(0^)^) >"%temp%sleep.vbs" && cscript "%temp%sleep.vbs" 5000 >nul

Continue reading “Waiting (or “sleeping”) in a batch file”