Jump to content

Recommended Posts

Posted (edited)

I was looking for something like this, in the forum and my HDD. Bad hair day. Anyway,
Here is the code

If Not StringInStr($CmdLineRaw, "/ErrorStdOut") Then Exit MsgBox(262144, @ScriptName, "...please run this example from editor.", 3)

#include <Date.au3>; for _EstimatedTime()
#include <InetConstants.au3>; for _EstimatedTime_Example_2()

;===============================================================================
;
; Function Name:    _EstimatedTime() ; https://www.autoitscript.com/forum/topic/177371-_estimatedtime-calculate-estimated-time-of-completion/
; Description:      calculate estimated time of completion
; Parameter(s):     $a  - holds the data
;                   $iCurrentCount  - Current count
;                   $iTotalCount - Total count
; Requirement(s):   #include <Date.au3>
; Return Value(s):  false on incomplete data for assessment
; Author(s):        argumentum
; Note(s):          read the comments
;
;===============================================================================
Func _EstimatedTime(ByRef $a, $iCurrentCount = "", $iTotalCount = "")
    If $iCurrentCount & $iTotalCount = "" Then ; initialize
        $a = ""
        Dim $a[12]
        $a[5] = TimerInit() ; handle for TimerDiff()
        $a[0] = "00:00:00"
        $a[1] = "00:00:00"
        $a[2] = _NowCalc() ; starting date and time
        $a[3] = ""
        $a[4] = "0"
    Else
        If $iTotalCount = 0 Then Return False
        If $iCurrentCount > $iTotalCount Then $iCurrentCount = $iTotalCount
        _TicksToTime(Int(TimerDiff($a[5])), $a[6], $a[7], $a[8])
        _TicksToTime(Int((TimerDiff($a[5]) / $iCurrentCount) * ($iTotalCount - $iCurrentCount)), $a[9], $a[10], $a[11])
        $a[0] = StringFormat("%02i:%02i:%02i", $a[6], $a[7], $a[8]) ; elapsed time
        $a[1] = StringFormat("%02i:%02i:%02i", $a[9], $a[10], $a[11]) ; estimated total time
        $a[3] = _DateAdd("s", Int((TimerDiff($a[5]) / $iCurrentCount) * ($iTotalCount) / 1000), $a[2]) ; estimated date and time of completion
        $a[4] = Int($iCurrentCount / $iTotalCount * 100) ; percentage done
    EndIf
    Return True
EndFunc   ;==>_EstimatedTime

_EstimatedTime_Example_1()
Func _EstimatedTime_Example_1()
    ConsoleWrite(@CRLF & "+ Example 1" & @CRLF)
    ConsoleWrite("elapsed " & @TAB & "estimated" & @TAB & "starting date time " & @TAB & "estimated date time" & @CRLF)
    ConsoleWrite("                   _NowCalc() = " & _NowCalc() & @CRLF)
    Local $n, $sec = @SEC, $aETTF ; "Estimated Time To Finish" data holder
    _EstimatedTime($aETTF) ; called with just the data holder to init. it
    For $n = 1234 To 1 Step -1
        Sleep(1)
        If $sec <> @SEC Then
            $sec = @SEC
            _EstimatedTime($aETTF, 1234 - $n, 1234)
            ConsoleWrite($aETTF[0] & @TAB & $aETTF[1] & @TAB & $aETTF[2] & @TAB & $aETTF[3] & @TAB & $aETTF[4] & @CRLF)
        EndIf
    Next
    _EstimatedTime($aETTF, 1234 - $n, 1234)
    ConsoleWrite($aETTF[0] & @TAB & $aETTF[1] & @TAB & $aETTF[2] & @TAB & $aETTF[3] & @TAB & $aETTF[4] & @CRLF)
    ConsoleWrite("                                           _NowCalc() = " & _NowCalc() & @CRLF)
EndFunc   ;==>_EstimatedTime_Example_1

_EstimatedTime_Example_2()
Func _EstimatedTime_Example_2()
    ConsoleWrite(@CRLF & "+ Example 2" & @CRLF)
    ConsoleWrite("elapsed " & @TAB & "estimated" & @TAB & "starting date time " & @TAB & "estimated date time" & @CRLF)
    ConsoleWrite("                   _NowCalc() = " & _NowCalc() & @CRLF)
    Local $aETTF ; "Estimated Time To Finish" data holder
    _EstimatedTime($aETTF) ; called with just the data holder to init. it

    ; Save the downloaded file to the temporary folder.
    Local $sFilePath = @MyDocumentsDir & "\tempFile.bin"
    Local $sLink = "http://downloads.sourceforge.net/project/portableapps/PortableApps.com%20Platform/PortableApps.com_Platform_Setup_12.0.5.paf.exe?r=http%3A%2F%2Fsourceforge.net%2Fdirectory%2Fbusiness-enterprise%2Fos%3Awindows%2Ffreshness%3Arecently-updated%2F&ts=1442615522&use_mirror=iweb"

    ; Download the file in the background with the selected option of 'force a reload from the remote site.'
    Local $hDownload = InetGet($sLink, $sFilePath, $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND)

    ; Wait for the download to complete by monitoring when the 2nd index value of InetGetInfo returns True.
    Do
        Sleep(1000)
        If _EstimatedTime($aETTF, InetGetInfo($hDownload, $INET_DOWNLOADREAD), InetGetInfo($hDownload, $INET_DOWNLOADSIZE)) Then
            ConsoleWrite($aETTF[0] & @TAB & $aETTF[1] & @TAB & $aETTF[2] & @TAB & $aETTF[3] & @TAB & $aETTF[4] & @CRLF)
        Else
            ConsoleWrite(". ")
        EndIf
    Until InetGetInfo($hDownload, $INET_DOWNLOADCOMPLETE)

    ; Retrieve the number of total bytes received and the filesize.
    Local $iBytesSize = InetGetInfo($hDownload, $INET_DOWNLOADREAD)
    Local $iFileSize = FileGetSize($sFilePath)

    ; Close the handle returned by InetGet.
    InetClose($hDownload)

    _EstimatedTime($aETTF, $iBytesSize, $iFileSize)
    ConsoleWrite($aETTF[0] & @TAB & $aETTF[1] & @TAB & $aETTF[2] & @TAB & $aETTF[3] & @TAB & $aETTF[4] & @CRLF)
    ConsoleWrite("                                           _NowCalc() = " & _NowCalc() & @CRLF)

    ; Display details about the total number of bytes read and the filesize.
    ConsoleWrite("The total download size: " & $iBytesSize & " - The total filesize: " & $iFileSize & @CRLF)

    ; Delete the file.
    FileDelete($sFilePath)
EndFunc   ;==>_EstimatedTime_Example_2

 

Edited by argumentum
Tweak the code

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...