nop*_*ole 660 timeout batch-file
Windows的截图工具可以捕获屏幕,但有时我想在5秒后捕获屏幕,例如拍摄网络摄像头显示的图像.(例如,运行脚本并对相机微笑).
我的问题是:如何在批处理文件中睡5秒?
asv*_*kau 1390
没有人提到我,我感到非常惊讶:
C:\> timeout 5
Run Code Online (Sandbox Code Playgroud)
NB请注意(感谢Dan!)这timeout 5意味着:
睡眠时间在4到5秒之间
这可以通过将以下内容放入批处理文件,重复运行并计算第一个和第二个echos 之间的时间差来凭经验验证:
@echo off
echo %time%
timeout 5 > NUL
echo %time%
Run Code Online (Sandbox Code Playgroud)
Mar*_*tin 817
一个hack是(错误)使用ping命令:
ping 127.0.0.1 -n 6 > nul
Run Code Online (Sandbox Code Playgroud)
说明:
ping是一个发送ping请求的系统实用程序.ping适用于所有版本的Windows.127.0.0.1是localhost的IP地址.保证此IP地址始终可以解析,可以访问,并立即响应ping.-n 6指定有6个ping.每次ping之间有1s延迟,因此对于5s延迟,您需要发送6次ping.> nulping通过重定向来抑制输出nul.Ada*_*rad 69
尝试选择命令.它自MSDOS 6.0以来一直存在,应该可以解决问题.
使用/ T参数指定超时(以秒为单位)和/ D参数指定默认选择,然后忽略选择的选项.
可能存在问题的一件事是,如果用户在超时期限过去之前键入其中一个选择字符.部分解决方法是混淆情况 - 使用/ N参数隐藏有效选择列表,并且在选择集中只有1个字符,这样用户在输入之前输入有效选项的可能性就会降低.超时到期.
以下是Windows Vista上的帮助文本.我认为它在XP上是一样的,但请查看XP计算机上的帮助文本进行验证.
C:\>CHOICE /?
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
Description:
This tool allows users to select one item from a list
of choices and returns the index of the selected choice.
Parameter List:
/C choices Specifies the list of choices to be created.
Default list is "YN".
/N Hides the list of choices in the prompt.
The message before the prompt is displayed
and the choices are still enabled.
/CS Enables case-sensitive choices to be selected.
By default, the utility is case-insensitive.
/T timeout The number of seconds to pause before a default
choice is made. Acceptable values are from 0 to
9999. If 0 is specified, there will be no pause
and the default choice is selected.
/D choice Specifies the default choice after nnnn seconds.
Character must be in the set of choices specified
by /C option and must also specify nnnn with /T.
/M text Specifies the message to be displayed before
the prompt. If not specified, the utility
displays only a prompt.
/? Displays this help message.
NOTE:
The ERRORLEVEL environment variable is set to the index of the
key that was selected from the set of choices. The first choice
listed returns a value of 1, the second a value of 2, and so on.
If the user presses a key that is not a valid choice, the tool
sounds a warning beep. If tool detects an error condition,
it returns an ERRORLEVEL value of 255. If the user presses
CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value
of 0. When you use ERRORLEVEL parameters in a batch program, list
them in decreasing order.
Examples:
CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."
Run Code Online (Sandbox Code Playgroud)
noc*_*ura 55
以下黑客让你睡了5秒钟
ping -n 6 127.0.0.1 > nul
Run Code Online (Sandbox Code Playgroud)
由于ping在ping之间等待一秒钟,因此您必须指定一个比您需要的更多的ping.
Nia*_*ton 41
如果您的系统上有PowerShell,则可以执行以下命令:
powershell -command "Start-Sleep -s 5"
Run Code Online (Sandbox Code Playgroud)
编辑:人们提出了一个问题,即PowerShell启动所需的时间与您尝试等待的时间相比是显着的.如果等待时间的准确性很重要(即不能接受额外的两秒或两个延迟),您可以使用以下方法:
powershell -command "$sleepUntil = [DateTime]::Parse('%date% %time%').AddSeconds(5); $sleepDuration = $sleepUntil.Subtract((get-date)).TotalMilliseconds; start-sleep -m $sleepDuration"
Run Code Online (Sandbox Code Playgroud)
这将花费发出Windows命令的时间,并且powershell脚本会在该时间之后休眠5秒.因此,只要powershell启动的时间少于睡眠持续时间,这种方法就可以工作(我的机器上的时间大约为600毫秒).
Ale*_*lex 38
你可以用timeout:
这将是可见的: timeout 5
这将不可见 timeout 5 >nul
小智 30
我们不能waitfor /T 180吗?
waitfor /T 180 pause 将导致"错误:超时等待'暂停'."
waitfor /T 180 pause >nul 将扫除地毯下的"错误"
waitfor在Win95之后,该命令应该在Windows操作系统中
在过去,我已经下载了一个名为的可执行文件sleep,它可以在您将其放入路径后在命令行上运行.
例如:sleep shutdown -r -f /m \\yourmachine
虽然shutdown现在内置了-t选项
小智 24
Timeout /t 1 >nul
就像在1秒内暂停一样,你可以将点击率提高到接近100.000(99.999)秒.如果您连接到互联网,最佳解决方案是:
ping 1.1.1.1. -n 1 -w 1000 >nul
当您ping时,以毫秒为单位计算,因此一秒钟将为1000毫秒.但ping命令有点不确定,它在离线机器上的工作方式不同.问题是机器因为离线而感到困惑,并且想要ping一个website/server/host/ip,但它不能.所以我建议超时.祝好运!
Ami*_*mir 21
SLEEP 5 包含在一些Windows资源工具包中.
TIMEOUT 5 一些Windows资源工具包中包含了它,但现在是Windows 7和8中的标准命令(不确定Vista).
PING 1.1.1.1 -n 1 -w 5000 >NUL 对于具有TCP/IP客户端的任何MS-DOS或Windows版本,PING可用于延迟执行数秒.
NETSH badcommand (仅限Windows XP/Server 2003)或 CHOICE
Ned*_*tin 12
两个答案:
首先,要延迟批处理文件,简单地没有人们提出的所有钝方法:
timeout /t <TimeoutInSeconds> [/nobreak]
Run Code Online (Sandbox Code Playgroud)
http://technet.microsoft.com/en-us/library/cc754891.aspx
其次,值得一提的是,虽然它可能无法完全满足您的需求,但使用内置的Windows剪切工具,您可以在不使用鼠标的情况下触发剪辑.运行剪切工具,从当前剪辑中退出但保持工具运行,并在需要剪辑时按"控制+打印"屏幕.这不应该干扰你想要剪断的东西.
scr*_*e02 12
有!
SLEEP <seconds>
Run Code Online (Sandbox Code Playgroud)
如果你想以毫秒模式运行:
SLEEP -m <time in milliseconds>
Run Code Online (Sandbox Code Playgroud)
这比TIMEOUT因为TIMEOUT可以通过单击键或CTRL + C(for TIMEOUT /t <secs> /nobreak)中止而更有帮助。SLEEP不能被任何东西中止(关闭按钮除外:p)
另一个是PING。但PING需要互联网连接,因为您将记录站点的连接。
gre*_*mac 11
我试图在msbuild任务中执行此操作,并且由于I/O重定向,选择和超时都不起作用.
我最终使用了来自http://sourceforge.net/projects/unxutils的 sleep.exe ,这很不错,因为它不需要任何安装,而且很小.
尝试choice:
<Target Name="TestCmd">
<Exec Command="choice /C YN /D Y /t 5 " />
</Target>
Run Code Online (Sandbox Code Playgroud)
结果是:
TestCmd:
choice /C YN /D Y /t 5
EXEC : error : The file is either empty or does not contain the valid choices. [test.proj]
[Y,N]?
C:\test.proj(5,9): error MSB3073: The command "choice /C YN /D Y /t 5 " exited with code 255.
Run Code Online (Sandbox Code Playgroud)
尝试timeout:
<Target Name="TestCmd">
<Exec Command="timeout /t 5 " />
</Target>
Run Code Online (Sandbox Code Playgroud)
结果是:
TestCmd:
timeout /t 5
EXEC : error : Input redirection is not supported, exiting the process immediately. [test.proj]
C:\test.proj(5,7): error MSB3073: The command "timeout /t 5 " exited with code 1.
Run Code Online (Sandbox Code Playgroud)
在旁边:
我实际上正在使用,<Exec Command="sleep 2 & dbghost.exe" />因为我并行多次执行dbghost.exe并且它基于当前的纪元时间以秒为单位创建临时文件/数据库 - 这当然意味着如果你启动多个实例,每个实例都使用相同的临时名称.我原本试图使用MSBuild Extension Pack Thread.Sleep命令,但似乎(通常)它正在运行睡眠任务,但随后<exec>在所有线程中同时启动任务,当然dbghost.exe会因冲突而失败.到目前为止,使用sleep.exe似乎更可靠.
这是我在实践中使用的最新版本,在脚本完成时看到输出的十秒钟暂停.
BEST>@echo done
BEST>@set DelayInSeconds=10
BEST>@rem Use ping to wait
BEST>@ping 192.0.2.0 -n 1 -w %DelayInSeconds%000 > nul
Run Code Online (Sandbox Code Playgroud)
回显完成允许我查看脚本何时完成并且ping提供延迟.额外的@符号意味着我看到"完成"文本并且等待发生而不会被他们的命令分心.
我已经在XP机器上尝试了各种解决方案,因为我们的想法是拥有一个可以在各种机器上运行的批处理文件,因此我选择了XP机器作为可能最不具备能力的环境.
GOOD> ping 192.0.2.0 -n 1 -w 3000 > nul
Run Code Online (Sandbox Code Playgroud)
这似乎按预期延迟了三秒钟.一次ping尝试持续指定的3秒.
BAD> ping -n 5 192.0.2.0 > nul
Run Code Online (Sandbox Code Playgroud)
这需要大约10秒钟(不是5秒).我的解释是有5次ping尝试,每次尝试大约相隔一秒,制作4秒.并且每次ping尝试可能持续大约一秒钟,总计估计为9秒.
BAD> timeout 5
BAD> sleep /w2000
BAD> waitfor /T 180
BAD> choice
Run Code Online (Sandbox Code Playgroud)
命令不可用.
BAD> ping 192.0.2.0 -n 1 -w 10000 > nul :: wait 10000 milliseconds, ie 10 secs
Run Code Online (Sandbox Code Playgroud)
我也尝试了上述内容,在阅读之后,可以使用两个连续的冒号将注释添加到BAT文件中.然而,该软件几乎立即返回.在ping之前将注释放在自己的行上工作正常.
GOOD> :: wait 10000 milliseconds, ie 10 secs
GOOD> ping 192.0.2.0 -n 1 -w 10000 > nul
Run Code Online (Sandbox Code Playgroud)
为了更好地理解ping在实践中的作用,我跑了
ping 192.0.2.0 -n 5 -w 5000
Run Code Online (Sandbox Code Playgroud)
这需要大约30秒,即使5*5 = 25.我的解释是有5次ping尝试,每次持续5秒,但是在ping尝试之间有大约1秒的时间延迟:如果你立即再次ping,并且最好有理由期待不同的结果,那么最好给出一个网络有一点时间从它遇到的任何问题中恢复过来.
编辑:从另一个帖子中窃取,.. RFC 3330说IP地址192.0.2.0不应该出现在互联网上,所以ping这个地址会阻止这些测试给任何人发送垃圾邮件!我相应地修改了上面的文字!
XP和更高版本上的所有东西都可以使用的两种其他方式:
与w32tm
w32tm /stripchart /computer:localhost /period:5 /dataonly /samples:2 1>nul
Run Code Online (Sandbox Code Playgroud)
使用typeperf:
typeperf "\System\Processor Queue Length" -si 5 -sc 1 >nul
Run Code Online (Sandbox Code Playgroud)
您可以使用VBScript,例如 file myscript.vbs:
set wsobject = wscript.createobject("wscript.shell")
do while 1=1
wsobject.run "SnippingTool.exe",0,TRUE
wscript.sleep 3000
loop
Run Code Online (Sandbox Code Playgroud)
批处理文件:
cscript myscript.vbs %1
Run Code Online (Sandbox Code Playgroud)
小智 5
用户Aacini提出的代码的改进,它具有百分之一秒的分辨率并且在时间达到23:59:59,99时不会失败:
for /f "tokens=1,2,3,4 delims=:," %%A in ("%TIME%") do set /a HH=%%A, MM=1%%B-100, SS=1%%C-100, CC=1%%D-100, TBASE=((HH*60+MM)*60+SS)*100+CC
:: Example delay 1 seg.
set /a TFIN=%TBASE%+100
:ESPERAR
for /f "tokens=1,2,3,4 delims=:," %%A in ("%TIME%") do set /a HH=%%A, MM=1%%B-100, SS=1%%C-100, CC=1%%D-100, TACTUAL=((HH*60+MM)*60+SS)*100+CC
if %TACTUAL% lss %TBASE% set /a TACTUAL=%TBASE%+%TACTUAL%
if %TACTUAL% lss %TFIN% goto ESPERAR
Run Code Online (Sandbox Code Playgroud)
小智 5
我做的.它正在工作,并在几秒钟内显示时间.如果要使用它,请添加到批处理文件:
call wait 10
Run Code Online (Sandbox Code Playgroud)
当我测试它时,它正在工作.
列表wait.bat(它必须在工作目录中)或者windir/system32/:
@echo off
set SW=00
set SW2=00
set /a Sec=%1-1
set il=00
@echo Wait %1 second
for /f "tokens=1,2,3,4 delims=:," %%A in ("%TIME%") do set /a HH=%%A, MM=1%%B-100, SS=1%%C-100, CC=1%%D-100, TBASE=((HH*60+MM)*60+SS)*100+CC, SW=CC
set /a TFIN=%TBASE%+%100
:ESPERAR
for /f "tokens=1,2,3,4 delims=:," %%A in ("%TIME%") do set /a HH=%%A, MM=1%%B-100, SS=1%%C-100,
CC=1%%D-100, TACTUAL=((HH*60+MM)*60+SS)*100+CC, SW2=CC
if %SW2% neq %SW% goto notype
if %il%==0 (echo Left %Sec% second & set /a Sec=sec-1 & set /a il=il+1)
goto no0
:notype
set /a il=0
:no0
if %TACTUAL% lss %TBASE% set /a TACTUAL=%TBASE%+%TACTUAL%
if %TACTUAL% lss %TFIN% goto ESPERAR
Run Code Online (Sandbox Code Playgroud)