Calculate How Long It Takes To Run A Batch File

Sometimes you might want to calcaulte how many
hours/minutes/seconds a batch file takes to run. Use this code to do so. Put
your own code inside the ‘code to benachmark goes here’ section.

@echo off
REM TimeDiff – batch file for
calculating time to execute a command
REM Note: Currently will not work over
a 24 hr period To Be Completed
REM by Malcolm McCaffery

REM ***************************************
REM
Get the hours/min/secs/hun, etc from current date and time
REM
***************************************
set DT=%DATE% %TIME%
set
year=%DT:~10,4%
set day=%DT:~4,2%
set mth=%DT:~7,2%
set
hour=%DT:~15,2%
set min=%DT:~18,2%
set sec=%DT:~21,2%
set
hun=%DT:~24,2%

REM *************** CODE TO BENCHMARK GOES STARTS
HERE ****************

REM This line is an example – remove and replace
with your own code

REM *************** CODE TO BENCHMARK ENDS HERE
***********************

REM
***************************************
REM Get the hours/min/secs/hun, etc
from current date and time
REM ***************************************
set
EndTime=%TIME%
set EndHour=%EndTime:~0,2%
set EndMin=%EndTime:~3,2%
set
EndSec=%EndTime:~6,2%
set EndHun=%EndTime:~9,2%

REM *******************************
REM Calculate
the difference
REM Hours, Minutes, seconds, hundredths calculated
separately
REM *******************************
set /a
Hour_Diff=EndHour-hour
set /a Min_Diff=EndMin-min
set /a
Sec_Diff=EndSec-sec
set /a Hun_Diff=EndHun-hun

REM *******************************
REM Carry any
differences
REM *******************************

IF %Hun_Diff% LSS 0 (
set /a
Hun_Diff=Hun_Diff+100
set /a Sec_Diff=Sec_Diff+1
)

IF %Sec_Diff% LSS 0 (
set /a
Sec_Diff=Sec_Diff+60
set /a Min_Diff=Min_Diff+1
)

If %Min_Diff% LSS 0 (
set /a
Min_Diff=Min_Diff+60
set /a Hour_Diff=Hour_Diff+1
)

echo Start Time = %hour%:%min%:%sec%:%hun%
echo
End Time = %EndHour%:%EndMin%:%EndSec%:%EndHun%
echo.
echo %Hour_Diff% hrs
%Min_Diff% mins %Sec_Diff%.%Hun_Diff% secs
REM The End

About chentiangemalc

specializes in end-user computing technologies. disclaimer 1) use at your own risk. test any solution in your environment. if you do not understand the impact/consequences of what you're doing please stop, and ask advice from somebody who does. 2) views are my own at the time of posting and do not necessarily represent my current view or the view of my employer and family members/relatives. 3) over the years Microsoft/Citrix/VMWare have given me a few free shirts, pens, paper notebooks/etc. despite these gifts i will try to remain unbiased.
This entry was posted in Batch Files, Command Line. Bookmark the permalink.

One Response to Calculate How Long It Takes To Run A Batch File

  1. John Pulwers says:

    Do not try to run this at the top of the hour. For example, if you run it at 8:59. and it takes 28 seconds to run, the report may show 2 hours, 2 minutes and 28 seconds run-time. But, if you run it at 9:02, it’ll show 0 hours, 0 minutes, and 28 seconds run-time. So, don’t count on accuracy if your execution time overlaps from one hour to the next, other than that, it works great!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s