Windows 批处理文件中的计时器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4313897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:48:20  来源:igfitidea点击:

Timer in Windows batch file

windowsbatch-filetimetimercmd

提问by JVIDIN

Can someone point me to a way of adding a timer to a Windows batch file? I need to track the time my batch runs from start.

有人可以指出一种将计时器添加到 Windows 批处理文件的方法吗?我需要跟踪我的批处理从开始运行的时间。

回答by mousio

Refactored the code to calculate the elapsed time:

重构代码以计算经过的时间:

  • less code in calculation,
  • provided padding for elapsed time less than .1 seconds,
  • using labels to allow calling it multiple times, and to group the timer code.
  • 计算中的代码更少,
  • 为小于 0.1 秒的经过时间提供填充,
  • 使用标签允许多次调用它,并对定时器代码进行分组。

Reusable code:

可重用代码:

:StartTimer
:: Store start time
set StartTIME=%TIME%
for /f "usebackq tokens=1-4 delims=:., " %%f in (`echo %StartTIME: =0%`) do set /a Start100S=1%%f*360000+1%%g*6000+1%%h*100+1%%i-36610100
goto :EOF

:StopTimer
:: Get the end time
set StopTIME=%TIME%
for /f "usebackq tokens=1-4 delims=:., " %%f in (`echo %StopTIME: =0%`) do set /a Stop100S=1%%f*360000+1%%g*6000+1%%h*100+1%%i-36610100
:: Test midnight rollover. If so, add 1 day=8640000 1/100ths secs
if %Stop100S% LSS %Start100S% set /a Stop100S+=8640000
set /a TookTime=%Stop100S%-%Start100S%
set TookTimePadded=0%TookTime%
goto :EOF

:DisplayTimerResult
:: Show timer start/stop/delta
echo Started: %StartTime%
echo Stopped: %StopTime%
echo Elapsed: %TookTime:~0,-2%.%TookTimePadded:~-2% seconds
goto :EOF

Calling code:

调用代码:

call :StartTimer
:: 
:: Add your script functionality here
::
call :StopTimer
call :DisplayTimerResult
pause

call :StartTimer
:: 
:: Add more script functionality here
::
call :StopTimer
call :DisplayTimerResult
goto :EOF

Note the "1"-prefix to avoid interpretation errors of zero-padded values as octal values (08 and 09), and the correction with the appropriate constant. As this might be confusing to understand the core calculation, an alternative to the one-line forstatement would be something like the following:

请注意“1”前缀,以避免将填充零的值解释为八进制值(08 和 09),并使用适当的常量进行校正。由于这可能会使理解核心计算感到困惑,因此单行for语句的替代方法如下所示:

for /f "usebackq tokens=1-4 delims=:., " %%f in (`echo.%StartTIME%`) do set TempTIME=%%f %%g %%h %%i
for /f "usebackq tokens=1-4" %%f in (`echo %TempTIME: 0= %`) do set /a Start100S=%%f*360000+%%g*6000+%%h*100+%%i

回答by migonzalvar

A minimalistic version with elapsed time in seconds.

以秒为单位的简约版本。

@set /A _tic=%time:~0,2%*3600^
            +%time:~3,1%*10*60^
            +%time:~4,1%*60^
            +%time:~6,1%*10^
            +%time:~7,1% >nul

:: actual script

@set /A _toc=%time:~0,2%*3600^
            +%time:~3,1%*10*60^
            +%time:~4,1%*60^
            +%time:~6,1%*10^
            +%time:~7,1% >nul

@set /A _elapsed=%_toc%-%_tic
@echo %_elapsed% seconds.

回答by MrEyes

As far as I know there is no explicit 'timer' command you can use in batch files - however there is a fairly simple solution. At the top of your batch file record the current time (start time) at the end of the batch file record the current time (end time) and then compare the two. Something like this should do it (it does seem overly complicated but it handles most quirks of time):

据我所知,没有可以在批处理文件中使用的显式“计时器”命令 - 但是有一个相当简单的解决方案。在批处理文件的顶部记录当前时间(开始时间),在批处理文件末尾记录当前时间(结束时间),然后比较两者。这样的事情应该可以做到(它看起来确实过于复杂,但它可以处理大多数时间的怪癖):

:: Store start time
set StartTIME=%TIME%
set H=%StartTIME:~0,2%
if "%H:~0,1%"==" " set H=%H:~1,1%
if "%H:~0,1%"=="0" set H=%H:~1,1%
set M=%StartTIME:~3,2%
if "%M:~0,1%"=="0" set M=%M:~1,1%
set S=%StartTIME:~6,2%
if "%S:~0,1%"=="0" set S=%S:~1,1%
set U=%StartTIME:~9,2%
if "%U:~0,1%"=="0" set U=%U:~1,1%
)
set /a Start100S=%H%*360000+%M%*6000+%S%*100+%U%

:: 
:: Add your script functionality here
::

:: Get the end time
set StopTIME=%TIME%
set H=%StopTIME:~0,2%
if "%H:~0,1%"==" " set H=%H:~1,1%
if "%H:~0,1%"=="0" set H=%H:~1,1%
set M=%StopTIME:~3,2%
if "%M:~0,1%"=="0" set M=%M:~1,1%
set S=%StopTIME:~6,2%
if "%S:~0,1%"=="0" set S=%S:~1,1%
set U=%StopTIME:~9,2%
if "%U:~0,1%"=="0" set U=%U:~1,1%
)

set /a Stop100S=%H%*360000+%M%*6000+%S%*100+%U%

:: Test midnight rollover. If so, add 1 day=8640000 1/100ths secs
if %Stop100S% LSS %Start100S% set /a Stop100S+=8640000
set /a TookTime=%Stop100S%-%Start100S%

echo Started: %StartTime%
echo Stopped: %StopTime%
echo Elapsed: %TookTime:~0,-2%.%TookTime:~-2% seconds

Alternatively #1 : The Windows Server 2k3 resource kit includes timeit.exe. You can use this to display various performance stats for your script.

或者 #1:Windows Server 2k3 资源工具包包括 timeit.exe。您可以使用它来显示脚本的各种性能统计信息。

Alternatively #2 : You could go much simpler than the script posted above and do this:

或者#2:您可以比上面发布的脚本简单得多,然后执行以下操作:

echo %time% 
::
:: Your script functionality
::
echo %time%

However, this won't give you execution time in seconds.

但是,这不会以秒为单位为您提供执行时间。

回答by I_boom5245

This timer is pretty accurate, and counts down from 5. I'm not sure how you could add this into your code though. Note:This is a verybasic batch timer, and the other answers are probably better, but this is just a quick way to design your own batch timer with simple code.

这个计时器非常准确,从 5 开始倒计时。不过,我不确定如何将其添加到代码中。注意:这是一个非常基本的批处理计时器,其他答案可能更好,但这只是使用简单代码设计自己的批处理计时器的快速方法。

@echo off
cls
set /a time=5
:hi
cls
echo Time:%time%
ping localhost -n 2 >nul
set /a time=%time%-1
if %time%==0 (
goto timeup
)else goto :hi
:timeup
cls
echo Time is up!
pause

Also to change the time this timer starts at, just change set /a time=5to set /a time=(whatever time you want to start at)

还要更改此计时器开始的时间,只需更改set /a time=5set /a time=(whatever time you want to start at)

回答by Paul Tomasi

Structure your program like this:

像这样构建你的程序:

@echo off
setlocal enabledelyedexpansion

set T[1]=%time%

 ::
 :: Your program goes here...
 :: 

set T[2]=%time%
call TIMER

Here is the code for the TIMER.BAT program. Save it as a seperate file somewhere in your path.

下面是 TIMER.BAT 程序的代码。将其另存为路径中某处的单独文件。

for /l %%t in (1,1,2) do for /f "tokens=1-4 delims=:." %%a in ("!T[%%t]!") do (
 set "T[%%t]=!T[%%t]: =0!"
 set "h[%%t]=%%a" & if "!h[%%t]:~0,1!"=="0" set "h[%%t]=!h[%%t]:~1!"
 set "m[%%t]=%%b" & if "!m[%%t]:~0,1!"=="0" set "m[%%t]=!m[%%t]:~1!"
 set "s[%%t]=%%c" & if "!s[%%t]:~0,1!"=="0" set "s[%%t]=!s[%%t]:~1!"
 set "c[%%t]=%%d" & if "!c[%%t]:~0,1!"=="0" set "c[%%t]=!c[%%t]:~1!"
 set /a T[%%t]=!h[%%t]!*360000+!m[%%t]!*6000+!s[%%t]!*100+!c[%%t]!
)

set /a c=!T[2]!-!T[1]!
set /a h=!c!/360000 & set /a c%%=360000 & if !h! lss 10 set "h= !h!"
set /a m=!c!/6000   & set /a c%%=6000   & if !m! lss 10 set "m=0!m!"
set /a s=!c!/100    & set /a c%%=100    & if !s! lss 10 set "s=0!s!"
echo !h!:!m!:!s!.!c!

回答by GaryNg

You can try this one(Shorter..)

你可以试试这个(短..)

@echo off
:loop
set TA=%time% & echo %time%--A

echo Put Your Code Here...

set TB=%time% & echo %time%--B

call:Timediff %TA% %TB% Tab
for /f "tokens=1-3 delims=:" %%a in ("%TAB%")do echo Time difference: %%a hours %%b minutes and %%c seconds
pause
goto:loop

:: /* ---------- Timediff ---------------
:Timediff [%t1%] [%t2%] [par|0]
for %%a in (+%1 +%2 +%3)do if "%%a"=="+" echo No Parameters!!&exit/b
setlocal enabledelayedexpansion
:timediff_1
set P2=%~1&set "P2=!P2::=!"
set/a P2=%P2:.=%-4000*(%P2:~,4%+60*%P2:~,2%)
if not "%3"=="" set P1=!P2!&shift&goto:timediff_1
if !P2! geq !P1! (set/a df=!P2!-!P1!) else set/a df=!P2!-!P1!+8640000
set/a h=df/360000,m=df%%360000/6000,s=df%%6000/100,pt=df%%100
if %pt% leq 9 set pt=0%pt%
endlocal&if %2.==0. (echo\%h%:%m%:%s%.%pt%) else set %2=%h%:%m%:%s%.%pt%&goto:eof
:: ------------- Timediff ------------- */