Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

make a batch file report the time it took to run into a text file

Posted on 2009-07-08
9
Medium Priority
?
300 Views
Last Modified: 2012-05-07
Hi guys

Can anyone help with my creation of a batch file to report into a text file?

This is what I want to achieve, I would like to run a simple batch file and at the end of it have the batch file report the amount of seconds it took to run into a text file.

Is this possible? and if so has anyone any ideas of the code required to have it time itself and then to report into a text file?

Im happy for it to report a unique time at the start of it running and also at the end which in essence is a good way of seeing how long it took to run.

Can anyone help? :)

thanks
0
Comment
Question by:ScumPuppy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 19

Expert Comment

by:deroode
ID: 24801463
that would be something like:

echo --start-- > test.txt
time /t >> test.txt
 
rem the rest of your batch commands
echo --end-- >> test.txt
time /t >> test.txt

Open in new window

0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 1000 total points
ID: 24801641
I will assume your task(s) will not take more than 24 hours to run.

Replace your tasks inbetween Set StartTime and Set EndTime. Or have this batch file time another running batch file.

Save the code something like timer.cmd:

Then pass the batch filename you want to time into timer.cmd. Eg. test.cmd on your desktop.

timer.cmd "%UserProfile%\Desktop\test.cmd"
@ECHO Off
@ECHO Doing stuff please wait...
Set StartTime=%Time%
 
Call %1
 
Set EndTime=%Time%
 
Set STHour=1%StartTime:~0,2%
Set STHour=%STHour: =0%
Set STMinute=1%StartTime:~3,2%
Set STSecond=1%StartTime:~6,2%
Set STHundredth=1%StartTime:~9,2%
 
Set EndHour=1%EndTime:~0,2%
Set EndHour=%EndHour: =0%
Set EndMinute=1%EndTime:~3,2%
Set EndSecond=1%EndTime:~6,2%
Set EndHundredth=1%EndTime:~9,2%
 
If %EndHundredth% LSS %STHundredth% (
    Set /a EndHundredth+=100
    Set /a EndSecond-=1
)
 
 
If %EndSecond% LSS %STSecond% (
    Set /a EndSecond+=60
    Set /a EndMinute-=1
)
 
If %EndMinute% LSS %STMinute% (
    Set /a EndMinute+=60
    Set /a EndHour-=1
)
 
If %EndHour% LSS %STHour% Set /a EndHour+=24
 
Set /a TimeDiffHour=1%EndHour%-%STHour%
Set /a TimeDiffMinute=1%EndMinute%-%STMinute%
Set /a TimeDiffSecond=1%EndSecond%-%STSecond%
Set /a TimeDiffHundredth=1%EndHundredth%-%STHundredth%
 
Set TimeDiff=%TimeDiffHour:~-2%:%TimeDiffMinute:~-2%:%TimeDiffSecond:~-2%.%TimeDiffHundredth:~-2%
 
Echo Start time: %StartTime%
Echo End time: %EndTime%
Echo Elapsed time: %TimeDiff%

Open in new window

0
 
LVL 16

Expert Comment

by:t0t0
ID: 24802399
AmazingTech

It's nice to see a programatic approach. It looks good.

ScumPuppy

I'm not looking for points here - both AmazingTech and deroode desreve them for their efforts.

The last three lines of AmazingTech's code will display the times on the screen. To have these sent to a file instead, swap the last three lines with the follows modified lines:

   Echo Start time: %StartTime% >textfile.txt
   Echo End time: %EndTime% >>textfile.txt
   Echo Elapsed time: %TimeDiff% >>textfile.txt

This will output the times to a file named TEXTFILE.TXT.

ON A SIDE-NOTE:

1) It's conventional to name the output file with a .LOG extension name as in: TEXTFILE.LOG.

2) Furthermore, it's conventional to name the .LOG file the same name as the batch file itself.

The following modified lines (instead of the one's above) will achieve this:

   Echo Start time: %StartTime% >%~n0.log
   Echo End time: %EndTime% >>%~n0.log
   Echo Elapsed time: %TimeDiff% >>%~n0.log

So, if you name AmazingTech's batch file TIMERUN.BAT then the times will be output to a file (automatically created and) named TIMERUN.LOG.



0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 1

Author Comment

by:ScumPuppy
ID: 24802552
Guys, all of these are fantastic responses!!! thank you..

Is there a way it will detail the time it took to run in seconds as well as hours and minutes?

It would be extra special if the time I get back from this showed it to the second as well :)

I dont know if thats possible though
0
 
LVL 19

Expert Comment

by:deroode
ID: 24802600
Amazingtech's script should show you the seconds and hundreths of a second...
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24802715
AmazingTech's code gives you the start time, end time AND time difference ALL to an accuracy of 1/100th of a second.

Your question could be considered rude as it suggests you have not taken the time to either examine the replies already given nor tried any of the code because if you had, you would already know the answer to your last question.
0
 
LVL 1

Author Closing Comment

by:ScumPuppy
ID: 31600984
excellent suggestion! many thanks :) works a treat!!
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24806471
No problem. I think ScumPuppy meant was to display 1 minute 5 seconds 76 one hundredth of a second as 65.76 seconds. That's a bit more tricky since I was using a 1 at the beginning to get around adding 08 and 09 in DOS which thinks it's an invalid octal number.

I will post a code shortly.
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24816101
OK. Give this a try.

I also added if exist then call the batch file otherwise run the command.
@ECHO On
@ECHO Doing stuff please wait...
Set StartTime=%Time%
Set StartTime= 9:36:47.31
if exist %1 (
    Call %*
) else (
    %*
)
 
Set EndTime=%Time%
 
Set STHour=1%StartTime:~0,2%
Set STHour=%STHour: =0%
Set STMinute=1%StartTime:~3,2%
Set STSecond=1%StartTime:~6,2%
Set STHundredth=1%StartTime:~9,2%
 
Set EndHour=1%EndTime:~0,2%
Set EndHour=%EndHour: =0%
Set EndMinute=1%EndTime:~3,2%
Set EndSecond=1%EndTime:~6,2%
Set EndHundredth=1%EndTime:~9,2%
 
If %EndHundredth% LSS %STHundredth% (
    Set /a EndHundredth+=100
    Set /a EndSecond-=1
)
 
 
If %EndSecond% LSS %STSecond% (
    Set /a EndSecond+=60
    Set /a EndMinute-=1
)
 
If %EndMinute% LSS %STMinute% (
    Set /a EndMinute+=60
    Set /a EndHour-=1
)
 
If %EndHour% LSS %STHour% Set /a EndHour+=24
 
Set /a TimeDiffHour=1%EndHour%-%STHour%
Set /a TimeDiffMinute=1%EndMinute%-%STMinute%
Set /a TimeDiffSecond=1%EndSecond%-%STSecond%
Set /a TimeDiffHundredth=1%EndHundredth%-%STHundredth%
 
Set TimeDiff=%TimeDiffHour:~-2%:%TimeDiffMinute:~-2%:%TimeDiffSecond:~-2%.%TimeDiffHundredth:~-2%
Set /a TimeDiffSeconds=((1%TimeDiffHour:~-2% - 100) * 3600) + ((1%TimeDiffMinute:~-2% - 100) * 60) + (1%TimeDiffSecond:~-2% - 100)
Set TimeDiffSeconds=%TimeDiffSeconds%.%TimeDiffHundredth:~-2%
 
Echo Start time: %StartTime%
Echo End time: %EndTime%
Echo Elapsed time: %TimeDiff%
Echo Elapsed time (seconds): %TimeDiffSeconds%

Open in new window

0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

722 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