如何在批处理文件/cmd中休眠五秒钟

发布于 2024-08-09 17:33:59 字数 99 浏览 4 评论 0原文

Windows 的截图工具可以捕获屏幕,但有时我想在五秒后捕获屏幕,例如拍摄网络摄像头显示的图像。 (例如,运行脚本并对着镜头微笑。)

如何在批处理文件中休眠 5 秒?

Windows's Snipping tool can capture the screen, but sometimes I want to capture the screen after five seconds, such as taking an image being displayed by the webcam. (Run the script and smile at the camera, for example.)

How do I sleep for 5 seconds in a batch file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(29

宫墨修音 2024-08-16 17:34:00

在 Windows xp sp3 中,您可以使用 sleep 命令

In Windows xp sp3 you can use sleep command

陌若浮生 2024-08-16 17:34:00

创建一个名为 sleep.cmd 的 cmd 文件:

REM Usage: SLEEP Time_in_MiliSECONDS
@ECHO off
ping 1.0.0.0 -n 1 -w %1 > nul

将 sleep.cmd 复制到 c:\windows\system32

用法:

sleep 500

休眠 0.5 秒。女士的论据。一旦复制到System32,就可以在任何地方使用。

编辑:您还应该注意,如果机器未连接到网络(例如您在地铁中使用的便携式设备),则 ping 技巧将不再起作用。

Make a cmd file called sleep.cmd:

REM Usage: SLEEP Time_in_MiliSECONDS
@ECHO off
ping 1.0.0.0 -n 1 -w %1 > nul

Copy sleep.cmd to c:\windows\system32

Usage:

sleep 500

Sleeps for 0.5 seconds. Arguments in ms. Once copied to System32, can be used everywhere.

EDIT: You should also be away that if the machine isn't connected to a network (say a portable that your using in the subway), the ping trick doesn't really work anymore.

失而复得 2024-08-16 17:34:00

好吧,如果你有选择或 ping,这很有效。

@echo off
echo.
if "%1"=="" goto askq
if "%1"=="/?" goto help
if /i "%1"=="/h" goto help
if %1 GTR 0 if %1 LEQ 9999 if /i "%2"=="/q" set ans1=%1& goto quiet
if %1 GTR 0 if %1 LEQ 9999 set ans1=%1& goto breakout
if %1 LEQ 0 echo %1 is not a valid number & goto help
if not "%1"=="" echo.&echo "%1" is a bad parameter & goto help
goto end

:help
echo SLEEP runs interactively (by itself) or with parameters (sleep # /q )
echo where # is in seconds, ranges from 1 - 9999
echo Use optional parameter /q to suppress standard output 
echo or type /h or /? for this help file
echo.
goto end

:askq
set /p ans1=How many seconds to sleep? ^<1-9999^> 
echo.
if "%ans1%"=="" goto askq
if %ans1% GTR 0 if %ans1% LEQ 9999 goto breakout
goto askq

:quiet
choice /n /t %ans1% /d n > nul
if errorlevel 1 ping 1.1.1.1 -n 1 -w %ans1%000 > nul
goto end

:breakout
choice /n /t %ans1% /d n > nul
if errorlevel 1 ping 1.1.1.1 -n 1 -w %ans1%000 > nul
echo Slept %ans1% second^(s^)
echo.

:end

只需将其命名为 sleep.cmd 或 sleep.bat 并运行它

Well this works if you have choice or ping.

@echo off
echo.
if "%1"=="" goto askq
if "%1"=="/?" goto help
if /i "%1"=="/h" goto help
if %1 GTR 0 if %1 LEQ 9999 if /i "%2"=="/q" set ans1=%1& goto quiet
if %1 GTR 0 if %1 LEQ 9999 set ans1=%1& goto breakout
if %1 LEQ 0 echo %1 is not a valid number & goto help
if not "%1"=="" echo.&echo "%1" is a bad parameter & goto help
goto end

:help
echo SLEEP runs interactively (by itself) or with parameters (sleep # /q )
echo where # is in seconds, ranges from 1 - 9999
echo Use optional parameter /q to suppress standard output 
echo or type /h or /? for this help file
echo.
goto end

:askq
set /p ans1=How many seconds to sleep? ^<1-9999^> 
echo.
if "%ans1%"=="" goto askq
if %ans1% GTR 0 if %ans1% LEQ 9999 goto breakout
goto askq

:quiet
choice /n /t %ans1% /d n > nul
if errorlevel 1 ping 1.1.1.1 -n 1 -w %ans1%000 > nul
goto end

:breakout
choice /n /t %ans1% /d n > nul
if errorlevel 1 ping 1.1.1.1 -n 1 -w %ans1%000 > nul
echo Slept %ans1% second^(s^)
echo.

:end

just name it sleep.cmd or sleep.bat and run it

执妄 2024-08-16 17:34:00

ping 会在超时之前等待大约5 秒,而不是如上所述的 1 秒。
也就是说,除非您告诉它在超时之前仅等待 1 秒。

ping 1.0.0.1 -n 1 -w 1000

将 ping 一次,仅等待 1 秒(1000 毫秒)响应,然后超时。

因此大约 20 秒的延迟将是:

ping 1.0.0.1 -n 20 -w 1000

ping waits for about 5 seconds before timing out, not 1 second as was stated above.
That is, unless you tell it to only wait for 1 second before timing out.

ping 1.0.0.1 -n 1 -w 1000

will ping once, wait only 1 second (1000 ms) for a response, then time out.

So an approximately 20-second delay would be:

ping 1.0.0.1 -n 20 -w 1000

千笙结 2024-08-16 17:34:00

我编写了一个 powerbasic 程序 wait.exe,您可以在批处理文件中向其传递一个毫秒参数,

wait 3000
system('c:/windows/system32/SnippingTool.exe')

即 EXE 代码:

FUNCTION PBMAIN()
  c$ = Command$
  s! = Val(c$)*1000
  Sleep s!         
END FUNCTION

I wrote a powerbasic program wait.exe, where you pass a millisecond parameter to it in your batch file

wait 3000
system('c:/windows/system32/SnippingTool.exe')

the code for the EXE:

FUNCTION PBMAIN()
  c$ = Command$
  s! = Val(c$)*1000
  Sleep s!         
END FUNCTION
季末如歌 2024-08-16 17:34:00

在较新的 Windows 操作系统版本上,您可以在 DOS 脚本(.cmd.bat)中使用该命令

sleep /w2000

等待 2 秒(2000 毫秒 - 替换您需要的时间(以毫秒为单位)) )。请小心包含 /w 参数 - 如果没有它,整个计算机就会进入睡眠状态!如果您愿意,可以使用 -m 代替 /m,并且可以选择在 w 和数字之间使用冒号 (:)。

On newer Windows OS versions you can use the command

sleep /w2000

in a DOS script (.cmd or .bat) to wait for 2s (2000 ms - substitute the time in ms you need). Be careful to include the /w argument - without it the whole computer is put to sleep! You can use -m instead of /m if you wish and optionally a colon (:) between the w and the number.

风苍溪 2024-08-16 17:34:00

我认为以下命令可以提供帮助:

pause 5

暂停命令的语法是:
暂停d \\其中d代表持续时间(以秒为单位)

我正在使用Windows 7(32位),但我不知道其他的。

I think the following command can help:

pause 5

The syntax of the pause command is:
pause d \\where d represents the duration in seconds

I am using Windows 7 (32 bit), but I don't know about the others.

邮友 2024-08-16 17:33:59

我很惊讶没有人提到:

C:\> timeout 5

注意但是请注意(感谢丹!)超时 5 的意思是:

在 4 到 5 秒之间的任何地方睡觉

这可以通过将以下内容放入批处理文件中,重复运行并计算第一个和第二个 echo 之间的时间差来凭经验验证:

@echo off
echo %time%
timeout 5 > NUL
echo %time%

另请注意这一点,来自评论:
timeout 在非交互式脚本中不起作用:“错误:不支持输入重定向,立即退出进程。” 丹尼尔

I'm very surprised no one has mentioned:

C:\> timeout 5

N.B. Please note however (thanks Dan!) that timeout 5 means:

Sleep anywhere between 4 and 5 seconds

This can be verified empirically by putting the following into a batch file, running it repeatedly and calculating the time differences between the first and second echos:

@echo off
echo %time%
timeout 5 > NUL
echo %time%

Also note this, from the comments:
timeout does not work in non-interactive scripts: "ERROR: Input redirection is not supported, exiting the process immediately." Daniel

小鸟爱天空丶 2024-08-16 17:33:59

一种技巧是(错误)使用 ping 命令:

ping 127.0.0.1 -n 6 > nul

解释:

  • ping 是一个发送 ping 请求的系统实用程序。 ping 可在所有版本的 Windows 上使用。
  • 127.0.0.1localhost 的 IP 地址。该 IP 地址保证始终可解析、可访问并立即响应 ping。
  • -n 6 指定有 6 个 ping。每个 ping 之间有 1 秒的延迟,因此对于 5 秒的延迟,您需要发送 6 个 ping。
  • <代码>> nul 通过重定向ping的输出> 它到 nul

One hack is to (mis)use the ping command:

ping 127.0.0.1 -n 6 > nul

Explanation:

  • ping is a system utility that sends ping requests. ping is available on all versions of Windows.
  • 127.0.0.1 is the IP address of localhost. This IP address is guaranteed to always resolve, be reachable, and immediately respond to pings.
  • -n 6 specifies that there are to be 6 pings. There is a 1s delay between each ping, so for a 5s delay you need to send 6 pings.
  • > nul suppress the output of ping, by redirecting it to nul.
江南月 2024-08-16 17:33:59

下面的 hack 让你休眠 5 秒,

ping -n 6 127.0.0.1 > nul

因为 ping 在两次 ping 之间等待一秒,所以你必须多指定一个比你需要的多一个。

The following hack let's you sleep for 5 seconds

ping -n 6 127.0.0.1 > nul

Since ping waits a second between the pings, you have to specify one more than you need.

谈情不如逗狗 2024-08-16 17:33:59

尝试使用 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."

Try the Choice command. It's been around since MSDOS 6.0, and should do the trick.

Use the /T parameter to specify the timeout in seconds and the /D parameter to specify the default selection and ignore then selected choice.

The one thing that might be an issue is if the user types one of the choice characters before the timeout period elapses. A partial work-around is to obfuscate the situation -- use the /N argument to hide the list of valid choices and only have 1 character in the set of choices so it will be less likely that the user will type a valid choice before the timeout expires.

Below is the help text on Windows Vista. I think it is the same on XP, but look at the help text on an XP computer to verify.

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."
暖心男生 2024-08-16 17:33:59

如果您的系统上有 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 毫秒)。

If you've got PowerShell on your system, you can just execute this command:

powershell -command "Start-Sleep -s 5"

Edit: people raised an issue where the amount of time powershell takes to start is significant compared to how long you're trying to wait for. If the accuracy of the wait time is important (ie a second or two extra delay is not acceptable), you can use this approach:

powershell -command "$sleepUntil = [DateTime]::Parse('%date% %time%').AddSeconds(5); $sleepDuration = $sleepUntil.Subtract((get-date)).TotalMilliseconds; start-sleep -m $sleepDuration"

This takes the time when the windows command was issued, and the powershell script sleeps until 5 seconds after that time. So as long as powershell takes less time to start than your sleep duration, this approach will work (it's around 600ms on my machine).

月隐月明月朦胧 2024-08-16 17:33:59

您可以使用timeout来实现:

这将是可见的:timeout 5

这将是不可见的timeout 5 >nul

You can make it with timeout:

This will be visible: timeout 5

This will not be visible timeout 5 >nul

不羁少年 2024-08-16 17:33:59

我们不能等待/T 180吗?

waitfor /T 180pause 将导致“错误:等待‘暂停’超时。”

waitfor /T 180 Pause >nul 将把这个“错误”掩盖起来

waitfor 命令应该在 Win95 之后的 Windows 操作系统中存在

过去我下载过一个名为 sleep 的可执行文件,将其放入路径后将在命令行上运行。

例如:睡眠关闭 -r -f /m \\yourmachine
虽然 shutdown 现在内置了 -t 选项

Can't we do waitfor /T 180?

waitfor /T 180 pause will result in "ERROR: Timed out waiting for 'pause'."

waitfor /T 180 pause >nul will sweep that "error" under the rug

The waitfor command should be there in Windows OS after Win95

In the past I've downloaded a executable named sleep that will work on the command line after you put it in your path.

For example: sleep shutdown -r -f /m \\yourmachine
although shutdown now has -t option built in

陪你到最终 2024-08-16 17:33:59

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 5 was included in some of the Windows Resource Kits.

TIMEOUT 5 was included in some of the Windows Resource Kits, but is now a standard command in Windows 7 and 8 (not sure about Vista).

PING 1.1.1.1 -n 1 -w 5000 >NUL For any MS-DOS or Windows version with a TCP/IP client, PING can be used to delay execution for a number of seconds.

NETSH badcommand (Windows XP/Server 2003 only) or CHOICE

this link will help you more.

季末如歌 2024-08-16 17:33:59

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,但它不能。
所以我建议超时。
祝你好运!

Timeout /t 1 >nul

Is like pause in 1 secound, you can take the limed to almost 100.000 (99.999) secounds.
If you are connected to the internet the best solution would be:

ping 1.1.1.1. -n 1 -w 1000 >nul

When you ping you count in milliseconds, so one second would be 1000 milliseconds. But the ping command is a little iffy, it does not work the same way on offline machines.
The problem is that the machine gets confused because it is offline, and it would like to ping a website/server/host/ip, but it can't.
So i would recommend timeout.
Good luck!

嘴硬脾气大 2024-08-16 17:33:59

有!

SLEEP <seconds>

如果您希望以毫秒模式进行:

SLEEP -m <time in milliseconds>

这比 TIMEOUT 更有帮助,因为可以通过单击某个键或 CTRL + C 来中止 TIMEOUT (对于TIMEOUT /t/nobreak)。 SLEEP 不能被任何东西中止(关闭按钮除外:p)

另一个是 PING。但是 PING 需要互联网连接,因为您将记录站点的连接。

There is!

SLEEP <seconds>

If you want it in millisecond mode:

SLEEP -m <time in milliseconds>

This is more helpful than TIMEOUT because TIMEOUT can be aborted with a click of a key or CTRL + C (for TIMEOUT /t <secs> /nobreak). SLEEP cannot be aborted by anything (except for close button :p)

Other one is PING. But PING needs internet connection because you'll be recording the connection of a site.

野却迷人 2024-08-16 17:33:59

两个答案:

首先,要延迟批处理文件,根本不需要人们提出的所有迟钝方法:

timeout /t <TimeoutInSeconds> [/nobreak] 

http ://technet.microsoft.com/en-us/library/cc754891.aspx

其次,值得一提的是,虽然它可能无法完全满足您的要求,但使用内置的 Windows 截图工具,您可以触发截图无需使用鼠标即可完成。运行截图工具,退出当前截图但保持工具运行,然后在您希望进行截图时按 Control + Print Screen。这不应该干扰您想要剪切的任何内容。

Two answers:

Firstly, to delay in a batch file, simply without all the obtuse methods people have been proposing:

timeout /t <TimeoutInSeconds> [/nobreak] 

http://technet.microsoft.com/en-us/library/cc754891.aspx

Secondly, worth mentioning that while it may not do exactly what you want, using the inbuilt Windows snipping tool, you can trigger a snip on it without using the mouse. Run the snipping tool, escape out of the current snip but leave the tool running, and hit Control + Print Screen when you want the snip to occur. This shouldn't interfere with whatever it is you're trying to snip.

原谅我要高飞 2024-08-16 17:33:59

我试图在 msbuild 任务中执行此操作,但由于 I/O 重定向,选择和超时都不起作用。

我最终使用了 http://sourceforge.net/projects/unxutils 中的 sleep.exe,这很好因为它不需要任何安装而且很小。


尝试使用选择:

<Target Name="TestCmd">
  <Exec Command="choice /C YN /D Y /t 5 " />
</Target>

结果:

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.

尝试使用超时:

<Target Name="TestCmd">
  <Exec Command="timeout /t 5 " />
</Target>

结果:

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.

旁白:

我实际上正在使用 因为我并行执行 dbghost.exe 多次,并且它根据当前纪元时间(以秒为单位)创建临时文件/数据库 - 这当然意味着如果您启动多个实例,每个实例都使用相同的临时文件姓名。我最初尝试使用 MSBuild 扩展包 Thread.Sleep 命令,但似乎(通常)它运行睡眠任务正常,但随后启动 同时在所有线程中执行任务,当然 dbghost.exe 会因冲突而失败。到目前为止,使用 sleep.exe 似乎更可靠。

I was trying to do this from within an msbuild task, and choice and timeout both did not work due to I/O redirection.

I ended up using sleep.exe from http://sourceforge.net/projects/unxutils, which is nice because it doesn't require any install and it's tiny.


Trying with choice:

<Target Name="TestCmd">
  <Exec Command="choice /C YN /D Y /t 5 " />
</Target>

Results in:

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.

Trying with timeout:

<Target Name="TestCmd">
  <Exec Command="timeout /t 5 " />
</Target>

Results in:

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.

Aside:

I am actually using <Exec Command="sleep 2 & dbghost.exe" /> because I am executing dbghost.exe multiple times in parallel and it creates temp files/databases based on the current epoch time in seconds - which of course means if you start multiple instances, each uses the same temp name. I was originally trying to use MSBuild Extension Pack Thread.Sleep command, but it seems that (usually) it was running the sleep task fine, but then starting the <exec> task in all threads at the same time, and of course dbghost.exe would fail with conflicts. So far, using sleep.exe seems to be more reliable.

情场扛把子 2024-08-16 17:33:59

还有两种方法适用于 XP 及以上版本的所有内容:

使用 w32tm

w32tm /stripchart /computer:localhost /period:5 /dataonly /samples:2  1>nul 

使用 typeperf

typeperf "\System\Processor Queue Length" -si 5 -sc 1 >nul

使用 mshta(不需要设置网络) :

start "" /w /b /min mshta "javascript:setTimeout(function(){close();},5000);"

Two more ways that should work on everything from XP and above:

with w32tm:

w32tm /stripchart /computer:localhost /period:5 /dataonly /samples:2  1>nul 

with typeperf:

typeperf "\System\Processor Queue Length" -si 5 -sc 1 >nul

with mshta (does not require set up network):

start "" /w /b /min mshta "javascript:setTimeout(function(){close();},5000);"
如此安好 2024-08-16 17:33:59

这是我在实践中使用的最新版本,在脚本完成时暂停十秒以查看输出。

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

echo 完成让我可以看到脚本何时完成并且 ping 提供延迟。额外的 @ 符号意味着我看到“完成”文本,并且等待发生,而我不会被他们的命令分散注意力。

我已经在 XP 计算机上尝试了这里给出的各种解决方案,因为我们的想法是拥有一个可以在各种计算机上运行的批处理文件,因此我选择 XP 计算机作为可能功能最差的环境。

GOOD> ping 192.0.2.0 -n 1 -w 3000 > nul

这似乎如预期的那样延迟了三秒。一次 ping 尝试持续指定的 3 秒。

BAD> ping -n 5 192.0.2.0 > nul

这花费了大约 10 秒(而不是 5 秒)。我的解释是,有 5 次 ping 尝试,每次间隔大约一秒,总共 4 秒。每次 ping 尝试可能持续约一秒,总共估计为 9 秒。

BAD> timeout 5
BAD> sleep /w2000
BAD> waitfor /T 180
BAD> choice

命令不可用。

BAD> ping 192.0.2.0 -n 1 -w 10000 > nul :: wait 10000 milliseconds, ie 10 secs

我也尝试了上面的方法,在阅读了可以使用两个连续的冒号将注释添加到 BAT 文件中之后。然而,软件几乎立即返回。在 ping 之前将注释放在自己的行中效果很好。

GOOD> :: wait 10000 milliseconds, ie 10 secs
GOOD> ping 192.0.2.0 -n 1 -w 10000 > nul

为了更好地理解 ping 在实践中的作用,我运行

ping 192.0.2.0 -n 5 -w 5000

了大约 30 秒,尽管 5*5=25。我的解释是,有 5 次 ping 尝试,每次持续 5 秒,但 ping 尝试之间大约有 1 秒的时间延迟:毕竟,如果您立即再次 ping 的话,没有什么理由期待不同的结果,最好给出一个网络需要一点时间来从遇到的任何问题中恢复过来。

编辑:从另一篇文章窃取,..< a href="https://www.rfc-editor.org/rfc/rfc3330" rel="nofollow noreferrer">RFC 3330 规定 IP 地址 192.0.2.0 不应该出现在互联网上,因此 ping 这个地址可以防止这些测试向任何人发送垃圾邮件!
我对上面的文字进行了相应的修改!

This is the latest version of what I am using in practice for a ten second pause to see the output when a script finishes.

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

The echo done allows me to see when the script finished and the ping provides the delay. The extra @ signs mean that I see the "done" text and the waiting occurs without me being distracted by their commands.

I have tried the various solutions given here on an XP machine, since the idea was to have a batch file that would run on a variety of machines, and so I picked the XP machine as the environment likely to be the least capable.

GOOD> ping 192.0.2.0 -n 1 -w 3000 > nul

This seemed to give a three second delay as expected. One ping attempt lasting a specified 3 seconds.

BAD> ping -n 5 192.0.2.0 > nul

This took around 10 seconds (not 5). My explanation is that there are 5 ping attempts, each about a second apart, making 4 seconds. And each ping attempt probably lasted around a second making an estimated 9 seconds in total.

BAD> timeout 5
BAD> sleep /w2000
BAD> waitfor /T 180
BAD> choice

Commands not available.

BAD> ping 192.0.2.0 -n 1 -w 10000 > nul :: wait 10000 milliseconds, ie 10 secs

I tried the above too, after reading that comments could be added to BAT files by using two consecutive colons. However the software returned almost instantly. Putting the comment on its own line before the ping worked fine.

GOOD> :: wait 10000 milliseconds, ie 10 secs
GOOD> ping 192.0.2.0 -n 1 -w 10000 > nul

To understand better what ping does in practice, I ran

ping 192.0.2.0 -n 5 -w 5000

This took around 30 seconds, even though 5*5=25. My explanation is that there are 5 ping attempts each lasting 5 seconds, but there is about a 1 second time delay between ping attempts: there is after all little reason to expect a different result if you ping again immediately and it is better to give a network a little time to recover from whatever problem it has had.

Edit: stolen from another post, .. RFC 3330 says the IP address 192.0.2.0 should not appear on the internet, so pinging this address prevents these tests spamming anyone!
I have modified the text above accordingly!

北风几吹夏 2024-08-16 17:33:59

我做了这个。它正在工作并以秒为单位显示剩余时间。如果您想使用它,请添加到批处理文件中:

call wait 10

当我测试它时它正在工作。

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

I made this. It is working and show time left in seconds. If you want to use it, add to a batch file:

call wait 10

It was working when I tested it.

Listing of wait.bat (it must be in the working directory or 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
梦过后 2024-08-16 17:33:59

对用户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

An improvement of the code proposed by the user Aacini, It has resolution of hundredths of a second and does not fail when the time reaches 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
若相惜即相离 2024-08-16 17:33:59

您可以使用 VBScript,例如文件 myscript.vbs

set wsobject = wscript.createobject("wscript.shell")

do while 1=1
    wsobject.run "SnippingTool.exe",0,TRUE
    wscript.sleep 3000
loop

批处理文件:

cscript myscript.vbs %1

You can use VBScript, for example, file myscript.vbs:

set wsobject = wscript.createobject("wscript.shell")

do while 1=1
    wsobject.run "SnippingTool.exe",0,TRUE
    wscript.sleep 3000
loop

Batch file:

cscript myscript.vbs %1
一萌ing 2024-08-16 17:33:59

可以通过批处理文件中的两行简单的行来完成:在 %temp% 文件夹中编写一个临时 .vbs 文件并调用它:

echo WScript.Sleep(5000) >"%temp%\sleep.vbs"
cscript "%temp%\sleep.vbs"

It can be done with two simple lines in a batch file: write a temporary .vbs file in the %temp% folder and call it:

echo WScript.Sleep(5000) >"%temp%\sleep.vbs"
cscript "%temp%\sleep.vbs"
几味少女 2024-08-16 17:33:59

我使用以下完全基于 Windows XP 功能的方法在批处理文件中进行延迟:

文件 DELAY.BAT

@ECHO OFF
REM DELAY seconds

REM GET ENDING SECOND
FOR /F "TOKENS=1-3 DELIMS=:." %%A IN ("%TIME%") DO SET /A H=%%A, M=1%%B%%100,     S=1%%C%%100, ENDING=(H*60+M)*60+S+%1

REM WAIT FOR SUCH A SECOND
:WAIT
FOR /F "TOKENS=1-3 DELIMS=:." %%A IN ("%TIME%") DO SET /A H=%%A, M=1%%B%%100, S=1%%C%%100, CURRENT=(H*60+M)*60+S
IF %CURRENT% LSS %ENDING% GOTO WAIT

您还可以在计算中插入日期,这样当延迟间隔超过午夜时,该方法也适用。

I use the following method entirely based on Windows XP capabilities to do a delay in a batch file:

File DELAY.BAT

@ECHO OFF
REM DELAY seconds

REM GET ENDING SECOND
FOR /F "TOKENS=1-3 DELIMS=:." %%A IN ("%TIME%") DO SET /A H=%%A, M=1%%B%%100,     S=1%%C%%100, ENDING=(H*60+M)*60+S+%1

REM WAIT FOR SUCH A SECOND
:WAIT
FOR /F "TOKENS=1-3 DELIMS=:." %%A IN ("%TIME%") DO SET /A H=%%A, M=1%%B%%100, S=1%%C%%100, CURRENT=(H*60+M)*60+S
IF %CURRENT% LSS %ENDING% GOTO WAIT

You may also insert the day in the calculation so the method also works when the delay interval pass over midnight.

纸伞微斜 2024-08-16 17:33:59

我这样做的最简单的方法是:

http://www.sleepcmd.com/。 .exe 文件应该与您编写的程序位于同一文件夹中!

The easiest way I did it was this:

Download the Sleep.exe at http://www.sleepcmd.com/. The .exe file should be in the same folder as the program you wrote!

燃情 2024-08-16 17:33:59

如果您有适当版本的 Windows 以及 Windows Server 2003资源工具包工具,它包括批处理程序的睡眠命令。更多信息请访问:http://malektips.com/xp_dos_0002.html

If you have an appropriate version of Windows and the Windows Server 2003 Resource Kit Tools, it includes a sleep command for batch programs. More at: http://malektips.com/xp_dos_0002.html

梦初启 2024-08-16 17:33:59
PING -n 60 127.0.0.1>nul

如果您的 LAN 适配器不可用。

PING -n 60 127.0.0.1>nul

in case your LAN adapter is not available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文