【问题标题】:Batch Script - Wait 5 seconds before exec program [duplicate]批处理脚本 - 在执行程序之前等待 5 秒 [重复]
【发布时间】:2024-01-17 06:31:02
【问题描述】:

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

有什么想法吗?

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

【问题讨论】:

    标签: windows batch-file cmd sleep


    【解决方案1】:

    正如其他人已经说过的,有(至少)以下选项:

    1. 要使用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
      

      注意事项:

      • timeout 实际上计算秒的倍数,因此等待时间实际上是 4 到 5 秒。对于短暂的等待时间,这尤其令人讨厌和不安。
      • 您不能在输入为redirected 的代码块中使用timeout,因为它会立即中止,并抛出错误消息ERROR: Input redirection is not supported, exiting the process immediately.。因此timeout /T 5 < nul 失败。
    2. 要使用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
      

      这是保证最短等待时间的唯一可靠方法。重定向没问题。

    【讨论】:

    • 在 powershell nul 中不起作用 - 请改用 $null。例如。 ping 127.0.0.1 -n 6 > $null
    【解决方案2】:

    试试timeout 命令。

    超时/t 10

    这显示Waiting for 10 seconds, press a key to continue ...

    您可以使用忽略用户输入的/nobreak 开关(CTRL-C 除外)

    timeout /t 30 /nobreak
    

    或者您可以将其输出重定向到 NUL 以等待 10 秒并忽略用户输入的空白​​屏幕:

    timeout /t 30 /nobreak > NUL 
    

    【讨论】:

      最近更新 更多