Task time duration

6 views
Skip to first unread message

ivandeni...@gmail.com

unread,
Sep 16, 2020, 3:06:53 PM9/16/20
to
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.

Barry Schwarz

unread,
Sep 16, 2020, 4:48:04 PM9/16/20
to
On Wed, 16 Sep 2020 12:06:50 -0700 (PDT), ivandeni...@gmail.com
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.

Before calling Robocopy: time /T > filename.txt

After Robocopy returns: time /T >> samefilename.txt

--
Remove del for email

JJ

unread,
Sep 17, 2020, 9:44:29 AM9/17/20
to
On Wed, 16 Sep 2020 12:06:50 -0700 (PDT), ivandeni...@gmail.com wrote:
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
[/code]

Ivan Denisovich

unread,
Oct 14, 2020, 12:23:55 PM10/14/20
to
Many thanks for your help
Reply all
Reply to author
Forward
0 new messages