我该如何让Windows批处理脚本等待数秒钟?
使用“sleep”和“wait”命令似乎无效(出现“未识别的命令”提示)。
使用“sleep”和“wait”命令似乎无效(出现“未识别的命令”提示)。
你可以尝试
ping -n XXX 127.0.0.1 >nul
其中XXX是等待的秒数加一。
timeout /t 10 /nobreak > NUL
/t
指定等待的时间,单位为秒
/nobreak
如果你按下一个键(除了CTRL-C),不会中断超时
> NUL
将抑制命令的输出
/t
等待时间(秒),/nobreak
不会在按下按钮时中断超时(除了 CTRL-C
),> NUL
将抑制命令的输出。 - lukuluku等待10秒钟:
choice /T 10 /C X /D X /N
> NUL
来抑制所有输出。 - ClaudiuChoice
在 Windows XP 上是可用的,但默认情况下未安装。它并没有在 Windows 2003 中引入,因为它存在于 Windows 的早期消费版本(95、98)甚至 MSDOS 6.0 中。当他们从 Windows 2000 开始将消费者版本与 NT 版本结合时,他们可能只是忘记了它。;-) - GolezTrol微软提供了一个可以直接调用的睡眠函数。
Usage: sleep time-to-sleep-in-seconds
sleep [-m] time-to-sleep-in-milliseconds
sleep [-c] commited-memory ratio (1%-100%)
你可以在批处理脚本中使用 sleep 1 命令来让程序休眠1秒。
我认为对于这种情况来说,ping 命令有点不太合适。
对于一个纯的cmd.exe脚本,您可以使用以下代码来返回以百分之一秒为单位的当前时间。
:gettime
set hh=%time:~0,2%
set mm=%time:~3,2%
set ss=%time:~6,2%
set cc=%time:~-2%
set /A %1=hh*360000+mm*6000+ss*100+cc
goto :eof
:wait
call :gettime wait0
:w2
call :gettime wait1
set /A waitt = wait1-wait0
if !waitt! lss %1 goto :w2
goto :eof
将所有的部分组合在一起:
@echo off
setlocal enableextensions enabledelayedexpansion
call :gettime t1
echo %t1%
call :wait %1
call :gettime t2
echo %t2%
set /A tt = (t2-t1)/100
echo %tt%
goto :eof
:wait
call :gettime wait0
:w2
call :gettime wait1
set /A waitt = wait1-wait0
if !waitt! lss %1 goto :w2
goto :eof
:gettime
set hh=%time:~0,2%
set mm=%time:~3,2%
set ss=%time:~6,2%
set cc=%time:~-2%
set /A %1=hh*360000+mm*6000+ss*100+cc
goto :eof
如果需要更详细的命令描述,请查看HELP SET
和HELP CALL
信息。
timeout x >nul
(将x替换为要等待的秒数)。 - Matt嘿,Windows 呃... 很有趣。这个可以:
choice /T 1 /d y > NUL
choice
提供一个提示,询问您是否是肯定的答复。 /d y
使其选择“是”。/t 1
使其在键入之前等待一秒钟。> NUL
压制输出。
Windows 2003 资源工具包 中有一个 sleep
批处理文件。如果您使用 PowerShell,您可以使用以下命令:
Start-Sleep -s <time to sleep>
或者类似那样。
powershell -command "Start-Sleep -Milliseconds 5000">nul
- Mister Henson我刚刚写了一个自己的睡眠函数,它叫作Win32 Sleep API函数。
-n
用于指示请求的数量。即使回复可能在更短的时间内到达,ping
默认每次等待一个回复1秒钟。 - Jaime Soto