How do I add a timer to a batch file?

2019-08-12 04:22发布

I want to make a batch file that waits for a few minutes, then executes a command. How would I do this? Specifically, I want it to end another program in 3 minutes after opening the file.

4条回答
成全新的幸福
2楼-- · 2019-08-12 04:28

Use timeout. That will let you wait for a number of seconds given in it's /t parameter. timeout /t 180 will sleep for 3 minutes (180 seconds).

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
查看更多
可以哭但决不认输i
3楼-- · 2019-08-12 04:30

Another method is to ping an invalid IP address for a certain amount of time:

PING 1.1.1.1 -n 1 -w 60000 >NUL

60000 = milliseconds

查看更多
贪生不怕死
4楼-- · 2019-08-12 04:38

hi i love stockoverflow but sometimes the answers are too concise... so heres more than you asked for... the timer and many other snipits

@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

hope you really enjoy this answer

查看更多
闹够了就滚
5楼-- · 2019-08-12 04:54

Simple, try this

@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
查看更多
登录 后发表回答