[Function] DownloadWithProgress

Post your working scripts, libraries and tools.
User avatar
dd900
Posts: 121
Joined: 27 Oct 2013, 16:03

[Function] DownloadWithProgress

19 Feb 2023, 15:18

Original post and function here.
Credit goes to @Bruttosozialprodukt

Another post here

This could probably use some error handling and possibly some more parameters for customization, but it does work as is.
Eventually I plan to re-write the function to allow cancelling the download. As far as I'm aware Download() does not have an option to cancel/abort the download. Correct me if I'm wrong.

Code: Select all

DownloadWithProgress(downloadUrl, outputFile) {
    _lastSize := 0
    , _lastSizeTick := 0
    , _url := downloadUrl
    , _file := outputFile
    , _fileSize := __GetNetworkFileSize()
    , _Gui := Gui("+ToolWindow -SysMenu -DPIScale", _url)
    , _speedSizeText := _Gui.AddText("xm ym w580 h20 Center", "Ready...")
    , _progressBar := _Gui.AddProgress("xm y+m wp hp")
    , _Gui.Show("AutoSize Center")
    , SetTimer(__Update, 100)
    , Download(_url, _file)
    , SetTimer(__Update, 0)
    , _Gui.Destroy()
    return

    __GetNetworkFileSize() {
        wr := ComObject("WinHttp.WinHttpRequest.5.1")
        , wr.open("HEAD", _url, false)
        , wr.send()
        , out := wr.getResponseHeader("Content-Length")
        return out
    }

    __Update() {
        if (!FileExist(_file))
            return
        
        currentSize := FileGetSize(_file)
        , currentSizeTick := A_TickCount
        , speed := Round((currentSize / 1024 - _lastSize / 1024) / ((currentSizeTick - _lastSizeTick) / 1000))
        , _lastSizeTick := currentSizeTick
        , _lastSize := currentSize
        , percentDone := Round(currentSize / _fileSize * 100)
        , _speedSizeText.Value := currentSize " of " _fileSize " bytes Downloaded @ " speed " Kb/s"
        , _progressBar.Value := percentDone
        , _Gui.Title := "Downloading " _url " (" percentDone "%)"
        return
    }
}
Ben G
Posts: 42
Joined: 22 Feb 2023, 20:53
Contact:

Re: [Function] DownloadWithProgress

25 Feb 2023, 14:49

@dd900 Nice! Like it. :thumbup:
Too bad it causes a zero-division error on line 32:
Image
If you hit continue, it works fine, though.
So I'd put that line of code in a try block to catch the error.
-Ben G
My Website (it's still in production).
Running Windows 11 Home on a 64-bit laptop.
User avatar
dd900
Posts: 121
Joined: 27 Oct 2013, 16:03

Re: [Function] DownloadWithProgress

27 Feb 2023, 01:31

Ben G wrote:
25 Feb 2023, 14:49
@dd900 Nice! Like it. :thumbup:
Too bad it causes a zero-division error on line 32:
Image
If you hit continue, it works fine, though.
So I'd put that line of code in a try block to catch the error.
I've used it hundreds of times now and have never had that error. I can't see a place in the code where the divisor could possibly be 0. Other than if _fileSize was 0. In which case the progress bar wouldn't work at all.

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 9 guests