计算批处理文件执行的开始和结束时间?

时间:2014-12-12 15:15:23

标签: batch-file time timer

如何使用批处理脚本计算开始和结束时间? 我希望我的最终输出只能在分钟。 例如,1小时30分钟应该是90分钟。

我确实发现了很多类似的问题,但不确定如何仅在几分钟内计算最终输出。

@echo off

rem Get start time:
for /F "tokens=1-4 delims=:.," %%a in ("%time%") do (
   set /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)

rem Any process here...
ECHO Start of Loop
for /L %%n in (1,1,5000) do echo %%n

rem Get end time:
for /F "tokens=1-4 delims=:.," %%a in ("%time%") do (
   set /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)

rem Get elapsed time:
REM set /A elapsed=end-start
echo %start%
echo %end%
echo %elapsed%

rem Show elapsed time:
set /A hh=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
set /A mm=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
if %mm% lss 10 set mm=0%mm%
if %ss% lss 10 set ss=0%ss%
if %cc% lss 10 set cc=0%cc%
echo %hh%:%mm% > time.csv

1 个答案:

答案 0 :(得分:1)

要将小时:分钟转换为几分钟,将小时数乘以60,然后再添加分钟数。

@echo off

rem Get start time:
for /F "tokens=1-4 delims=:.," %%a in ("%time%") do (
   set /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)

rem Any process here...
ECHO Start of Loop
for /L %%n in (1,1,5000) do echo %%n

rem Get end time:
for /F "tokens=1-4 delims=:.," %%a in ("%time%") do (
   set /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)

rem Get elapsed time:
REM set /A elapsed=end-start
echo %start%
echo %end%
echo %elapsed%

rem Show elapsed time:
set /A hh=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
set /A mm=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
set /a mm=%hh%*60+%mm%
if %mm% lss 10 set mm=0%mm%
echo.>time.csv
echo %mm%>>time.csv

我将转换步骤向上移动,以防经过的时间在8或9分钟结束,因为批处理认为以0开头的数字是八进制而不是十进制,而08和09不是有效的八进制数。 / p>