Passing Elapsed Time from One Batch File To Another

jstevens
GoldLounger
Posts: 2631
Joined: 26 Jan 2010, 16:31
Location: Southern California

Passing Elapsed Time from One Batch File To Another

Post by jstevens »

I know how to capture elapsed time by using the following code.

Code: Select all

@echo off
echo Wscript.echo eval(WScript.Arguments(0))>evaluate.vbs
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "(timer)"') do set start=%%A

REM TASK TO BE TIMED HERE
REM Example:
ping www.eileenslounge.com

for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "(timer)"') do set end=%%A
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "(%end%-%start%)"') do set longnumber=%%A
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "FormatNumber(%longnumber%,2,-1)"') do set elapsed=%%A
echo times in seconds
echo start   %start%
echo end     %end%
echo elapsed %elapsed% second(s)

pause
What I would like to do is pass the elapsed time over to another batch file which sends out an email message. I already have the batch file that sends out the email message and would like to include the elapsed time within the email body.

Thanks,
John
Regards,
John

User avatar
John Gray
PlatinumLounger
Posts: 5408
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Passing Elapsed Time from One Batch File To Another

Post by John Gray »

Are you calling one batch file from the other? If so, the elapsed time can be passed as a parameter.

Otherwise you'll need to set up a one-line file, write it from the first batch file, and in the second look for its presence and read it when it turns up.
(Dunno what all that cscript is for, though!)

If all the second batch file does is to write a message, then why not incorporate the code in the first batch file? Would save a lot of effort...
John Gray

"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...

jstevens
GoldLounger
Posts: 2631
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Passing Elapsed Time from One Batch File To Another

Post by jstevens »

John,

Batch file #1 process and calls two additional Batch files. The last Batch file sends out an email that the task has completed along with the elapsed time it took to process everything.

The code above calcualates the elapsed time. If you have a better way of calculating it, please share your thoughts.

Regards,
John
Regards,
John

User avatar
John Gray
PlatinumLounger
Posts: 5408
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Passing Elapsed Time from One Batch File To Another

Post by John Gray »

It would depend on whether there was any interaction between the batch files!

Presumably there's a reason why you don't just put the code from the other two batch files 'in line' into the first and only batch file, saving the clock time when you start, and work out the elapsed time when you've finished? or do

:: save start time in a variable
call batch-file-one
call batch-file-two
:: save end time in another variable
:: calculate elapsed time and send out email message (using BLAT?)


Without knowing how you want to display the elapsed time, here's something which works out the elapsed time in milliseconds.

Code: Select all

call :timer start
:: lots of Stuff happens here now, which is to be timed
call :timer
goto :eof

::-----------------------------------------------------------------------------
:timer function: resolution quantum is 10 ms;
::               variable %TIMER% holds number of milliseconds since midnight
setlocal
:: get the current hours/minutes/seconds/centiseconds since midnight
for /f "tokens=5-8 delims=:,. " %%a in ('echo:^|time') do (
  set /a hh=%%a,mm=1%%b-100,ss=1%%c-100,cc=1%%d-100
)
if /i "%1"=="start" set timer=0
set /a timer=hh*3600000+mm*60000+ss*1000+cc*10-timer
:: VV  note that the /i parameter is not displayed in ECHOed statements
if /i not "%1"=="start" echo Elapsed time = %timer% msec
endlocal & set timer=%timer%
John Gray

"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...

jstevens
GoldLounger
Posts: 2631
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Passing Elapsed Time from One Batch File To Another

Post by jstevens »

John,

Unfortuantely I cannot consolidate the batch files into one as they touch various systems.

The elapsed time should display as ??HRS ??Minutes ??seconds.

I'm not familiar enough with batch files to tweak your code.

Regards,
John
Regards,
John

User avatar
StuartR
Administrator
Posts: 12610
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Passing Elapsed Time from One Batch File To Another

Post by StuartR »

John,

If you have the elapsed time in a variable called elapsed then you can call another procedure with the syntax
Call Procname %elapsed%

in the procedure called procname you can then
Set elapsed=%1
(or you can simply use a reference to %1 wherever you need the information)
StuartR


jstevens
GoldLounger
Posts: 2631
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Passing Elapsed Time from One Batch File To Another

Post by jstevens »

Stuart,

That's great. Now all I need is display the elapsed time in ??HRS ??Minutes ??seconds.

Thank you,
John
Regards,
John

User avatar
John Gray
PlatinumLounger
Posts: 5408
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Passing Elapsed Time from One Batch File To Another

Post by John Gray »

Here's an example routine for you, where I've made it linear. It's assumed that whatever'd done doesn't take more than 24 hours!

Code: Select all

@echo .off
:: example to measure the elapsed time in centisecond resolution
::   time spent doing calculations means that the results 
::   are too high by anything up to a second...

setlocal

:: calculate the start time in centiseconds since midnight
for /f "tokens=5-8 delims=:,. " %%a in ('echo:^|time') do (
  set /a hh=%%a,mm=1%%b-100,ss=1%%c-100,cc=1%%d-100
)
set /a start=360000*hh+6000*mm+100*ss+cc


:: here we would place the code/calls we wish to time
ping -n 123 127.0.0.1>nul


:: calculate the finish time in centiseconds since midnight
for /f "tokens=5-8 delims=:,. " %%a in ('echo:^|time') do (
  set /a hh=%%a,mm=1%%b-100,ss=1%%c-100,cc=1%%d-100
)
::  calculate the elapsed time in centiseconds
set /a elapsed=360000*hh+6000*mm+100*ss+cc-start

:: if we have gone over a midnight boundary, correct the elapsed time
if %elapsed% lss 0 set /a elapsed=8640000-elapsed

:: turn the centiseconds into hours, minutes and seconds
set /a t=elapsed,cc=t%%100,t/=100,ss=t%%60,t/=60,mm=t%%60,t/=60,hh=t%%24
echo Elapsed time = %hh% hrs %mm% mins %ss%.%cc% secs

endlocal
John Gray

"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...

jstevens
GoldLounger
Posts: 2631
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Passing Elapsed Time from One Batch File To Another

Post by jstevens »

John,

Thank you for the code. I was able to create a batch file that does exactly what I want.

Regards,
John
Regards,
John