View Single Post
  #3  
Old September 17th 20, 02:44 PM posted to microsoft.public.windowsxp.help_and_support
JJ[_14_]
external usenet poster
 
Posts: 46
Default Task time duration

On Wed, 16 Sep 2020 12:06:50 -0700 (PDT), wrote:
I am running a Robocopy task using a a script in a .bat file. What
command lines would I have to incorporate into the script that will post
somewhere (i.e. a text file) the start and ending time of the task so its
duration can be ascertained? This does not seem possible directly from
Robocopy itself, despite its multitude of switches.

All suggestions appreciated.


You can use JScript via MSHTA to get a number which is a timestamp of the
current time (since January 1st 1970) in milliseconds unit (1 second =
1000ms).

But because batch file can only handle signed 32-bit integer number (i.e.
from -2147483648 to 2147483647), and the timestamp number is larger than
2147483647, JScript is needed to calculate the timestamp difference (the
duration), as well as displaying the duration in a more meaningful format
such as: 1 hours, 10 minutes, 34.123 seconds.

e.g. (warning: long text)
note: the JScript code doesn't support time duration longer than a year.

Code:
@echo off
setlocal

rem get timestamp at start of task
call :GetTimestamp ts1
echo start timestamp = %ts1%

rem do time consuming task... this sample task takes about 4 seconds.
for /l %%A in (0,1,200000) do rem.

rem get timestamp at end of task
call :GetTimestamp ts2
echo end timestamp = %ts2%

rem get difference between two timestamps
call :CalcTimestampDelta td %ts2% %ts1%
echo timestamp delta = %td%

rem display duration
call :DurationStr d %td%
echo duration = %d%
goto :eof

:GetTimestamp {ResultVarName}
for /f %%A in ('mshta "javascript:(new ActiveXObject('scripting.filesystemobject')).getstandardstream(1).writeline((new Date()).getTime());close()"') do set %1=%%A
goto :eof

:CalcTimestampDelta {ResultVarName} {Timestamp1} {Timestamp2}
for /f %%A in ('mshta "javascript:(new ActiveXObject('scripting.filesystemobject')).getstandardstream(1).writeline(Math.abs(%3-%2));close()"') do set %1=%%A
goto :eof

:DurationStr {ResultVarName} {TimestampDelta}
for /f "delims=" %%A in ('mshta "javascript:t=new Date(%2);r=(t.getUTCSeconds()+t.getUTCMilliseconds()/1000)+' seconds';h=t.getUTCDate()*24+t.getUTCHours();r=((m=t.getUTCMinutes())||h?m+' minutes'+(r?' ':''):'')+r;r=(h?h+' hours'+(r?' ':''):'')+r;(new ActiveXObject('scripting.filesystemobject')).getstandardstream(1).writeline(r);close()"') do set %1=%%A
goto :eof
Ads