Jump to content

Auto Download UDF


AcidCorps
 Share

Recommended Posts

This is a little Download function I made for myself, it has a lot to it but the only variable that is not optional is the $sDownload everything else it automatically detected

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.5.0 (beta)
 Author:         Acid Corps
 Title:          Auto Download

 Script Function:
    Simplifies Downloading
    
    $sDownload = Link to file to download
                     Format http://www.example.com/file.exe
                     
    $sFilename = Name to save download file as (optional)
                     Default: Original name from download link
                     
    $sSize     = Size of file to download (optional)
                     Format: 100 or 100 KB or 100 MB or 100 GB
                     If not stated attempts to use InetGetSize (does not always work)
                     
    $sFolder   = Folder to save file to (optional)
                     Default: Script Directory
                     Format: C:\My Folder
                     
    $sProxy    = Proxy to connect to the net (optional)
                     Default: No Proxy
                     Format: Proxy:Port
                     1 = Default Proxy
                     
    $sName     = Username for download (optional)
                     For use with ftp and some http downloads
                     
    $sPass     = Password for download (optional)
                     For use with ftp and some http downloads
                     
    $sProgress = Progress bar on or off (optional)
                     Default: On
                     
    If you do not wish to use ,"", to omit variables then use ,0, and default will be used
    
    Usage: _Download($sDownload, $sFilename, $sSize, $sFolder, $sProxy, $sName, $sPass, $sProgress)

#ce ----------------------------------------------------------------------------

Func _Download($sDownload, $sFilename=0, $sSize=0, $sFolder=0, $sProxy=0, $sName=0, $sPass=0, $sProgress=0)
Opt("ExpandEnvStrings", 1)

;   Setting Proxy
    If $sProxy = 1 Then 
        HttpSetProxy(0)
        FtpSetProxy(0)
    EndIf
    
    If $sProxy <> 0 And $sProxy <> 1 And $sName <> 0 And $sPass <> 0 Then 
        HttpSetProxy(2, $sProxy, $sName, $sPass)
        FtpSetProxy(2, $sProxy, $sName, $sPass)
    EndIf
    
;  Setting Size
    If $sSize = 0 Then
        $ssSize = InetGetSize($sDownload)
    Else
        $ssSize = $sSize
    EndIf
    
    If StringRight($sSize, 2) = "kb" Then $sSizeTimes = 1024
    If StringRight($sSize, 2) = "mb" Then $sSizeTimes = 1048576
    If StringRight($sSize, 2) = "gb" Then $sSizeTimes = 1073741824
    
;   Finding Filename
    If $sFilename = 0 Then
        $ssFilename = StringTrimLeft($sDownload,StringInStr($sDownload,"/",1,-1))
    Else
        $ssFileName = $sFileName
    EndIf
    
;   Progress/Download
    If $sProgress = 0 then
         Dim $ssSize = InetGetSize($sDownload)
         INetGet($sDownload,$ssFilename)
         ProgressOn("Downloading...", "Retrieving new version.")
                 While @InetGetActive
                 Dim $p = (100 * @InetGetBytesRead) / $ssSize
                 ProgressSet($p, @InetGetBytesRead & "/" & $ssSize & " bytes", "Download in progress.")
             Sleep(250)
         ProgressOff()
     WEnd
     Else
         InetGet($sDownload, $ssFilename)

     EndIf
EndFunc

Edit: Updated script with mrRevoked's method of finding the filename

_Download.au3

Edited by AcidCorps
Link to comment
Share on other sites

If StringRight($sSize, 2) = "kb" Then $sSizeTimes = 1024

If StringRight($sSize, 2) = "mb" Then $sSizeTimes = 1048576

If StringRight($sSize, 2) = "gb" Then $sSizeTimes = 1073741824

What does this do? And why?

Finding Filename

If $sFilename = 0 Then

$sFNArray = StringSplit($sDownload, "/")

$sFNArrayAmount = $sFNArray[0]

$ssFileName = $sFNArray[$sFNArrayAmount]

This is a shorter method:

StringTrimLeft($sDownload,StringInStr($sDownload,"/",1,-1))

greetings

Pascal

Link to comment
Share on other sites

What does this do? And why?

It lets you use kb, mb, and gb, abbreviations in the $sSize

This is a shorter method:

StringTrimLeft($sDownload,StringInStr($sDownload,"/",1,-1))

Updated

Edited by AcidCorps
Link to comment
Share on other sites

_Download("http://www.autoitscript.com/cgi-bin/getfile.pl?autoit3/autoit-v3.2.4.9-setup.exe","Autoit.exe","4.24 MB", @ScriptDir, 0, "AcidCorps", "AcidPass", 0)oÝ÷ Ùj.ÖÞÂ+ajYb­°z0z÷«².Ö¶Ø^±©jÆ®¶­seôF÷væÆöBgV÷C¶GG¢ò÷wwræWFöG67&6öÒö6vÖ&âövWFfÆRçÃöWFöC2öWFöB×c2ã"ãBã×6WGWæWRgV÷C²

Edit:missed closing autoit tag

Edited by AcidCorps
Link to comment
Share on other sites

_Download("http://www.autoitscript.com/cgi-bin/getfile.pl?autoit3/autoit-v3.2.4.9-setup.exe","Autoit.exe","4.24 MB", @ScriptDir, 0, "AcidCorps", "AcidPass", 0)

Would be with all inputs

However should act the same as 

[code]_Download("http://www.autoitscript.com/cgi-bin/getfile.pl?autoit3/autoit-v3.2.4.9-setup.exe")

ok no progress bar..

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...