Frank, I was up early this morning, and I discovered that my code is broken when the hour is less than 10. Although the system time string has a a leading space, this is stripped off when it is passed as a parameter to a batch file, unless:
1. You enclose it in quotes
2. You then dequote it in the batch file
See here
1. elapsed-calc.bat
@echo off
REM remove surrounding quotes (if any)
set time1=%~1
set time2=%~2
set hh1=%time1:~0,2%
set mm1=%time1:~3,2%
set ss1=%time1:~6,2%
set cs1=%time1:~9,2%
if "%hh1:~0,1%"==" " set hh1=%hh1:~1,1%
if "%mm1:~0,1%"=="0" set mm1=%mm1:~1,1%
if "%ss1:~0,1%"=="0" set ss1=%ss1:~1,1%
if "%cs1:~0,1%"=="0" set cs1=%cs1:~1,1%
set hh2=%time2:~0,2%
set mm2=%time2:~3,2%
set ss2=%time2:~6,2%
set cs2=%time2:~9,2%
if "%hh2:~0,1%"==" " set hh2=%hh2:~1,1%
if "%mm2:~0,1%"=="0" set mm2=%mm2:~1,1%
if "%ss2:~0,1%"=="0" set ss2=%ss2:~1,1%
if "%cs2:~0,1%"=="0" set cs2=%cs2:~1,1%
set /a msec1=3600000*%hh1%+60000*%mm1%+1000*%ss1%+10*%cs1%
set /a msec2=3600000*%hh2%+60000*%mm2%+1000*%ss2%+10*%cs2%
set /a elapsed=%msec2%-%msec1%
echo %elapsed%
2. Example of use in batch
@echo off
set time1=%time%
REM code to be timed here
set time2=%time%
REM surround time strings with quote marks
REM line edited to correct error found by Frank
for /f %%A in ('elapsed-calc.bat "%time1%" "%time2%"') do set elapsed=%%A
echo elapsed time %elapsed% msec
3. From the command line using command grouping (you did know about that?) to avoid typing delays when setting start & finish times
S:\Test>(
More? set tt1=
More? set tt2=
More? set tt1=%time%
More? wget -q http://www.programacion-tdt.com/Canal_24h.html
More? set tt2=%time%
More? elapsed-calc "%tt1%" "%tt2%"
More? )
15000
Command grouping works like this (apologies if you know this already!)
At the prompt you type an opening parenthesis ( + Enter. The command processor responds with More? and you type a command or program name & hit Enter & you get another More? prompt and so on until you type a closing parenthesis ) + Enter at which point the whole stored series of commands is executed in sequence almost as if they were in a batch file, with some differences obviously. (E.g. no multiline parenthetical expressions.)