HOME
Stephen Knight
08/23/2012 08:20 PM
Time taken for batch file to execute

Type:

VBScript, Batch/Command file

Category:

Batch, Uptime, VBScript, Time
This script uses a small vbscript line to time a batch file. You can Start / Stop the timer and count how many secs, mins, hours between the two times - they can be on different days if needed and span midnight as it records date & time.

Another option is to use

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.

Show details for Alternate subroutines to get date using VBScript as otherwise it is dependent on system date format in %date%Alternate subroutines to get date using VBScript as otherwise it is dependent on system date format in %date%
Hide details for Alternative Start and End subroutines for date format including the day name:Alternative Start and End subroutines for date format including the day name:

: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



Hide details for CodeCode
@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


Hide details for ExplanationExplanation
Hide details for ExamplesExamples
This is the output from the above batch file:

START at 23/08/2012 21:23:45
Waiting
END at 23/08/2012 21:23:48
TIME TAKEN: 3 s
START at 23/08/2012 21:23:48
Waiting
END at 23/08/2012 21:24:33
TIME TAKEN: 45 s
Continuing
END at 23/08/2012 21:26:03
TIME TAKEN: 3 m



Hide details for AttachmentsAttachments