?
Solved

Generate Time Elapsed  in Batch file

Posted on 2012-08-23
27
Medium Priority
?
3,451 Views
Last Modified: 2012-09-07
I typically use something like this at the beginning and end of my scripts:

ECHO .
Beginning work at %TIME% on %DATE% for %COMPUTERNAME%.

Do stuff..

ECHO .
Ending work at %TIME% on %DATE% for %COMPUTERNAME%.

Question:

Is there a way to dump the system's TIME variable into a file or NUL, then when the script is complete, use the system's TIME variable again to convert the difference to another variable or ???

I need to be able to do something like this:

Time Elapsed: %TIME_ELAPSED%
0
Comment
Question by:zequestioner
  • 10
  • 5
  • 5
  • +3
27 Comments
 
LVL 26

Expert Comment

by:pony10us
ID: 38326197
While I could write something it is easier to point you to a site that I used when I needed to do this some time ago.  (I can't find my script from that time right now)

http://stackoverflow.com/questions/739606/how-long-a-batch-file-takes-to-execute
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38326203
I use a small utility for this that works well for me.  Check out TIMER.EXE at:

http://www.gammadyne.com/cmdline.htm

Here's the help screen:

Timer 2.1 - Copyright (C) 2005-2010 by Greg Wittmeyer - All Rights Reserved

Usage:  timer [/et] [/n] [/nologo] [/q] [/s]

Switches:
/et      When stopping the timer, display the current date and time.
/n       Display the elapsed time numerically (e.g. hh:mm:ss.uu)
/nologo  Hide the program banner.
/q       Quiet mode - do not report current time when starting timer.
/s       Stop timer and report elapsed time.

Open in new window

~bp
0
 
LVL 5

Expert Comment

by:kevinhigg
ID: 38326339
The simple answer is that it's possible, but not pretty ;)  This is trivial in most other technologies, but batch doesn't make this easy...  Consider this:

$start = get-date
start-sleep 5
$end = get-date
($end - $start).TotalSeconds

Open in new window


This is Powershell, and could be easily adapted to do about anything that you'd need.  In batch, you're kind of on your own.  You can use something like this to parse elements of the time:

@echo off

echo %time%
set hour=%time:~0,2%
set min=%time:~3,2%
set sec=%time:~6,2%
echo Hour: %hour%
echo Minute: %min%
echo Second: %sec%

Open in new window


Then you can use set /a to do the arithmetic, but you have to manage the boundaries.  Knowing that seconds depends on minutes depends on hours, for instance.  Good luck!
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 1

Expert Comment

by:carcarah
ID: 38326388
Try it
@echo off
setlocal ENABLEEXTENSIONS

set hora_inicial=%time%
echo Início em: %hora_inicial%

@rem call userscript.bat %*
PAUSE

set hora_final=%time%
echo.

echo Hora inicial: %hora_inicial%
echo Hora final: %hora_final%


set TEMPRESULT=%hora_inicial:~0,2%
call:FN_REMOVELEADINGZEROS
set ini_h=%TEMPRESULT%
@rem
set TEMPRESULT=%hora_inicial:~3,2%
call:FN_REMOVELEADINGZEROS
set ini_m=%TEMPRESULT%
@rem
set TEMPRESULT=%hora_inicial:~6,2%
call:FN_REMOVELEADINGZEROS
set ini_s=%TEMPRESULT%
@rem
set TEMPRESULT=%hora_inicial:~9,2%
call:FN_REMOVELEADINGZEROS
set ini_mile=%TEMPRESULT%

set TEMPRESULT=%hora_final:~0,2%
call:FN_REMOVELEADINGZEROS
set fin_h=%TEMPRESULT%
@rem
set TEMPRESULT=%hora_final:~3,2%
call:FN_REMOVELEADINGZEROS
set fin_m=%TEMPRESULT%
@rem
set TEMPRESULT=%hora_final:~6,2%
call:FN_REMOVELEADINGZEROS
set fin_s=%TEMPRESULT%
@rem
set TEMPRESULT=%hora_final:~9,2%
call:FN_REMOVELEADINGZEROS
set fin_mile=%TEMPRESULT%

set /A ini_total=(((((%ini_h%*60)+%ini_m%)*60)+%ini_s%)*100)+%ini_mile%
set /A fin_total=(((((%fin_h%*60)+%fin_m%)*60)+%fin_s%)*100)+%fin_mile%

set /A total=%fin_total% - %ini_total%

set /A total_mile=%total% %% 100
set total_mile=00%total_mile%
set total_mile=%total_mile:~-2%
set /A total=%total% / 100

set /A total_s="%total% %% 60"
set total_s=00%total_s%
set total_s=%total_s:~-2%
set /A total=%total% / 60

set /A total_m="%total% %% 60"
set total_m=00%total_m%
set total_m=%total_m:~-2%
set /A total=%total% / 60

set /A total_h="%total% %% 60"
@rem Handle if it wrapped around over midnight
if "%total_h:~0,1%"=="-" set /A total_h=%total_h% + 24

echo Total: %total_h%:%total_m%:%total_s%.%total_mile%

endlocal
goto END

:FN_REMOVELEADINGZEROS
if "%TEMPRESULT%"=="0" goto END
if "%TEMPRESULT:~0,1%" NEQ "0" goto END
set TEMPRESULT=%TEMPRESULT:~1%
goto FN_REMOVELEADINGZEROS

:END

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38326956
If it is of interest I have a little timing routine I have used for a while which I have just posted on my script pages for you:

http://scripts.dragon-it.co.uk/links/batch-find-time-taken

Here it is with an example in there.  Lets you START / END / ShowDiff at different points.  See the comments in the code.

Steve

@echo off
REM =======================================
REM Set of subroutines to make time differences easier in batch files using VBScript to do the maths.
REM Steve Knight, Aug 2012
REM http://scripts.dragon-it.co.uk/
REM =======================================
REM Call :START to "Start the clock"
REM Call :END to "Stop the clock"
REM Call :ShowDiff x to show the difference where x is s, n, h for secs, mins, hours
REM Once STARTed you can do multiple ENDs or do another START to reset
REM =======================================

REM =======================================
REM Examples
REM =======================================
call :START
  echo Waiting
  ping 127.0.0.1 -n 4 > NUL
call :END
call :ShowDiff s

call :START
  echo Waiting
  ping 127.0.0.1 -n 45 > NUL
CALL :END
call :ShowDiff s

  echo Continuing
  ping 127.0.0.1 -n 90 > NUL
CALL :END
call :ShowDiff n

EXIT/B
REM =======================================
REM Subroutines below here
REM =======================================
:START
  set start=%date% %time:~0,8%
  echo START at %start%
exit /b

:END
  set end=%date% %time:~0,8%
  echo END at %end%
exit /b

:ShowDiff
  REM Call with s,n,h for seconds, mins, hours. defaults to secs
  (set type=%~1)& if "%~1"=="" set type=s

  echo Wscript.Echo DateDiff("%type%", #%start%#,#%end%# ) > "%temp%\timediff.vbs"
  for /f %%s in ('cscript //nologo "%temp%\timediff.vbs"') do set TimeDiff=%%s
  del "%temp%\timediff.vbs"
  if %Type%=n set type=m
  echo TIME TAKEN: %TimeDiff% %Type%
exit /b

Open in new window

0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38327003
Steve,

You're clearly a closet vbscripter trying to get out...

:-)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38327031
Why re-invent the wheel ... been there done that as you have and we all can if needed but when there is a "DateDiff" command which can take two time AND dates reliably so it works across days, months, midnight etc.

Maybe I'm lazy :-)

Steve
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38327076
Hey, you saw my approach, a one line free util.  I always say "work smart, not hard".

Cheers,
~bp
0
 
LVL 26

Expert Comment

by:pony10us
ID: 38327228
Hello Bill and Steve,

Great scripts as usual.  The reason I went with the one I pointed to was because it ran the calculations "outside" the actual running of the batch for accuracy. If I recall correctly I was using it as a timecard process while I was working away from the office and they would only pay while I was actually working.  Bummer right?   :)
0
 
LVL 1

Author Comment

by:zequestioner
ID: 38327862
Using the above code from Steve, the script errors out the first time it calls :showdiff

I copied the code above into R.bat

D:\Scripts>R.bat
START at Thu 08/23/2012 19:12:27
Waiting
END at Thu 08/23/2012 19:12:30
C:\Users\me\AppData\Local\Temp\timediff.vbs(1, 28) Microsoft VBScript compilation
 error: Syntax error

=n was unexpected at this time.


timediff.vbs contains the following:

Wscript.Echo DateDiff("s", #Thu 08/23/2012 19:43:05#,#Thu 08/23/2012 19:43:08# )
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38328137
Sorry line 54 lost an = in my cut and pasting.... should be two == in there
Will check that it works as posted from pc shortly.

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38328409
Confirmed is working for me as posted except for that line #54
0
 
LVL 1

Expert Comment

by:carcarah
ID: 38328862
Well Steve, it is not working, maybe it is the reason :-p

Why re-invent the wheel ... been there done that as you have and we all can if needed but when there is a "DateDiff" command which can take two time AND dates reliably so it works across days, months, midnight etc.

Maybe I'm lazy :-)

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38328926
Was just a typo, works fine :-)
0
 
LVL 1

Author Comment

by:zequestioner
ID: 38330005
I think we're getting somewhere....

Now the script will complete, but I still get the compilation error. I changed line 54 to the following:

  if %Type%==n set type=m

START at Fri 08/24/2012  9:11:39
Waiting
END at Fri 08/24/2012  9:11:42
C:\Users\me\AppData\Local\Temp\timediff.vbs(1, 28) Microsoft VBScript compilation
 error: Syntax error

TIME TAKEN: Input s

START at Fri 08/24/2012  9:11:42
Waiting
END at Fri 08/24/2012  9:12:27
C:\Users\me\AppData\Local\Temp\timediff.vbs(1, 28) Microsoft VBScript compilation
 error: Syntax error

TIME TAKEN: Input s

Continuing
END at Fri 08/24/2012  9:13:56
C:\Users\me\AppData\Local\Temp\timediff.vbs(1, 28) Microsoft VBScript compilation
 error: Syntax error

TIME TAKEN: Input m

timediff.vbs now looks like this:

Wscript.Echo DateDiff("n", #Fri 08/24/2012  9:11:39#,#Fri 08/24/2012  9:11:42# )
0
 
LVL 1

Author Comment

by:zequestioner
ID: 38330076
The above is from a Win Vista machine... I get the same thing on Win 7, but no 'input'

Win 7 output:

TIME TAKEN: s
TIME TAKEN: s
TIME TAKEN: m
0
 
LVL 26

Expert Comment

by:pony10us
ID: 38330120
What version of datediff are you using?  3.0 is updated for Vista/7

http://www.freewarefiles.com/DateDiff_program_19138.html
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38330177
Not trying to be a salesman, but you really should take a look at TIMER in my post above, it makes this so easy, and keeps all the clutter out of the script.  And it uses very little extra time of its own to do the calculations...

Naturally if you absolutely can't use a helper EXE utility then I guess that's a deal breaker, but I didn't see that stated yet.

~bp
0
 
LVL 1

Expert Comment

by:carcarah
ID: 38330483
running my script running at Win7:


C:\>defhora.bat
Started: 15:02:05,24
waiting

Start time: 15:02:05,24
Final time: 15:02:07,97
Total: 0:00:02.73

C:\>
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38331021
Mine not working is classic %date% formatting not agreeing with one allowed in VBScript... works on mine but doesn't like your day mm/dd/yyyy format.  You can bodge it using %date:~4% to chop off the day part of the date, will see if can make this work better and update the version on my script page too.

Steve

echo off
REM =======================================
REM Set of subroutines to make time differences easier in batch files using VBScript to do the maths.
REM Steve Knight, Aug 2012
REM http://scripts.dragon-it.co.uk/
REM =======================================
REM Call :START to "Start the clock"
REM Call :END to "Stop the clock"
REM Call :ShowDiff x to show the difference where x is s, n, h for secs, mins, hours
REM Once STARTed you can do multiple ENDs or do another START to reset
REM =======================================

REM =======================================
REM Examples
REM =======================================
call :START
  echo Waiting
  ping 127.0.0.1 -n 4 > NUL
call :END
call :ShowDiff s

call :START
  echo Waiting
  ping 127.0.0.1 -n 45 > NUL
CALL :END
call :ShowDiff s

  echo Continuing
  ping 127.0.0.1 -n 90 > NUL
CALL :END
call :ShowDiff n

EXIT/B
REM =======================================
REM Subroutines below here
REM =======================================
:START
  set start=%date:~4% %time:~0,8%
  echo START at %start%
exit /b

:END
  set end=%date:~4% %time:~0,8%
  echo END at %end%
exit /b

:ShowDiff
  REM Call with s,n,h for seconds, mins, hours. defaults to secs
  (set type=%~1)& if "%~1"=="" set type=s

  echo Wscript.Echo DateDiff("%type%", #%start%#,#%end%# ) > "%temp%\timediff.vbs"
  for /f %%s in ('cscript //nologo "%temp%\timediff.vbs"') do set TimeDiff=%%s
  del "%temp%\timediff.vbs"
  if %Type%==n set type=m
  echo TIME TAKEN: %TimeDiff% %Type%
exit /b
0
 
LVL 1

Author Comment

by:zequestioner
ID: 38333495
Steve,

That did the trick. Your website is an amazing resource and I appreciate you updating it there for other users as well.

Cacarah,

I apologize but it was easier for me to use Steve's script to avoid language translation and his was very easy to understand as I know enough vbs to feel comfortable with it. I haven't seen DateDiff and it's a easy integration to batch.

Billprew,

Thanks for the offer but yes I would much rather do it within a single script. From the dragon.it website i see that you and Steve may work together occasionally.

Thanks guys!
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 2000 total points
ID: 38334074
Thanks, I've learned most of the stuff I use myself and for here from here frankly over the years -- SteveGTR, BillPrew, PaulTomasi, odBa, Qlemo etc... -- and thought I might aswell drop them into a database as I do ones that might be useful again.

I can still think of many better ways to do this but was what I had already used before.

If you replace the subroutines section with this it will make the date and time within a VBScript.  Swap the words day( and month( to get it in mm/dd/yyyy format instead.

REM =======================================
REM Subroutines below here
REM =======================================
:START
  call :GetDate
  set start=%now%
  echo START at %start%
exit /b

:END
  call :GetDate
  set end=%now%
  echo END at %end%
exit /b

:ShowDiff
  REM Call with s,n,h for seconds, mins, hours. defaults to secs
  (set type=%~1)& if "%~1"=="" set type=s

  echo Wscript.Echo DateDiff("%type%", #%start%#,#%end%# ) > "%temp%\timediff.vbs"
  for /f %%s in ('cscript //nologo "%temp%\timediff.vbs"') do set TimeDiff=%%s
  del "%temp%\timediff.vbs"
  if %Type%==n set type=m
  echo TIME TAKEN: %TimeDiff% %Type%
exit /b

:GetDate
  REM Gets date into %now% in date/time format the VBS understands -- dd/mm/yyyy hh:mm:ss here as by default now() or date()
  REM uses the built in date format which could be Mon 01-04-2012 etc.
  REM You may be able to just use %date% %time:~0,8% or %Date:~4% %time:~0,8% if preferred

  echo wscript.echo day(date) ^& "/" ^& month(date) ^& "/" ^& year(date) ^& " " ^& hour(time) ^& ":" ^& right(100+minute(time),2) ^&

":" ^& right(100+second(time),2) > "%temp%\now.vbs"
  for /f "tokens=*" %%d in ('cscript //nologo "%temp%\now.vbs"') do (set Now=%%d)
  del "%temp%\now.vbs" > NUL
exit /b

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38357480
If you got this working OK can you select an answer please to close it off?

thanks

Steve
0
 
LVL 1

Author Closing Comment

by:zequestioner
ID: 38374279
Sorry steve, i thought I did this already..
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38374403
No problem, glad it helped.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38374566
Interesting selection, I don't think I would have gone that way, but I respect your right to chose.  And Steve knows I love him either way :-).

So, what's typical runtimes for the job you are using this in?  I'm think it must be somewhat long, since there is a fair amount of overhead to Steve's VBS routines to get and calc times.  But given that, what unit are you using to report the elapsed time in (seconds, minutes, hours, etc)?  Typically when I display elapsed times I prefer something like 5:30 rather than 5.5 minutes, but that may just be personal taste.  Always like to hear other perspectives.

Steve, I think in this case I'd probably build the VBS support scripts in an editor outside of the BAT script, and just distribute them with the BAT.  Yes, more files to release, but easier to reuse, and makes the BAT script a lot cleaner, don't you thing.  Again, lots of ways to solve problems, but having a reusable VBS for use in BAT files that took a number of parms to drive it might be a very useful approach?

At any rate, glad you got something that meets your needs, that's what it's all about here.

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38375358
Agreed there about pre-building the VBScripts if they can be -- at least if there are multiple time checks probably best to do it once at the beginning of the scripts, or have it prebuilt anf pass in parameters.

Running a loop of routine as shown doing "end" and "showdiff" on this slow netbook I get about 3 a second.... not fast, but all depends whether this runs once across a script of 10 minutes or dozens of times and over periods of seconds or less I suppose.

If I get a chance will look at writing something more efficient.

Steve
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month16 days, 22 hours left to enroll

864 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