Windows 批处理文件中的计时器

发布于 2024-10-05 04:06:11 字数 62 浏览 4 评论 0原文

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

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.

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

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

发布评论

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

评论(7

鹿港巷口少年归 2024-10-12 04:06:12

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

  • 计算中的代码更少,
  • 为小于0.1秒的经过时间提供填充,
  • 使用标签允许多次调用,并对计时器代码进行分组。

可重用代码:

: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

调用代码:

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

请注意“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

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.

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 for statement would be something like the following:

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
奈何桥上唱咆哮 2024-10-12 04:06:12

一个简约的版本,经过的时间以秒为单位。

@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.

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.
梦里°也失望 2024-10-12 04:06:12

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

:: 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

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

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

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

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

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.

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

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

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

痴意少年 2024-10-12 04:06:12

像这样构造您的程序:

@echo off
setlocal enabledelyedexpansion

set T[1]=%time%

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

set T[2]=%time%
call TIMER

这是 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!

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.

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!
音盲 2024-10-12 04:06:12

这个计时器非常准确,从 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

另外,要更改此计时器的开始时间,只需将 set /a time=5 更改为 set /a time=(无论您想开始的时间)

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 very basic 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.

@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=5 to set /a time=(whatever time you want to start at)

远昼 2024-10-12 04:06:12

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

@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 ------------- */

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 ------------- */
篱下浅笙歌 2024-10-12 04:06:12

我认为最好的选择是使用 powershell,因为

@echo off
for /f %%t in ('powershell "(get-date).tofiletime()"') do set mst=%%t

rem some commands

powershell ((get-date).tofiletime() - %mst%)

这会打印经过的时间(以毫秒为单位)

I think the best option will be to use powershell as

@echo off
for /f %%t in ('powershell "(get-date).tofiletime()"') do set mst=%%t

rem some commands

powershell ((get-date).tofiletime() - %mst%)

this will print the time passed in milliseconds

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