Windows 批处理:睡眠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4317020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows batch: sleep
提问by Claudiu
How do I get a Windows batch script to wait a few seconds?
如何让 Windows 批处理脚本等待几秒钟?
sleep
and wait
don't seem to work (unrecognized command).
sleep
并且wait
似乎不起作用(无法识别的命令)。
回答by tenfour
You can try
你可以试试
ping -n XXX 127.0.0.1 >nul
where XXX is the number of seconds to wait, plus one.
其中 XXX 是等待的秒数加一。
回答by Jaime Soto
回答by lukuluku
timeout /t 10 /nobreak > NUL
/t
specifies the time to wait in seconds
/t
指定等待的时间(以秒为单位)
/nobreak
won't interrupt the timeout if you press a key (except CTRL-C)
/nobreak
如果您按下某个键,则不会中断超时(CTRL-C 除外)
> NUL
will suppress the output of the command
> NUL
将抑制命令的输出
回答by GolezTrol
To wait 10 seconds:
等待 10 秒:
choice /T 10 /C X /D X /N
回答by emmax
Microsoft has a sleep function you can call directly.
微软有一个睡眠功能,你可以直接调用。
Usage: sleep time-to-sleep-in-seconds
sleep [-m] time-to-sleep-in-milliseconds
sleep [-c] commited-memory ratio (1%-100%)
You can just say sleep 1for example to sleep for 1 second in your batch script.
例如,您可以在批处理脚本中说sleep 1以睡眠 1 秒。
IMO Ping is a bit of a hack for this use case.
对于这个用例,IMO Ping 是一个小技巧。
回答by PA.
For a pure cmd.exe script, you can use this piece of code that returns the current time in hundreths of seconds.
对于纯 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
You may then use it in a wait loop like this.
然后您可以在像这样的等待循环中使用它。
:wait
call :gettime wait0
:w2
call :gettime wait1
set /A waitt = wait1-wait0
if !waitt! lss %1 goto :w2
goto :eof
And putting all pieces together:
并将所有部分放在一起:
@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
For a more detailed description of the commands used here, check HELP SET
and HELP CALL
information.
有关此处使用的命令的更详细说明,请查看HELP SET
和HELP CALL
信息。
回答by Alessandro Jacopson
I rely on JScript. I have a JScript file like this:
我依赖JScript。我有一个这样的 JScript 文件:
// This is sleep.js
WScript.Sleep( WScript.Arguments( 0 ) );
And inside a batch file I run it with CScript(usually it is %SystemRoot%\system32\cscript.exe
)
在批处理文件中,我使用CScript运行它(通常是%SystemRoot%\system32\cscript.exe
)
rem This is the calling inside a BAT file to wait for 5 seconds
cscript /nologo sleep.js 5000
回答by wkl
The Windows 2003 Resource Kithas a sleep
batch file. If you ever move up to PowerShell, you can use:
在Windows 2003的资源工具包有一个sleep
批处理文件。如果您升级到PowerShell,则可以使用:
Start-Sleep -s <time to sleep>
Or something like that.
或类似的东西。
回答by Claudiu
Heh, Windows is uhm... interesting. This works:
呵呵,Windows 是呃……很有趣。这有效:
choice /T 1 /d y > NUL
choice
presents a prompt asking you yes or no. /d y
makes it choose yes. /t 1
makes it wait a second before typing it. > NUL
squashes output.
choice
出现一个提示,询问您是或否。/d y
让它选择是。/t 1
让它在输入之前等待一秒钟。> NUL
挤压输出。
回答by David Heffernan
I just wrote my own sleep which called the Win32Sleep API function.
我刚刚编写了自己的 sleep,它调用了Win32 Sleep API 函数。