Thursday, July 8, 2010

Calculating batch file elapsed time

Recently I was tasked with writing a rather lengthy batch file and I thought I would output the total elapsed time at the end so the user could see how long it took. Batch files don't have date math so I wrote this batch file to calculate the time for me and display it in minutes, or seconds if less than 60 seconds.

@echo off

REM --------------------------------------------------------------
REM  Elapsed Time Calculator
REM --------------------------------------------------------------
REM  Elapsed time calculator only good for 
REM  time spans that are less than 24 hrs.
REM --------------------------------------------------------------
REM  Inputs:
REM    starttime
REM    endtime
REM --------------------------------------------------------------
REM  Outputs:
REM    Displays processing time in minutes.  
REM    Seconds shown if less than 60 seconds.
REM --------------------------------------------------------------

set /a daysecs=86400

REM --------------------------------------------------------------
REM  Calculate starttime seconds.
REM --------------------------------------------------------------

if %starttime:~0,2% LSS 10 (set /a starthrs=%starttime:~1,1%) 
else (set /a starthrs=%starttime:~0,2%) 
set /a starthrs=%starthrs%*3600
 
if %starttime:~3,2% LSS 10 (set /a startmins=%starttime:~4,1%) 
else (set /a startmins=%starttime:~3,2%) 
set /a startmins=%startmins%*60
 
if %starttime:~6,2% LSS 10 (set /a startsecs=%starttime:~7,1%) 
else (set /a startsecs=%starttime:~6,2%)  
set /a startsecs=%startsecs%+%startmins%+%starthrs%

REM --------------------------------------------------------------
REM  Calculate endtime seconds.
REM --------------------------------------------------------------

set /a endhrs=%endtime:~0,2%
if %endtime:~0,2% LSS 10 (set /a endhrs=%endtime:~1,1%) 
else (set /a endhrs=%endtime:~0,2%)
set /a endhrs=%endhrs%*3600 
 
set /a endmins=%endtime:~3,2%
if %endtime:~3,2% LSS 10 (set /a endmins=%endtime:~4,1%) 
else (set /a endmins=%endtime:~3,2%)
set /a endmins=%endmins%*60
 
set /a endsecs=%endtime:~6,2% 
if %endtime:~6,2% LSS 10 (set /a endsecs=%endtime:~7,1%)
else (set /a endsecs=%endtime:~6,2%)
set /a endsecs=%endsecs%+%endmins%+%endhrs%

REM --------------------------------------------------------------
REM  Calculate total time difference.
REM --------------------------------------------------------------

if %endsecs% GTR %startsecs% GOTO SAMEDAY
if %endsecs% EQU %startsecs% GOTO SAMETIME
GOTO CROSSDAYS

:SAMETIME
set ElapsedTime=0 Seconds
GOTO OUTPUT

:CROSSDAYS
set /a totsec=%daysecs%-%startsecs%
set /a totsec=%totsec%+%endsecs%
set /a totmin=%totsec%/60
GOTO RESULT

:SAMEDAY
set /a totsec=%endsecs%-%startsecs%
set /a totmin=%totsec%/60

:RESULT
if %totsec% LSS 60 (set ElapsedTime=%totsec% Second^(s^)) 
else (set ElapsedTime=%totmin% Minute^(s^))

REM --------------------------------------------------------------
REM  Output total elapsed time.
REM --------------------------------------------------------------
:OUTPUT
echo Total Processing Time: %ElapsedTime%

1 comment:

  1. drew@drewdecker.com,August 18, 2011 at 4:20 PM

    Sorry for such a simpleton question - where does the command that you are looking to time go in your script?

    ReplyDelete