Hi guys, I needed a simple function to download files and show the progress of the download. I commented a bit (don't know if i should have added more) You can provide the url to the file, the wanted file name, the visible name for in the download progress UI, where the file should be downloaded to, if the progressbar should be hidden at the end of the function or if it should remain visible, how long the last message (completed/failed) should show and last but not least, a title for the progress window. At the end of the function you either get the full path of the download file or false when it fails with @error set and @extended set with the error code of the download. #include <InetConstants.au3>
Func _webDownloader($sSourceURL, $sTargetName, $sVisibleName, $sTargetDir = @TempDir, $bProgressOff = True, $iEndMsgTime = 2000, $sDownloaderTitle = "MyDownloader")
; Declare some general vars
Local $iMBbytes = 1048576
; If the target directory doesn't exist -> create the dir
If Not FileExists($sTargetDir) Then DirCreate($sTargetDir)
; Get download and target info
Local $sTargetPath = $sTargetDir & "\" & $sTargetName
Local $iFileSize = InetGetSize($sSourceURL)
Local $hFileDownload = InetGet($sSourceURL, $sTargetPath, $INET_LOCALCACHE, $INET_DOWNLOADBACKGROUND)
; Show progress UI
ProgressOn($sDownloaderTitle, "Downloading " & $sVisibleName)
; Keep checking until download completed
Do
Sleep(250)
; Set vars
Local $iDLPercentage = Round(InetGetInfo($hFileDownload, $INET_DOWNLOADREAD) * 100 / $iFileSize, 0)
Local $iDLBytes = Round(InetGetInfo($hFileDownload, $INET_DOWNLOADREAD) / $iMBbytes, 2)
Local $iDLTotalBytes = Round($iFileSize / $iMBbytes, 2)
; Update progress UI
If IsNumber($iDLBytes) And $iDLBytes >= 0 Then
ProgressSet($iDLPercentage, $iDLPercentage & "% - Downloaded " & $iDLBytes & " MB of " & $iDLTotalBytes & " MB")
Else
ProgressSet(0, "Downloading '" & $sVisibleName & "'")
EndIf
Until InetGetInfo($hFileDownload, $INET_DOWNLOADCOMPLETE)
; If the download was successfull, return the target location
If InetGetInfo($hFileDownload, $INET_DOWNLOADSUCCESS) Then
ProgressSet(100, "Downloading '" & $sVisibleName & "' completed")
If $bProgressOff Then
Sleep($iEndMsgTime)
ProgressOff()
EndIf
Return $sTargetPath
; If the download failed, set @error and return False
Else
Local $errorCode = InetGetInfo($hFileDownload, $INET_DOWNLOADERROR)
ProgressSet(0, "Downloading '" & $sVisibleName & "' failed." & @CRLF & "Error code: " & $errorCode)
If $bProgressOff Then
Sleep($iEndMsgTime)
ProgressOff()
EndIf
SetError(1, $errorCode, False)
EndIf
EndFunc ;==>_webDownloaderLet me know what you think The test in my example is done with the installer for java 8 update 65 Example usage : $url = "http://javadl.sun.com/webapps/download/AutoDL?BundleId=111687"
$file = "Java_8_Update_65.exe"
$name = "Java 8 Update 65"
$dir = @TempDir & "\MyDownloader"
$installcommand = " /s STATIC=Disable AUTO_UPDATE=Disable WEB_JAVA=Enable WEB_JAVA_SECURITY_LEVEL=H WEB_ANALYTICS=Disable EULA=Enable REBOOT=Disable SPONSORS=Disable"
$test = _webDownloader($url, $file, $name, $dir, False)
If $test Then
ProgressSet(100, "Running silent installation...", "Installing " & $name)
$exitCode = RunWait($test & $installcommand)
If $exitCode = 0 Then ProgressSet(100, "Installation completed")
If $exitCode <> 0 Then ProgressSet(0, "Installation failed" & @CRLF & "Exit code: " & $exitCode)
Sleep(3000)
ProgressOff()
FileDelete($test)
Else
ProgressOff()
EndIfGreetz
_webDownloader.au3