Jump to content

Trying to make a file downloader with progress bar


jjump
 Share

Recommended Posts

Hi,

I am trying to build a file downloader which shows the progress. This can either be via showing a percentage, showing how many bytes have been downloaded (i.e. 100Kb / 1000kb) or by the progress bar.. I have looked in the help file, but really do not understand how to make the example read how many bytes have been transferred.. This is the GUI part of the downloader so far;

CODE

Opt("GuiOnEventMode", 1)

#include <ButtonConstants.au3>

#include <GUIConstantsEx.au3>

#include <ProgressConstants.au3>

#include <StaticConstants.au3>

#include <WindowsConstants.au3>

#Region ### START Koda GUI section ###

Form=file downloader.kxf

$Form1_1 = GUICreate("ClassicGamingPresents.com", 500, 210, 193, 129, BitOR($WS_SYSMENU,$WS_DLGFRAME,$WS_POPUP,$WS_CLIPSIBLINGS))

GUISetIcon("cgp.ico")

GUISetBkColor(0xA6CAF0)

$Button1 = GUICtrlCreateButton("Download", 40, 152, 193, 49, 0)

GUICtrlSetOnEvent($Button1, "Download")

GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")

$Progress1 = GUICtrlCreateProgress(176, 104, 281, 41)

$Pic1 = GUICtrlCreatePic("aniglob.gif", 16, 16, 145, 129, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))

$Label1 = GUICtrlCreateLabel("Downloading From: ", 176, 88, 285, 17)

$Label2 = GUICtrlCreateLabel("Download Transport Tycoon", 176, 24, 309, 33)

GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")

$Button2 = GUICtrlCreateButton("Exit", 264, 152, 193, 49, 0)

GUICtrlSetOnEvent($Button2, "Terminate")

GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")

GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

Any help would be great!

Ian

Edited by jjump
Link to comment
Share on other sites

Hi,

I am trying to build a file downloader which shows the progress. This can either be via showing a percentage, showing how many bytes have been downloaded (i.e. 100Kb / 1000kb) or by the progress bar.. I have looked in the help file, but really do not understand how to make the example read how many bytes have been transferred.. This is the GUI part of the downloader so far;

Here's a (lightly tested) example that may be of use in trying to understand the tracking and displaying of the progress:

#include <GUIConstants.au3>
AutoItSetOption("GUIOnEventMode", 1)

$GUITitle = "Download Firefox"
$GUIWidth = 400
$GUIHeight = 55
Dim $iDloadFinalSize = ""
Dim $iDloadCurrentSize = ""

$GUI = GUICreate($GUITitle, $GUIWidth, $GUIHeight, (@DesktopWidth - $GUIWidth) / 2, (@DesktopHeight - $GUIHeight) / 2)
$buttonDownload = GUICtrlCreateButton("Get Firefox 3.0.1", 150, 10, 100, 20)
$progressDownloadStatus = GUICtrlCreateProgress(5, 40, $GUIWidth - 10, 10)
GUISetState()

#Region Events list
GUISetOnEvent($GUI_EVENT_CLOSE, "_ExitIt")
GUICtrlSetOnEvent($buttonDownload,"_DownloadFF")
#EndRegion Events list

While 1
    Sleep(10)
    If $iDloadFinalSize <> "" Then _CheckActiveDownload()
WEnd

Func _ExitIt()
    Exit
EndFunc

Func _CheckActiveDownload()
    If @InetGetBytesRead <> "-1" Then
        $iDloadCurrentSize = @InetGetBytesRead
        $iPercentage = Int(($iDloadCurrentSize/$iDloadFinalSize)*100)
        GUICtrlSetData($progressDownloadStatus,$iPercentage)
    Else
        $iDloadFinalSize = ""
        GUICtrlSetData($progressDownloadStatus,"0")
        MsgBox(16,"Error","The download was cancelled, or did not complete successfully.")
    EndIf
    If $iDloadCurrentSize = $iDloadFinalSize Then
        $iDloadFinalSize = ""
        MsgBox(64,"Complete","Download complete!")
    EndIf
EndFunc

Func _DownloadFF()
    If @InetGetActive Then
        InetGet("abort")
        GUICtrlSetData($progressDownloadStatus,"0")
    Else
        $sFileURL = "http://mozilla2.mirrors.tds.net/pub/mozilla.org/firefox/releases/3.0.1/win32/en-US/Firefox%20Setup%203.0.1.exe"
        $sFileDest = @ScriptDir & "\Firefox_setup_3.0.1.exe"
        $iDloadFinalSize = InetGetSize($sFileURL)
        $iDloadCurrentSize = "0"
        InetGet($sFileURL, $sFileDest, 1, 1)
    EndIf
EndFunc

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Link to comment
Share on other sites

  • 3 years later...

How would I use this script to say download more than one file... IE: it would download firefox, then once done would automatically start say thunderbird? Restting the gui to reflect the new file? Thanks.

 

Spoiler

WinSizer 2.1 (01/04/2017) - Download - [ Windows Layout Manager ]
Folder+Program (12/23/2016) - Download - [ USB Shortcut Creator ]

 

Link to comment
Share on other sites

Here's an old script I used for multiple downloads. This one has 2 progress bars (one for current file progress and one for progress of all files). It also allows you to cancel and has an option to shutdown when downloads have completed. It's a little more complicated but theres lots of comments.

Just edit the array at the start to have however many elements you need and change the URLs.

NiVZ

#include <StaticConstants.au3>
#include <GUIConstants.au3>
#include <GUIStatusBar.au3>
; 2D array to hold URL and filesize
Dim $arrDownload[4][2]
; Put the number of elements into element [0][0]
$arrDownload[0][0] = UBound($arrDownload)-1
; Setup the files to download
$arrDownload[1][0] = "[url="http://mozilla.ftp.halifax.rwth-aachen.de/mozilla/firefox/releases/11.0/win32/en-GB/Firefox"]http://mozilla.ftp.halifax.rwth-aachen.de/mozilla/firefox/releases/11.0/win32/en-GB/Firefox[/url] Setup 11.0.exe"
$arrDownload[2][0] = "[url="http://mozilla.ftp.halifax.rwth-aachen.de/mozilla/firefox/releases/11.0/win32/en-GB/Firefox"]http://mozilla.ftp.halifax.rwth-aachen.de/mozilla/firefox/releases/11.0/win32/en-GB/Firefox[/url] Setup 11.0.exe"
$arrDownload[3][0] = "[url="http://mozilla.ftp.halifax.rwth-aachen.de/mozilla/firefox/releases/11.0/win32/en-GB/Firefox"]http://mozilla.ftp.halifax.rwth-aachen.de/mozilla/firefox/releases/11.0/win32/en-GB/Firefox[/url] Setup 11.0.exe"
; ## GUI ######
$Form   = GUICreate("Downloader", 400, 200)
$butStart = GUICtrlCreateButton("Start", 150, 15, 100, 30)
$txtCurrent = GUICtrlCreateLabel("Current File Progress:", 30, 55, 100, 20)
$prgCurrent = GUICtrlCreateProgress(30, 75, 300, 10)
$lblCurrent = GUICtrlCreateLabel("0%", 340, 72, 30, 15, $SS_RIGHT)
$txtOverall = GUICtrlCreateLabel("Overall Progress:", 30, 105, 80, 20)
$prgOverall = GUICtrlCreateProgress(30, 125, 300, 10)
$lblOverall = GUICtrlCreateLabel("0%", 340, 122, 30, 15, $SS_RIGHT)
$chkShutdown = GUICtrlCreateCheckbox("Shutdown PC when finished", 130, 150, 175, 20)
$StatusBar = _GUICtrlStatusBar_Create($Form)
_GUICtrlStatusBar_SetText($StatusBar, "Ready")
GUISetState(@SW_SHOW)
; ## END GUI #####

While 1
  
   $nMsg = GUIGetMsg()
  
   If $nMsg = $GUI_EVENT_CLOSE Then Exit
  
   If $nMsg = $butStart Then doDownloads()
  
WEnd

Func doDownloads()
   ; Variables for calculating percentages
   Local $CurrentFileSize   = 0
   Local $TotalFileSize   = 0
   Local $currBytesDownloaded = 0
   Local $overallBytesDownloaded = 0
   Local $TotalBytesDownloaded = 0
   Local $currPercent   = 0
   Local $overallPercent  = 0
   Local $Filename  = ""
   Local $currDownload
   Local $CancelPressed   = False
   ; Change the Start button to Cancel
   GuiCtrlSetData($butStart, "Cancel")
  
   ; Reset all progress data
   GUICtrlSetData($lblOverall, "0%")
   GUICtrlSetData($prgOverall, "0")
   GUICtrlSetData($lblCurrent, "0%")
   GUICtrlSetData($prgCurrent, "0")
  
  
   ; Loop through all the URLs to get all the FileSizes (this allows us to calculate overall progress of ALL downloads)
   For $i = 1 To UBound($arrDownload)-1
  
   ; Get the filename from after the last forward slash
   $FileName = StringRight($arrDownload[$i][0], StringLen($arrDownload[$i][0])-StringInStr($arrDownload[$i][0], "/", -1, -1))
  
   _GUICtrlStatusBar_SetText($StatusBar, 'Getting filesize for file "' & $FileName & '"...')
  
   ; Put the filesize into second column of array
   $CurrentFileSize = INetGetSize($arrDownload[$i][0])
  
   ; We're going to use this filesize twice so put it in a variable so we only actually look it up once
   $arrDownload[$i][1] = $CurrentFileSize
   ; Add this to the total file size
   $TotalFileSize += INetGetSize($arrDownload[$i][0])
  
   Next
  
  
   ; Now do the individual downloads
   For $i = 1 To UBound($arrDownload)-1
     
   ; Reset current bytes
   $currBytesDownloaded = 0
  
   ; Reset the current progress bar
   GUICtrlSetData($lblCurrent, "0%")
   GUICtrlSetData($prgCurrent, "0")
  
   ; Get the filename from after the last forward slash
   $Filename = StringRight($arrDownload[$i][0], StringLen($arrDownload[$i][0])-StringInStr($arrDownload[$i][0], "/", -1, -1))
  
   _GUICtrlStatusBar_SetText($StatusBar, 'Downloading "' & $Filename & '" (' & $i & '/' & UBound($arrDownload)-1 & ')...')
 
   ; Start the download
   $currDownload = INetGet($arrDownload[$i][0], @DesktopDir & "" & $Filename, 0, 1)
  
   Do
   ; Check for messages
   Local $dMsg = GUIGetMsg()
  
   ; If the press the 'Cancel button
   If $dMsg = $butStart Then
    
   ; Check they really want to cancel
   Local $Confirm_Cancel = MsgBox(4, "Downloader", "Do You Want To Cancel All Downloads?", 0, $Form)
   
   ; If they do want to cancel
   If $Confirm_Cancel = 6 Then
     
      ; stop the download
    INetClose($currDownload)
    
    ; notify the outer loop to exit
    $CancelPressed = True
      ExitLoop
   EndIf
   
   EndIf
  
   ; Get Current Bytes downloaded
   $currBytesDownloaded  = INetGetInfo($currDownload, 0)
  
   ; Add current bytes downloaded to the total filesize of completed downloads
   $overallBytesDownloaded = $TotalBytesDownloaded + $CurrBytesDownloaded
  
   ; Get the current percentages
   $currPercent   = ($currBytesDownloaded / $arrDownload[$i][1]) * 100
   $overallPercent  = ($overallBytesDownloaded / $TotalFileSize) * 100
  
   ; Update the current progress bar
   GUICtrlSetData($prgCurrent, $currPercent)
   GUICtrlSetData($prgOverall, $overallPercent)
  
   ; Update the progress labels only if they have changed (to avoid flicker)
   If GUICtrlRead($lblCurrent) <> floor($currPercent) & "%" Then GUICtrlSetData($lblCurrent, Floor($currPercent) & "%")
   If GUICtrlRead($lblOverall) <> floor($overallPercent) & "%" Then
   GUICtrlSetData($lblOverall, floor($overallPercent) & "%")
   WinSetTitle($Form, "", floor($overallPercent) & "% - Downloader")
   EndIf
  
   Until INetGetInfo($currDownload, 2)
  
   If Not $CancelPressed Then
   ; Set current progress to 100% here (sometimes gets stuck at 99 with rounding)
   GUICtrlSetData($prgCurrent, 100)
   GUICtrlSetData($lblCurrent, "100%")
   EndIf
   ; Add the filesize of this completed download to the total bytes downloaded
   $TotalBytesDownloaded += $arrDownload[$i][1]
  
   ; User cancelled - don't download anything else
   If $CancelPressed Then ExitLoop
     
   Next
  
   ; Reset the GUI labels
   _GUICtrlStatusBar_SetText($StatusBar, "Ready")
   GUICtrlSetData($butStart, "Start")
   If $CancelPressed Then
   ; User cancelled
   MsgBox(48, "Downloader", "All downloads cancelled", 0, $Form)
   Else
      ; everything finished downloading
 
   ; Set current progress to 100% here (sometimes gets stuck at 99 with rounding)
   GUICtrlSetData($prgCurrent, 100)
   GUICtrlSetData($lblCurrent, "100%")
 
   ; Check if user wants us to shutdown
   If GUICtrlRead($chkShutdown) = $GUI_CHECKED Then
   ; Shutdown the PC if box is ticked and exit program
   Shutdown(29)
   Exit
   Else
   MsgBox(64, "Downloader", "All downloads complete", 0, $Form)
   EndIf
  
   EndIf
   ; Reset the window title
   WinSetTitle($Form, "", "Downloader")
EndFunc
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...