Windows的截图工具可以捕捉屏幕,但有时候我想在五秒后捕捉屏幕,比如拍摄通过网络摄像头显示的图像(例如运行脚本并对着摄像头微笑)。
我如何在批处理文件中等待五秒钟?
Windows的截图工具可以捕捉屏幕,但有时候我想在五秒后捕捉屏幕,比如拍摄通过网络摄像头显示的图像(例如运行脚本并对着摄像头微笑)。
我如何在批处理文件中等待五秒钟?
C:\> timeout 5
timeout 5
的意思是:
休眠时间在4到5秒之间
可以通过将以下内容放入批处理文件中,重复运行并计算第一个和第二个echo
之间的时间差来进行实证验证:
@echo off
echo %time%
timeout 5 > NUL
echo %time%
timeout
命令无效:"ERROR: Input redirection is not supported, exiting the process immediately." Daniel
一个技巧是(滥用)ping命令:
ping 127.0.0.1 -n 6 > nul
说明:
ping
是一个发送 ping 请求的系统实用程序。在所有版本的 Windows 上都可以使用 ping
。127.0.0.1
是 localhost 的 IP 地址。此 IP 地址保证始终能够解析、可达并立即响应 ping。-n 6
指定要发送 6 个 ping。每个 ping 之间有 1 秒的延迟,因此要进行 5 秒的延迟,需要发送 6 个 ping。> nul
将 ping
的输出 redirecting 到 nul
,从而抑制其输出。ping -n 6 127.0.0.1 > nul
因为ping命令在每次发送请求后会等待一秒钟,所以你需要指定比你实际需要的次数多一次。
尝试使用Choice命令。它从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."
choice.com
,它在Windows XP中可以正常工作。 - Radon8472如果您的系统中安装了PowerShell,您可以执行以下命令:
如果您的系统中安装了PowerShell,您可以执行以下命令:powershell -command "Start-Sleep -s 5"
编辑:有人提出了一个问题,即PowerShell启动所需的时间与您尝试等待的时间相比显著。如果等待时间的准确性很重要(即一两秒钟的额外延迟是不可接受的),则可以使用此方法:
powershell -command "$sleepUntil = [DateTime]::Parse('%date% %time%').AddSeconds(5); $sleepDuration = $sleepUntil.Subtract((get-date)).TotalMilliseconds; start-sleep -m $sleepDuration"
这会获取Windows命令发出的时间,然后PowerShell脚本会休眠,直到超过该时间5秒。只要PowerShell启动所需的时间少于您设置的休眠时间,此方法就可以起作用(在我的机器上大约为600毫秒)。
你可以使用 timeout
命令来实现:
这个会显示: timeout 5
这个不会显示: timeout 5 >nul
我们不能使用waitfor /T 180
吗?
waitfor /T 180 pause
会导致"ERROR: Timed out waiting for 'pause'."
waitfor /T 180 pause >nul
将会掩盖这个"错误"
waitfor
命令应该在Win95之后的Windows操作系统中都存在。
过去,我下载了一个名为sleep
的可执行文件,可以在将其放入路径后在命令行上运行它。
例如:sleep shutdown -r -f /m \\yourmachine
虽然现在的shutdown已经内置了-t选项。
2>
来吞噬错误,例如:waitfor /T 5 pause 2>NUL
。 - Archimedes TrajanoTimeout /t 1 >nul
ping 1.1.1.1 -n 1 -w 1000 >nul
网站/服务器/主机/IP
,但是它做不到。所以我建议使用timeout。祝你好运!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
SLEEP <seconds>
SLEEP -m <time in milliseconds>
这比TIMEOUT
更有用,因为TIMEOUT
可以通过按键或CTRL + C
(对于TIMEOUT /t <secs> /nobreak
)来中止。 SLEEP
无法被任何东西中止(除了关闭按钮:p)
另一个是PING
。但是PING
需要互联网连接,因为您将记录站点的连接。
TIMEOUT
。参考文献:SS64,Rob van der Woude。 - Gras Doublepowershell Start-Sleep -m 50
。 - Pavel Ptimeout /t 5 /nobreak >nul
。它会等待5秒钟,然后静默结束,不会有任何输出。 - user9556248node -e 'setTimeout(a => a, 5000)'
来玩一下。它适用于Mac上的Node v12.14.0,可能在Windows上也适用。 - nonopolarity