批处理脚本 - 在执行程序前等待 5 秒

squ*_*idg 0 sleep batch-file

我有以下脚本,但在执行软件之前它不会休眠。

有任何想法吗?

@echo off
SLEEP 10
START "" "C:\Program Files (x86)\..." 
Run Code Online (Sandbox Code Playgroud)

asc*_*pfl 6

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
    
    Run Code Online (Sandbox Code Playgroud)

    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
    
    Run Code Online (Sandbox Code Playgroud)

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