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.
Alternate subroutines to get date using VBScript as otherwise it is dependent on system date format in %date%
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
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
Code
@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
Explanation
Examples
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
Attachments