And an Example using a GUI. It's slightly different to >> Also this is an example which of course can be improved upon! I hope it gives you inspiration to look into creating GUI's Function: ; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
; #INDEX# =======================================================================================================================
; Title .........: _InetGetGUI
; AutoIt Version : v3.3.2.0 or higher
; Language ......: English
; Description ...: Download a file updating a GUICtrlCreateProgress.
; Note ..........:
; Author(s) .....: guinness
; Remarks .......:
; ===============================================================================================================================
; #CURRENT# =====================================================================================================================
; _InetGetGUI: Download a file updating a GUICtrlCreateProgress.
; ===============================================================================================================================
; #INTERNAL_USE_ONLY#============================================================================================================
; _ByteSuffix ......; Convert bytes to the largest measure. Thanks to Spliff69 - http://www.autoitscript.com/forum/topic/...ync-tool/page__view__findpost_
; ===============================================================================================================================
; #FUNCTION# =========================================================================================================
; Name...........: _InetGetGUI()
; Description ...: Download a file updating a GUICtrlCreateProgress().
; Syntax.........: _InetGetGUI($sURL, $iLabel, $iProgress, $iButton, [$sDirectory = @ScriptDir])
; $sURL - A valid URL that contains the filename too.
; $iLabel - ControlID of a GUICtrlCreateLabel.
; $iProgress - ControlID of a GUICtrlCreateProgress.
; $iButton - ControlID of a GUICtrlCreateButton.
; $sDirectory - [Optional] Directory of where to download to. Default = @ScriptDir
; Parameters ....: None
; Requirement(s).: v3.3.2.0 or higher
; Return values .: Success - Downloaded filename.
; Failure - Returns downloaded filename & sets @error = 1 (@extended = 0 if internal error or @extended = 1 if cancelled was selected.)
; Author ........: guinness
; Example........; Yes
;=====================================================================================================================
Func _InetGetGUI($sURL, $iLabel, $iProgress, $iButton, $sDirectory = @ScriptDir)
Local $hDownload, $iBytesRead = 0, $iError = 0, $iFileSize, $iPercentage, $iSpeed = 0, $iTimer = 0, $sFilePath, $sProgressText, $sRead = GUICtrlRead($iButton), $sSpeed
$sFilePath = StringRegExpReplace($sURL, "^.*/", "")
If @error Then
Return SetError(1, 0, $sFilePath)
EndIf
$sDirectory = StringRegExpReplace($sDirectory, "[\\/]+\z", "") & "\" & $sFilePath
$iFileSize = InetGetSize($sURL, 1)
$hDownload = InetGet($sURL, $sDirectory, 0, 1)
If @error Then
Return SetError(1, $iError, $sFilePath)
EndIf
GUICtrlSetData($iButton, "&Cancel")
$sSpeed = "Current Speed: " & _ByteSuffix($iBytesRead - $iSpeed) & "/s"
$iTimer = TimerInit()
While InetGetInfo($hDownload, 2) = 0
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE, $iButton
GUICtrlSetData($iLabel, "Download Cancelled!")
$iError = 1
ExitLoop
EndSwitch
$iBytesRead = InetGetInfo($hDownload, 0)
$iPercentage = $iBytesRead * 100 / $iFileSize
$sProgressText = "Downloading " & _ByteSuffix($iBytesRead, 0) & " Of " & _ByteSuffix($iFileSize, 0) & @LF & $sSpeed
GUICtrlSetData($iLabel, $sProgressText)
GUICtrlSetData($iProgress, $iPercentage)
If TimerDiff($iTimer) > 1000 Then
$sSpeed = "Current Speed: " & _ByteSuffix($iBytesRead - $iSpeed) & "/s"
$iSpeed = $iBytesRead
$iTimer = TimerInit()
EndIf
Sleep(100)
WEnd
InetClose($hDownload)
GUICtrlSetData($iButton, $sRead)
Return SetError($iError, $iError, $sFilePath)
EndFunc ;==>_InetGetGUI
; #INTERNAL_USE_ONLY#============================================================================================================
Func _ByteSuffix($iBytes, $iRound = 2)
Local $A, $aArray[9] = [" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"]
While $iBytes > 1023
$A += 1
$iBytes /= 1024
WEnd
Return Round($iBytes, $iRound) & $aArray[$A]
EndFunc ;==>_ByteSuffix
; #INTERNAL_USE_ONLY#============================================================================================================Example use of Function: #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#include <GUIConstantsEx.au3>
_Main()
Func _Main()
Local $sFilePathURL = "http://ftp.opera.com/pub/opera/win/1152/en/Opera_1152_en_Setup.exe"
Local $hGUI, $iButton, $iLabel, $iProgressBar, $sFilePath
$hGUI = GUICreate("_InetGetGUI()", 370, 90, -1, -1)
$iLabel = GUICtrlCreateLabel("Welcome to the simple Downloader!", 5, 5, 270, 40)
$iButton = GUICtrlCreateButton("&Download", 275, 2.5, 90, 25)
$iProgressBar = GUICtrlCreateProgress(5, 60, 360, 20)
GUISetState(@SW_SHOW, $hGUI)
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
Exit
Case $iButton
$sFilePath = _InetGetGUI($sFilePathURL, $iLabel, $iProgressBar, $iButton, @ScriptDir)
If @error Then
Switch @extended ; Check what the actual error was by using the @extended command.
Case 0
MsgBox(64, "Error", "Check the URL or your Internet Connection!")
Case 1
MsgBox(64, "Fail", "Seems the download was canecelled, but the file was >> " & $sFilePath)
EndSwitch
Else
MsgBox(64, "Success", "Downloaded >> " & $sFilePath)
EndIf
EndSwitch
WEnd
EndFunc ;==>_Main