【问题标题】:How do I add a timer to a batch file?如何将计时器添加到批处理文件?
【发布时间】:2012-05-11 22:27:17
【问题描述】:

我想制作一个等待几分钟,然后执行命令的批处理文件。我该怎么做?具体来说,我希望它在打开文件后 3 分钟内结束另一个程序。

【问题讨论】:

    标签: file batch-file timer


    【解决方案1】:

    使用timeout。这将让您等待/t 参数中给出的几秒钟。 timeout /t 180 将休眠 3 分钟(180 秒)。

    TIMEOUT [/T] timeout [/NOBREAK]
    
    Description:
        This utility accepts a timeout parameter to wait for the specified
        time period (in seconds) or until any key is pressed. It also
        accepts a parameter to ignore the key press.
    
    Parameter List:
        /T        timeout       Specifies the number of seconds to wait.
                                Valid range is -1 to 99999 seconds.
    
        /NOBREAK                Ignore key presses and wait specified time.
    
        /?                      Displays this help message.
    
    NOTE: A timeout value of -1 means to wait indefinitely for a key press.
    
    Examples:
        TIMEOUT /?
        TIMEOUT /T 10
        TIMEOUT /T 300 /NOBREAK
        TIMEOUT /T -1
    

    【讨论】:

      【解决方案2】:

      另一种方法是在一定时间内ping一个无效的IP地址:

      PING 1.1.1.1 -n 1 -w 60000 >NUL
      

      60000 = 毫秒

      【讨论】:

        【解决方案3】:

        嗨,我喜欢 stockoverflow,但有时答案过于简洁......所以这里比你要求的要多......计时器和许多其他片段

        @ECHO off
        MODE CON:COLS=25 LINES=11
        cls
        color 0B
        :taskkill
        echo.
        echo    Program To Shutdown
        echo.
        set /p taskkill=                               
        if "%taskkill%" == "%taskkill%" goto taskkillconfirm
        exit
        :taskkillconfirm
        color 0B
        :image
        set image=/im
        if "%image%" == "%image%" goto imageconfirm
        exit
        :imageconfirm
        color 0B
        :forced
        set forced=/f
        if "%forced%" == "%forced%" goto forcedconfirm
        exit
        :forcedconfirm
        cls
        :hour
        color 0B
        echo.
        echo.                                  Hours?
        echo.
        set /p hour=                                     
        :min
        color 0B
        cls
        echo.
        echo                                   Minutes?
        echo.
        set /p min=                                     
        :sec
        color 0B
        cls
        echo.
        echo                                   Seconds?
        echo.
        set /p sec=                                     
        :continue
        color 0B
        cls                                                             
        echo                             %taskkill%  
        echo                             Program Shutdown in                                                             
        echo   %hour%   Hours
        echo   %min%  Minutes  
        echo   %sec%  Seconds
        set /a sec="%sec%-1"
        if %sec%==-1 set /a min="%min%-1"
        if %sec%==-1 set /a sec="59"
        if %min%==-1 set /a hour="%hour%-1"
        if %min%==-1 set /a min="59"
        if %hour%==-1 goto done
        ping -n 2 127.0.0.1 >NUL
        goto continue
        :done
        color 0B
        cls
        taskkill %image% %taskkill% %forced%
        exit
        

        希望你真的喜欢这个答案

        【讨论】:

        • 确实很有用。
        【解决方案4】:

        简单,试试这个

        @echo off
        echo Do you want to read word file or pdf file? Hit any key for options...
        pause >nul
        echo w for word
        echo p for pdf
        ::set input =
        set /p input = Choose your option
        if %input% == w goto w
        if %input% == p goto p
        :w
        echo Reading Word file...
        ::start /your/path/to/your/Office/winword.exe          
        /path/to/your/doc/file/filename.doc
        echo Timer starts
        pause >nul
        echo 10
        ping localhost -n 2 >nul
        cls
        echo 9
        ping localhost -n 2 >nul
        cls
        echo 8
        ping localhost -n 2 >nul
        cls
        echo 7
        ping localhost -n 2 >nul
        cls
        echo 6
        ping localhost -n 2 >nul
        cls
        echo 5
        ping localhost -n 2 >nul
        cls
        echo 4
        ping localhost -n 2 >nul
        cls
        echo 3
        ping localhost -n 2 >nul
        cls
        echo 2
        ping localhost -n 2 >nul
        cls
        echo 1
        ping localhost -n 2 >nul
        cls
        echo Time's up. Starting the next document...
        ::you can copy/paste the above commands to start another word file
        :p
        echo Reading Pdf file...
        echo Timer starts
        ::start /your/path/to/your/Acrobat/acrord32.exe    
        /path/to/your/pdf/file/filename.pdf
        pause >nul
        echo 10
        ping localhost -n 2 >nul
        cls
        echo 9
        ping localhost -n 2 >nul
        cls
        echo 8
        ping localhost -n 2 >nul
        cls
        echo 7
        ping localhost -n 2 >nul
        cls
        echo 6
        ping localhost -n 2 >nul
        cls
        echo 5
        ping localhost -n 2 >nul
        cls
        echo 4
        ping localhost -n 2 >nul
        cls
        echo 3
        ping localhost -n 2 >nul
        cls
        echo 2
        ping localhost -n 2 >nul
        cls
        echo 1
        ping localhost -n 2 >nul
        cls
        echo Times up. Starting the next document...
        ::you can copy/paste the above commands to start another Pdf file
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-05-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-17
          • 2020-04-27
          • 2016-03-05
          • 1970-01-01
          相关资源
          最近更新 更多