Link to home
Start Free TrialLog in
Avatar of Culwatrnca11
Culwatrnca11Flag for United States of America

asked on

script to show hours, minutes and seconds. Used to measure how long a certain batch file takes to run.

Modify the below to show hours, minutes and seconds for run time.Currently shows seconds only and I have to figure out the minutes, etc.. I want it to show hours, min and sec in the popup box when done. 

Purpose shows how long it took to run BOs_main.bat

Set WshShell = WScript.CreateObject("WScript.Shell")
sCmd = chr(34) & "c:\Final Reports\BOs_main.bat" & chr(34)
dtmStartTime = Timer
Return = WshShell.Run(sCmd, 1, true)
Wscript.Echo "The task completed in " & Round(Timer - dtmStartTime, 2) & " seconds."

Open in new window


Avatar of zc2
zc2
Flag of United States of America image

Try this:
dim dd, ds
dd = TimeSerial( 0, 0, Timer() - dtmStartTime )
ds = Hour(dd) & "h " & Minute(dd) & "m " & Second( dd ) & "s"
Wscript.Echo "The task completed in " & ds

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Arana (G.P.)
Arana (G.P.)

Set WshShell = WScript.CreateObject("WScript.Shell")
sCmd = chr(34) & "c:\Final Reports\BOs_main.bat" & chr(34)
dtmStartTime = Timer
Return = WshShell.Run(sCmd, 1, true)

s=Round(Timer - dtmStartTime, 2)

Hours = s \ 3600
Minutes = (s \ 60) Mod 60
Seconds = (s Mod 3600) Mod 60

Wscript.Echo "the script completed in " & Format(Hours, "00") & ":" & Format(Minutes, "00") & ":" & Format(Seconds, "00") & " That is: " & Hours & " hours " & Minutes & " minutes" & Seconds & " seconds"
Avatar of Culwatrnca11

ASKER

how do i work either one of the suggestions into my existing VBA?  What would my final code look like?

how do i work either one of the suggestions into my existing VBA? 
just add the function on top of your file
and call it whenever you need...

Function FormatTime(s)
  Dim t, a, sec
  sec = Int(s)
  a = Array(CStr(Right("00" & Int(sec / 3600) Mod 24, 2)), CStr(Right("00" & Int(sec / 60) Mod 60, 2)), CStr(Right("00" & sec Mod 60, 2)))
  FormatTime = Join(a, ":")
End Function

'your code here...

Wscript.Echo "The task completed in " & FormatTime(Timer - dtmStartTime) & " seconds."

Open in new window





Arana,

That i get an error line 15, char 190, expected end of statement, Code 800A0401
I had a typo, already edited, try again.
HainKurt,

your solution solved my issue. I have another issue that could be added to this code but i will submit a new issue.

Thank you Again. 
I completly forgot VBscript doesnt have the format function, it would work for VBA,
so just replace FORMAT (Hours,"00")  with Right("00" & Hours,2)  , the same for minutes and seconds