Right way to define time spent in Batch

patrick
Here is an example of how to calculate the time spent during batch script execution:

REM Start timer
call z_timer_start.bat

REM Action
...

REM Stop timer
call z_timer_finish.bat

And here are the scripts.

z_timer_start.bat

@echo off

REM Settings
set start_time=%time%

REM Get start time
for /f %%i in ('z_strtoint %start_time:~0,2%') do set /a hours_before=%%i
for /f %%i in ('z_strtoint %start_time:~3,2%') do set /a minutes_before=%%i
for /f %%i in ('z_strtoint %start_time:~6,2%') do set /a seconds_before=%%i

z_timer_finish.bat

@echo off

REM Settings
set finish_time=%time%
set debug=0

REM Get finish time
for /f %%i in ('z_strtoint %finish_time:~0,2%') do set /a hours_after=%%i
for /f %%i in ('z_strtoint %finish_time:~3,2%') do set /a minutes_after=%%i
for /f %%i in ('z_strtoint %finish_time:~6,2%') do set /a seconds_after=%%i

REM Make calc
set /a before=%hours_before% * 3600 + %minutes_before% * 60 + %seconds_before%
set /a after=%hours_after% * 3600 + %minutes_after% * 60 + %seconds_after%
set /a timediff=%after%-%before%

REM Show result
echo.
echo Total time spent: %timediff% seconds

REM Show debug
if "%debug%" == "1" (
	echo.
	echo Timer debug-info:
	
	echo.
	echo start_time:  %start_time%
	echo finish_time: %finish_time%
	
	echo.
	echo hours_before:   %hours_before%
	echo minutes_before: %minutes_before%
	echo seconds_before: %seconds_before%
	
	echo.
	echo hours_after:  %hours_after%
	echo minutes_after: %minutes_after%
	echo seconds_after: %seconds_after%
	
	echo.
	echo before: %before%
	echo after:  %after%
	echo timediff: %timediff%
)

And use z_strtoint from here remove-leading-zeros-from-string-in-batch/

You can download all files here: batch_timer