0

I want to make a batch script that counts how long it's been run in seconds. How can I do this?

Daniel
  • 2,339
  • 9
  • 21
  • 28
Mrmajik45
  • 99
  • 9
  • 2
    This sounds like an [XY problem](http://xyproblem.info/). What are you actually trying to accomplish by determining how long the script has been running? – SomethingDark Oct 14 '18 at 06:23
  • 1
    See: [Calculate time difference in Windows Batch file](https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file/9935540#9935540) – Aacini Oct 14 '18 at 14:11

2 Answers2

0

Hope you would like it. >u<


Add this into the first line of the batch that you wanted to count its running time:

echo %time% >"starting time.any-file-type"

And that into the end(s) of the batch file:

(start) "counter.bat"


Code of "counter.bat"

@echo off
set thetimenow=%time%
set /p thetimebefore=<"starting time.any-file-type"
set /a hours=%thetimenow:~0,2% - %thetimebefore:~0,2%
set /a mins=%thetimenow:~3,2% - %thetimebefore:~3,2%
set /a seconds=%thetimenow:~6,2% - %thetimebefore:~6,2%
set /a minsecs=%thetimenow:~9,2% - %thetimebefore:~9,2%
if %minsecs% lss 0 (set /a seconds-=1
set /a minsecs+=100)
if %seconds% lss 0 (set /a mins-=1
set /a seconds+=60)
if %mins% lss 0 (set /a hours-=1
set /a mins+=60)
if %hours% lss 0 (set /a days=1
set /a hours+=24)
if not "%days%" == "1" (set days=) else (set days=1 or more days, )
echo The batch required has ran for %days%%hours% hour(s), %mins% minute(s), %seconds% second(s), and %minsecs% miniseconds.

That's the best that I can give.

Note that the required batch must NOT contain the keyword exit.

If needed, use chcp 437 right before the line which contains %time%.

Make sure the batches are at the same folder if you don't wish to modifiy the code which calls for the counter.

Arnold Lai
  • 75
  • 9
0

...

rem Change formatting for the start and end times
for /F "tokens=1-4 delims=:.," %%a in ("%function_starttime%") do (
   set /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)

for /F "tokens=1-4 delims=:.," %%a in ("%function_endtime%") do (
   set /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)

REM To avoid rollover errors, add 24 hours (in hundredths of a 
REM second) if the end time is past midnight
REM 24 hours * 60 minutes * 60 seconds * 100 = 8,640,000 hundredths of
REM a second
if %end% lss %start% set /A end=end+(24*60*60*100)

rem Calculate the elapsed time by subtracting values
set /A elapsed=end-start

...

Community
  • 1
  • 1
Hackoo
  • 15,943
  • 3
  • 28
  • 59