; 
;=====================================================================================----- 
;Function       FTPget() - Deprecated in ITCG Coding - see URL() for full functionality 
; 
;Action         gets file from ftp or http server and saves it in specified file 
; 
;Author         Lonkero & Kent Dyer 
; 
;Version        1.3 - 2015/09/23 
; 
;History        1.1 - 2002/12/06 - Initial Release 
;               1.2 - 2007/08/02 - Added Force-parameter 
;               1.3 - 2015/09/23 - Rewrite to accommodate Win-8/Server 2012 
;                                  Returns nothing if filespec is specified, or 
;                                  FTP/HTTP content as string if filespec is null. 
; 
;Syntax 	FTPget(URL, TARGET, USERNAME, PASSWORD, FORCE) 
; 
;Parameters  
;               URL - Required - String 
;               - full url to the file to get 
;               TARGET - Required - String 
;               - full path to file where to save. If empty, the data 
;                 is returned in a string. 
;               USERNAME - Optional - String 
;               - specifies username to use in connection 
;               PASSWORD - Optional - String 
;               - specifies password to use in connection 
;               FORCE - Optional - String 
;               - force udf to bypass cache 
; 
;Returns        Nothing if file is specified, otherwise downloaded data as string 
;               On error errorcodes set: 
;                1 - failed to initialize http-object 
;                2 - failed to initialize ADODB-object* 
;                3 - failed to open connection 
;                4 - ADO write failed* 
;                5 - save to file failed* 
;                * These results are only valid when an output file is written 
; 
;Dependencies   IE5 or higher 
; 
;Remarks        if file exists, it will be overwritten. 
; 
;Examples	 
;               "Downloading TypelibViewer, Please standby..." 
;               FTPget("http://www.rwksystems.com/files/TypeLibViewer.exe","%temp%\typelibViewer.exe") 
;               If @ERROR 
;                 "error occured:" @error " / " @SERROR ? 
;               Else 
;                 "Download complete. file is saved in %temp% as typelibViewer.exe" ? 
;               EndIf 
; 
;               ; Download a text or html file directly into a variable 
;               $HTTP_Body = FTPget("http://www.innotechcg.com/downloads/filelist.txt", "", "", "", 1) 
; 
; 
Function FTPget($_sURL, $_sTargetFile, optional $_sUser, $_sPass, $_iForce)
 
  Dim $_oFTP				; COM Object - FTP connection 
  Dim $_oStream				; COM Object - output stream 
  Dim $_dwSM				; original cache reference value 
  Dim $_				; Temp var 
  Dim $_sLoc				; Reg key 
 
  $FtpGet = ''
 
  ; Get the original cache value and replace it with the Force Bypass value 
  $_sLoc = 'HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings'
  $_sUser = '' + $_sUser
  If $_iForce
    $_dwSM = ReadValue($_sLoc, 'Syncmode5')
    $_ = WriteValue($_sLoc, 'Syncmode5', 3, 'REG_DWORD')
  EndIf
 
  ; Instantiate the FTP connection object 
  If @DOS < 6.1
    $_oFTP = CreateObject('Microsoft.XMLHTTP')
  Else
    $_oFTP = CreateObject('MSXML2.ServerXMLHTTP')
  EndIF
  If @ERROR
    If $_iForce
      $_ = WriteValue($_sLoc, 'Syncmode5', $_dwSM, 'REG_DWORD')
    EndIf
    Exit 1
  EndIf
 
  ; If a target file is defined, create the output stream object 
  If $_sTargetFile
    $_oStream = CreateObject('ADODB.Stream')
    If @ERROR
      If $_iForce
        $_ = writevalue($_sLoc, 'Syncmode5', $_dwSM, 'REG_DWORD')
      EndIf
      Exit 2
    EndIf
  EndIf
 
  ; Open the connection, with or without credentials as appropriate. 
  If $_sUser
    $_oFTP.Open('GET', $_sURL, not 1, $_sUser, $_sPass)
  Else
    $_oFTP.Open('GET', $_sURL, not 1)
  EndIf
  If @ERROR
    If $_iForce
      $_ = writevalue($_sLoc, 'Syncmode5', $_dwSM, 'REG_DWORD')
    EndIf
    Exit 3
  EndIf
 
  $_oFTP.Send
 
  If $_sTargetFile
    $_oStream.Type = 1
    $_oStream.Mode = 3
    $_oStream.Open
    $_oStream.Write($_oFTP.responseBody)
    If @ERROR
      If $_iForce
        $_ = writevalue($_sLoc, 'Syncmode5', $_dwSM, 'REG_DWORD')
      EndIf
      Exit 4
    EndIf
 
    ; Write the returned data to the file 
    $_oStream.SaveToFile($_sTargetFile, 2)
    If @ERROR
      If $_iForce
        $_ = writevalue($_sLoc, 'Syncmode5', $_dwSM, 'REG_DWORD')
      EndIf
      $_oStream.Close
      Exit 5
    EndIf
    $_oStream.Close
  Else
    ; Return the data as a string 
    $FTPGet = $_oFTP.responseBody
  EndIf
 
  $_oFTP = 0
 
  If $_iForce
    $_ = writevalue($_sLoc, 'Syncmode5', $_dwSM, 'REG_DWORD')
  EndIf
 
  Exit 0
 
EndFunction