Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 如何在批处理文件/cmd中睡眠五秒钟_Batch File_Timeout - Fatal编程技术网

Batch file 如何在批处理文件/cmd中睡眠五秒钟

Batch file 如何在批处理文件/cmd中睡眠五秒钟,batch-file,timeout,Batch File,Timeout,Windows的剪贴工具可以捕捉屏幕,但有时我想在五秒钟后捕捉屏幕,比如拍摄网络摄像头显示的图像。(例如,运行脚本并对着相机微笑。) 如何在批处理文件中睡眠5秒钟?例如,您可以使用filemyscript.vbs: set wsobject = wscript.createobject("wscript.shell") do while 1=1 wsobject.run "SnippingTool.exe",0,TRUE wscript.sleep 3000 loop 批处理

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

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

例如,您可以使用file
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
一种黑客行为是(错误地)使用ping命令:

ping 127.0.0.1 -n 6 > nul
说明:

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

    • 下面的黑客让你睡5秒钟

      ping -n 6 127.0.0.1 > nul
      

      由于ping在ping之间等待一秒钟,因此您必须指定一个超出需要的值。

      我很惊讶没有人提到:

      C:\> timeout 5
      

      N.B.但是请注意(谢谢Dan!)
      timeout 5
      表示:

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

      ping -n 6 127.0.0.1 > nul
      
      这可以通过将以下内容放入批处理文件中,反复运行并计算第一个和第二个
      echo
      s之间的时间差来进行经验验证:

      @echo off
      echo %time%
      timeout 5 > NUL
      echo %time%
      
      试试这个命令。它从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."
      

      如果您有适当版本的Windows和,它将包括用于批处理程序的sleep命令。更多信息请访问:

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

      call wait 10
      
      文件延迟.BAT
      您还可以在计算中插入日期,这样当延迟时间间隔超过午夜时,该方法也可以工作。

      如果您有选择或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
      
      @echo关闭
      回声。
      如果“%1”==“转到askq
      如果“%1”=“/?”转到帮助
      如果/i“%1”=“/h”转到帮助
      如果%1全球技术法规0如果%1 LEQ 9999如果/i“%2”==“/q”设置ans1=%1并转到安静状态
      如果%1全球技术法规0如果%1 LEQ 9999设置ans1=%1和转到断开
      如果%1 LEQ 0回音%1不是有效的数字(&G)转到帮助
      如果不是“%1”==“echo”。echo“%1”是一个错误的参数,转到“帮助”
      转到终点
      :救命
      echo SLEEP以交互方式(自身)运行或使用参数(SLEEP#/q)运行
      #所在位置的回声(以秒为单位),范围为1-9999
      echo使用可选参数/q抑制标准输出
      echo或type/h或/?对于此帮助文件
      回声。
      转到终点
      :askq
      set/p ans1=睡眠时间为多少秒?^
      回声。
      如果“%ans1%”==“转到askq
      如果%ans1%GTR 0如果%ans1%LEQ 9999转到断开
      转到askq
      :安静
      选项/n/t%ans1%/d n>nul
      如果错误级别1 ping 1.1.1.1-n 1-w%ans1%000>nul
      转到终点
      :突破
      选项/n/t%ans1%/d n>nul
      如果错误级别1 ping 1.1.1.1-n 1-w%ans1%000>nul
      回显睡眠%ans1%秒^
      回声。
      :结束
      

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

      我们能不能
      等待/t180

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

      waitfor/t180 pause>nul
      将清除地毯下的“错误”

      Win95之后,Windows操作系统中应该有
      waitfor
      命令

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

      例如:
      sleep shutdown-r-f/m\\yourmachine

      虽然shutdown现在内置了-t选项,这是对用户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
      

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

      我最终使用了来自的sleep.exe,这很好,因为它不需要任何安装,而且很小


      尝试使用
      选项

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

      尝试使用
      超时

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

      旁白:


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

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

      call wait 10
      
      当我测试它时,它正在工作

      wait.bat
      (它必须位于工作目录或
      windir/system32/
      )的列表:


      我做这件事最简单的方法是:


      在下载Sleep.exe。.exe文件应与您编写的程序位于同一文件夹中

      我认为下面的命令会有所帮助:

      pause 5
      
      pause命令的语法为: 暂停d\\d表示持续时间(秒)

      我正在使用Windows7(32位),但我不知道
      @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
      
      pause 5
      
      sleep /w2000
      
      powershell -command "Start-Sleep -s 5"
      
      powershell -command "$sleepUntil = [DateTime]::Parse('%date% %time%').AddSeconds(5); $sleepDuration = $sleepUntil.Subtract((get-date)).TotalMilliseconds; start-sleep -m $sleepDuration"
      
      echo WScript.Sleep(5000) >"%temp%\sleep.vbs"
      cscript "%temp%\sleep.vbs"
      
      PING -n 60 127.0.0.1>nul
      
      timeout /t <TimeoutInSeconds> [/nobreak] 
      
      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
      
      GOOD> ping 192.0.2.0 -n 1 -w 3000 > nul
      
      BAD> ping -n 5 192.0.2.0 > nul
      
      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
      
      GOOD> :: wait 10000 milliseconds, ie 10 secs
      GOOD> ping 192.0.2.0 -n 1 -w 10000 > nul
      
      ping 192.0.2.0 -n 5 -w 5000
      
      w32tm /stripchart /computer:localhost /period:5 /dataonly /samples:2  1>nul 
      
      typeperf "\System\Processor Queue Length" -si 5 -sc 1 >nul
      
      start "" /w /b /min mshta "javascript:setTimeout(function(){close();},5000);"
      
      REM Usage: SLEEP Time_in_MiliSECONDS
      @ECHO off
      ping 1.0.0.0 -n 1 -w %1 > nul
      
      sleep 500
      
      wait 3000
      system('c:/windows/system32/SnippingTool.exe')
      
      FUNCTION PBMAIN()
        c$ = Command$
        s! = Val(c$)*1000
        Sleep s!         
      END FUNCTION
      
      SLEEP <seconds>
      
      SLEEP -m <time in milliseconds>