Question

batch file timer

Asked by: christian1234

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

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-04-30 at 15:23:59ID21832875
Tags

timer

,

batch

,

file

,

batch.file

Topic

Windows 2000 Operating System

Participating Experts
2
Points
500
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Batch Script DEL /F/S/Q path\file > c:\log.txt ??
    I have a batch here that deletes the temp folder on a user's computer (C:\Temp\) and I see all the files it deletes scrolling by, however I want this to be outputed into a log file (local or unc) however everything i try gives me an empty log file and the "Deleted C:\Tem...
  2. batch file
    id like to be able to have winzip grab a folder each night and zip the it automatically. The folder is about 11G in size and is at c:\backup I would like it to be saved as c:\backupfolder.zip Can a batch file be written to do this? Thanks
  3. What does this @rem mean?
    Hi At the top of a perl script, I see this @rem. Looks like comments, but I know comments start with #. So what does this mean? Thanks. Jirong @rem = ' PERL for Windows NT -- ccperl must be in search path @echo off ccperl %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 goto endofperl @rem '...
  4. REM Line More defined
    I am writing a batch file and don't like the REM line set out from the path name. I would like the REM line to be more like a Title or Explaination of a Process so that it is real clear to the user what it is doing. How can I accomplish this?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: leewPosted on 2006-04-30 at 15:30:07ID: 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

 

by: christian1234Posted on 2006-04-30 at 15:32:42ID: 16574188

I know that exists, but is there a way to do this without any kit utility?

 

by: christian1234Posted on 2006-04-30 at 15:49:41ID: 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.

 

by: christian1234Posted on 2006-04-30 at 16:15:49ID: 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:)

 

by: christian1234Posted on 2006-04-30 at 16:28:18ID: 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:)

 

by: WelkinMazePosted on 2006-04-30 at 16:39:04ID: 16574368

 

by: WelkinMazePosted on 2006-04-30 at 16:53:36ID: 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

 

by: christian1234Posted on 2006-04-30 at 17:15:52ID: 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?

 

by: christian1234Posted on 2006-04-30 at 19:16:11ID: 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







 

by: christian1234Posted on 2006-04-30 at 20:24:10ID: 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.

 

by: WelkinMazePosted on 2006-05-01 at 06:14:16ID: 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. :)

 

by: christian1234Posted on 2006-05-01 at 15:48:19ID: 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

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...