[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

batch file timer

Posted on 2006-04-30
12
Medium Priority
?
3,577 Views
Last Modified: 2010-05-18
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

0
Comment
Question by:christian1234
  • 8
  • 3
12 Comments
 
LVL 97

Assisted Solution

by:Lee W, MVP
Lee W, MVP earned 300 total points
ID: 16574178
Why don't you just download and use the 2000 resource kit utility, TimeThis.exe - see this:
http://www.microsoft.com/windows2000/techinfo/reskit/tools/existing/timethis-o.asp
0
 

Author Comment

by:christian1234
ID: 16574188
I know that exists, but is there a way to do this without any kit utility?
0
 

Author Comment

by:christian1234
ID: 16574236
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.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:christian1234
ID: 16574303
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:)
0
 

Author Comment

by:christian1234
ID: 16574335
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:)
0
 
LVL 11

Accepted Solution

by:
WelkinMaze earned 1200 total points
ID: 16574368
0
 
LVL 11

Expert Comment

by:WelkinMaze
ID: 16574419
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
0
 

Author Comment

by:christian1234
ID: 16574496
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?
0
 

Author Comment

by:christian1234
ID: 16574837
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







0
 

Author Comment

by:christian1234
ID: 16574960
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.
0
 
LVL 11

Expert Comment

by:WelkinMaze
ID: 16576972
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. :)
0
 

Author Comment

by:christian1234
ID: 16581719
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
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
There’s hardly a doubt that Business Communication is indispensable for both enterprises and small businesses, and if there is an email system outage owing to Exchange server failure, it definitely results in loss of productivity.
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question