Link to home
Start Free TrialLog in
Avatar of christian1234
christian1234

asked on

batch file timer

Hi guys, does anybody know how to calculate the running time of a batch file. I would like to capture the starting time
and run some commands. After finishing the commands I get another evironment time variable.
Now my problem is to calculate the difference between the starting time and ending_time.
Please help me. The code skeleton is below.

The batch file looks like this:

echo Off
rem capturing the starting time
SET start_time=%time%
echo starting at %start_time% >> log.txt

rem here i want to put the batch file commands
.......
rem end of commands

rem capturing the end time and calculate the elapsed time of the commands
SET ending_time=%time%
echo ending at %ending_time% >> log.txt
rem now i need to calculate the differences between starting_time and ending_time to get the elapsed time

SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of christian1234
christian1234

ASKER

I know that exists, but is there a way to do this without any kit utility?
I would like to be able to output just the elapsed time of the batch file commands to
any log file in the following format mm:ss.mm.
Thank you soo much for any help.
Assuming I use the 2000 resource kit.
Now I am putting a loop into a bat file like the following just to make the computer work for a while.
rem commands.bat:
SET counter=0
goto LOOP
:LOOP
SET /a counter=%counter%+1

if 1000 == %counter% (
GOTO EXIT
)
GOTO LOOP
:EXIT
rem end commands.bat

How can I use the timethis method to just get elapsed time output to a log.txt file?
I assume that I am able to call the commands.bat file like
timethis "command.bat >> log.txt"
But the problem is that I do not want starting time and endtime in the log, just the elapsed time.
Thank you soo much for any clue. And also let me know if my task does not make any sense:)
the resource kit seems to be pretty cool, just to clarify my problem.
The following bat file contains:
timethis.exe commands.bat >> log.txt

this redirects all the loop steps in the command.bat file to the log.txt file.
And the last part contains the timethis information. This is the last part of the log.txt file for one run:

C:\Documents and Settings\Chris\Desktop\timer>SET /a counter=998+1

C:\Documents and Settings\Chris\Desktop\timer>if 1000 == 999 (GOTO EXIT )

C:\Documents and Settings\Chris\Desktop\timer>GOTO LOOP

C:\Documents and Settings\Chris\Desktop\timer>SET /a counter=999+1

C:\Documents and Settings\Chris\Desktop\timer>if 1000 == 1000 (GOTO EXIT )

TimeThis :  Command Line :  commands.bat
TimeThis :    Start Time :  Sun Apr 30 19:22:12 2006
TimeThis :  Command Line :  commands.bat
TimeThis :    Start Time :  Sun Apr 30 19:22:12 2006
TimeThis :      End Time :  Sun Apr 30 19:22:14 2006
TimeThis :  Elapsed Time :  00:00:02.187

My question is, how can I just get the elapsed time to the log file and not all the other stuff.
Please help:)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
about the question from your last posting - "how can I just get the elapsed time to the log file and not all the other stuff"
1) you may try to use "echo off/on" in the beginning/end of your batch file
2) you may see the help about FOR command in Windows 2000 command prompt window. There are ways to parse some info from a file
I tried several different ways with echo Off, echo On.
I am having the command.bat file containing a loop to spent some time.
This bat file is called from another .bat file containing the timethis command like this:
echo On
rem test.bat
timethis "commands.bat >> log.txt"

rem commands.bat
echo Off
SET counter=0

goto LOOP
:LOOP
SET /a counter=%counter%+1

if 1000 == %counter% (
GOTO EXIT
)


GOTO LOOP
:EXIT
echo On

playing around with the echo off or on i get either no output at all in the log.txt or all the not needed commands.
How can i just extract the elapsed time from the file?
Thank you welkin, I need to recheck my algorithm there might be still a flaw in there.
But your url link brought me to the idea to calculate the elapsed time without any resource kit. Finding
specific parts in the file, particular the elapsed time part using the timethis function would be even harder i guess.

Here is what i have to far:

Set start_time=%time%

echo Off
SET start_time=%time%
echo starting at %start_time% >> log.txt
rem here put all the commands
rem the for loop simulates the commands I want to clock

SET counter=0

goto LOOP
:LOOP
SET /a counter=%counter%+1

if 1000 == %counter% (
GOTO EXIT
)


GOTO LOOP
:EXIT

rem end of commands

SET ending_time=%time%
echo ending at %ending_time% >> log.txt

rem initialize variables, have to make sure that we work with integers, get rid of leading zero's

SET start_Hr=%start_time:~1,1%
SET start_Min=%start_time:~3,2%
SET start_Sec=%start_time:~6,2%
SET start_Mil=%start_time:~9,2%

SET end_Hr=%ending_time:~1,1%
SET end_Min=%ending_time:~3,2%
SET end_Sec=%ending_time:~6,2%
SET end_Mil=%ending_time:~9,2%

rem verifying start_time



if 0 == %start_time:~0,1% (
SET start_Hr=%start_time:~1,1%
)

if 0 == %start_time:~3,1% (
SET start_Min=%start_time:~4,1%
)

if 0 == %start_time:~6,1% (
SET start_Sec=%start_time:~7,1%
)

if 0 == %start_time:~9,1% (
SET start_Mil=%start_time:~10,1%
)

echo text >> log.txt

rem verifying ending_time

if 0 == %ending_time:~0,1% (
SET end_Hr=%ending_time:~1,1%
)

if 0 == %ending_time:~3,1% (
SET end_Min=%ending_time:~4,1%
)

if 0 == %ending_time:~6,1% (
SET end_Sec=%ending_time:~7,1%
)

if 0 == %ending_time:~9,1% (
SET end_Mil=%ending_time:~10,1%
)


rem initialize output variables

SET out_Min=0
SET out_Sec=0
SET out_Mil=0

SET remainder=0

rem calculating difference

if %end_Mil% gtr %start_Mil% (
Set /a out_Mil=%end_Mil%-%start_Mil%
)

if %start_Mil% gtr %end_Mil% (
Set /a out_Mil=100-%start_Mil%+%end_Mil%
Set remainder=1
)

if %end_Sec% gtr %start_Sec% (
Set /a out_Sec=%end_Sec%-%start_Sec%-%remainder%
Set remainder=0
)

if %start_Sec% gtr %end_Sec% (
Set /a out_Sec=60-start_Sec%+%end_Sec%-%remainder%
Set remainder=1
)

if%end_Min% gtr %start_Min% (
Set /a out_Min=%end_Min%-%start_Min%-%remainder%
Set remainder=0
)

if %end_Min% gtr %start_Min% (
Set /a out_Min=60-start_Min%+%end_Min%-%remainder%
Set remainder=1
)

if%end_Hr% gtr %start_Hr% (
Set /a out_Hr=%end_Hr%-%start_Hr%-%remainder%
)

if %end_Min% gtr %start_Min% (
Set /a out_Sec=24-start_Hr%+%end_Hr%-%remainder%
)

echo elapsed time %out_Hr%:%out_Min%:%out_Sec%.%out_Mil% >> log.txt

I thought this little tool comes handy







Ok, here is my final version. If still any bucks, please let me know:)
Thank you again for the help, was fun figuring this out here.
Here it is:
echo Off

SET start_time=%time%
echo starting at %start_time% >> log.txt
rem here put all the commands

SET counter=0

goto LOOP
:LOOP
SET /a counter=%counter%+1

if 100000 == %counter% (
GOTO EXIT
)


GOTO LOOP
:EXIT


rem end of commands

SET ending_time=%time%
echo ending at %ending_time% >> log.txt

rem initialize variables, have to make sure that we work with integers, get rid of leading zero's

SET start_Hr=%start_time:~1,1%
SET start_Min=%start_time:~3,2%
SET start_Sec=%start_time:~6,2%
SET start_Mil=%start_time:~9,2%

SET end_Hr=%ending_time:~1,1%
SET end_Min=%ending_time:~3,2%
SET end_Sec=%ending_time:~6,2%
SET end_Mil=%ending_time:~9,2%

rem verifying start_time



if 0 == %start_time:~0,1% (
SET start_Hr=%start_time:~1,1%
)

if 0 == %start_time:~3,1% (
SET start_Min=%start_time:~4,1%
)

if 0 == %start_time:~6,1% (
SET start_Sec=%start_time:~7,1%
)

if 0 == %start_time:~9,1% (
SET start_Mil=%start_time:~10,1%
)

rem verifying ending_time
if 0 == %ending_time:~0,1% (
SET end_Hr=%ending_time:~1,1%
)

if 0 == %ending_time:~3,1% (
SET end_Min=%ending_time:~4,1%
)

if 0 == %ending_time:~6,1% (
SET end_Sec=%ending_time:~7,1%
)

if 0 == %ending_time:~9,1% (
SET end_Mil=%ending_time:~10,1%
)


rem initialize output variables

SET out_Min=0
SET out_Sec=0
SET out_Mil=0

SET remainder=0

rem calculating difference

if %end_Mil% gtr %start_Mil% (
Set /a out_Mil=%end_Mil%-%start_Mil%
)

if %start_Mil% gtr %end_Mil% (
Set /a out_Mil=100-%start_Mil%+%end_Mil%
Set remainder=1
)

if %end_Sec% gtr %start_Sec% (
Set /a out_Sec=%end_Sec%-%start_Sec%-%remainder%
Set remainder=0
)

if %start_Sec% gtr %end_Sec% (
Set /a out_Sec=60-%start_Sec%+%end_Sec%-%remainder%
Set remainder=1
)

if%end_Min% gtr %start_Min% (
Set /a out_Min=%end_Min%-%start_Min%-%remainder%
Set remainder=0
)

if %start_Min% gtr %end_Min% (
Set /a out_Min=60-start_Min%+%end_Min%-%remainder%
Set remainder=1
)

if%end_Hr% gtr %start_Hr% (
Set /a out_Hr=%end_Hr%-%start_Hr%-%remainder%
)

if %start_Hr% gtr %end_Hr% (
Set /a out_Hr=24-start_Hr%+%end_Hr%-%remainder%
)

echo elapsed time %out_Hr%:%out_Min%:%out_Sec%.%out_Mil% >> log.txt

Have a good night and thank u for the help.
t's really good that you're trying to figure things out without waiting the complete solution from somebody else.
I'm glad that the link has helped you and it works for you. :)
Yeah, well did not have too much time. Thanks again.
Going to give u all the points for the links and for sticking with me:)
GTR helped me out in the if statements.
Regards,
Christian