Search code examples
windowsbatch-filecmdsleep

Batch Script - Wait 5 seconds before exec program


I have the below script but it does not sleep before executing the software.

Any ideas?

@echo off
SLEEP 10
START "" "C:\Program Files (x86)\..." 

Solution

  • There are (at least) the following options, as others already stated:

    1. To use the timeout command:

      rem // Allow a key-press to abort the wait; `/T` can be omitted:
      timeout /T 5
      timeout 5
      rem // Do not allow a key-press to abort the wait:
      timeout /T 5 /NOBREAK
      rem // Suppress the message `Waiting for ? seconds, press a key to continue ...`:
      timeout /T 5 /NOBREAK > nul
      

      Caveats:

      • timeout actually counts second multiples, therefore the wait time is actually something from 4 to 5 seconds. This is particularly annoying and disturbing for short wait times.
      • You cannot use timeout in a block of code whose input is redirected, because it aborts immediately, throwing the error message ERROR: Input redirection is not supported, exiting the process immediately.. Hence timeout /T 5 < nul fails.
    2. To use the ping command:

      rem /* The IP address 127.0.0.1 (home) always exists;
      rem    the standard ping interval is 1 second; so you need to do
      rem    one more ping attempt than you want intervals to elapse: */
      ping 127.0.0.1 -n 6 > nul
      

      This is the only reliable way to guarantee the minimum wait time. Redirection is no problem.