Batch Timer

Gigabyte / M52s-s3p
April 1, 2009 at 04:32:13
Specs: Microsoft Windows XP Professional, 2.31 GHz / 3582 MB
I am trying to write a script that calculates the
time that a batch file takes to run.
I have managed to make it print the start and
end lines but i want it to be able to calculate
the time difference between STT and ENT.

If someone could please give me an example of how to do this id me much appreciative.

here is what i have so far:


Start Of Batch

::Set Start Time
SET STT=%time%

::Place For Script.

::Set End Time
SET ENT=%time%


End Of Batch

Its obviously a simple script but i am going to
add it to another script to log the time it takes
to complete.


See More: Batch Timer

Report •

April 1, 2009 at 04:55:37
Holla already did the work.

@echo off & setLocal EnableDelayedExpansion

If %1a==a goto Usage
set starttime=%time%
call :ExtractHMSHn %starttime% StartTimeInNum
call %*
set endtime=%time%
call :ExtractHMSHn %endtime% EndTimeInNum
:find the Difference in time
set /a diff = %EndTimeInNum% - %StartTimeInNum%
Set /a Elapsedhrs= %diff% / (60 * 60 * 100)
Set /a Elapsedmin= (%diff% - (%Elapsedhrs% * 60*60*100)) / 6000
set /a elapsedsectmp= %diff% - (%Elapsedhrs% * 60*60*100 ) %% 6000
set /a elapsedsec= %elapsedsectmp% / 100
set /a elapsedhun= %elapsedsectmp% %% 100
Echo Elapsed time: %Elapsedhrs%:%Elapsedmin%:%elapsedsec%.%elapsedhun%
goto :EOF
Echo Usage: %0 YourCommand [Your command's parameters]
Echo Example: %0 ping -n 2
echo - will execute "ping -n 2" and tells how long it for execution.
goto :EOF

set t1=%1
set hun=%t1:~9,2%
set sec=%t1:~6,2%
set min=%t1:~3,2%
set hr=%t1:~0,2%
:Remove zero from the begining so that it does not crib about invalid octal
if %hun:~0,1%==0 set hun=%hun:~1,1%
if %sec:~0,1%==0 set sec=%sec:~1,1%
if %min:~0,1%==0 set min=%min:~1,1%
if %hr:~0,1%==0 set hr=%hr:~1,1%
set /a %2 = %hun% %% 100 + %sec% * 100 + %min% * 60*100 + %hr% * 60*60*100
exit /b

If at first you don't succeed, you're about average.


Report •

April 1, 2009 at 05:08:46
::NOTE: The answer wont be 100% accurate, but it is reasonably correct.
:: The answer is in seconds. (so if your script takes minutes, divide by 60)
:: Enjoy

@echo off & cls & type nul>timelog.log
echo %time%>timelog.log
::--------YOUR SCRIPT BEGINS HERE--------
::The ping below is just for testing purposes... you must remove this when you put
:: your script here, as it will mess your time around..
ping -n 3 -w 1000 >nul

::--------YOUR SCRIPT ENDS HERE--------
echo %time%>>timelog.log
::-------TIME MATHS-------
::set information into variables
setlocal enabledelayedexpansion
for /f %%j in (timelog.log) do (
set /a number +=1
set reaction_time=%%j
set reaction!number!=!reaction_time!
::sort variables into specific variables
set hh1=%reaction1:~0,2%
set mm1=%reaction1:~3,2%
set ss1=%reaction1:~6,2%
set hh2=%reaction2:~0,2%
set mm2=%reaction2:~3,2%
set ss2=%reaction2:~6,2%
::this is an important step. the reason you add 2 is to get
::away from the 08 and 09 octal which will crash your file.
set /a hh1 +=2
set /a hh2 +=2
set /a mm1 +=2
set /a mm2 +=2
set /a ss1 +=2
set /a ss2 +=2
::a bit of maths to get varibales correct for time
::sort out seconds
set /a answer_ss = %ss2%-%ss1%
if %answer_ss% == 0 goto :GET_MM
if %answer_ss% lss 0 (
) else (
goto :GET_MM
set answer_ss=
set /a ss2 +=60
set /a answer_ss = %ss2%-%ss1%
set /a mm2 -=1
::sort out minutes
set /a answer_mm=%mm2%-%mm1%
if %answer_mm% == 0 goto :GET_HH
if %answer_mm% lss 0 (
) else (
set /a answer_mm = 60*%answer_mm%
goto :GET_HH
set answer_mm=
set /a mm2 +=60
set /a answer_mm = %mm2%-%mm1%
set /a hh2 -=1
::sort out hours
set /a answer_hh = %hh2%-%hh1%
if %answer_hh% == 0 goto :FINAL_ANSWER
if %answer_hh% lss 0 (
) else (
set /a answer_hh = 60*60*%answer_hh%
set answer_hh=
set /a hh2 +=60
set /a answer_hh = %hh2%-%hh1%
::final answer should display in combined seconds
set /a final_time = %answer_hh%+%answer_mm%+%answer_ss%
echo Your time was: %final_time% (Seconds)
rem pause>nul
ping -n 2 -w 1000 >nul
goto :EOF

Report •

April 1, 2009 at 19:37:07
Call me lazy but I just use ntimer.exe from one of the windows resource kits(2003 I think).

Report •

Related Solutions

April 1, 2009 at 22:45:08
Thankyou All very much for your replies.
The first one wasn't exactly what i was after but it was what i was looking for for one
of my other programs.

The first one seems to be a replacement for ntimer.exe.

Thanks again,

EDIT: In the second one the last little section has a pause. That is a great idea to show messages for a small amount of time. I never thought about doing that. Thanks for the idea.

Report •

Ask Question