Function FTPGet(ByVal HostName As String, _
ByVal Username As String, _
ByVal Password As String, _
ByVal LocalFileName As String, _
ByVal RemoteFileName As String, _
ByVal sDir As String, _
ByVal sMode As String, Optional ByRef iCnt = 1, Optional ByRef iTot = 1) As Boolean
On Error GoTo Err_Function
' Declare variables
Dim hConnection, hOpen, hFile As Long ' Used For Handles
Dim iSize As Long ' Size of file for download
Dim iMaxSize As Long
Dim Retval As Variant ' Used for progress meter
Dim iRead As Long ' Used by InternetReadFile to report bytes downloaded
Dim iLoop As Long ' Loop for downloading chunks
Dim iFile As Integer ' Used for Local file handle
Dim FileData(BUFFER_SIZE - 1) As Byte ' buffer array of BUFFER_SIZE (100) elements 0 to 99
' Open Internet Connecion
hOpen = InternetOpen("FTP", 1, "", vbNullString, 0)
' Connect to FTP
hConnection = InternetConnect(hOpen, HostName, INTERNET_DEFAULT_FTP_PORT, Username, Password, INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
' Change Directory
Call FtpSetCurrentDirectory(hConnection, sDir)
' Open Remote File
hFile = FtpOpenFile(hConnection, RemoteFileName, GENERIC_READ, IIf(sMode = "Binary", FTP_TRANSFER_TYPE_BINARY, FTP_TRANSFER_TYPE_ASCII), 0)
' Check for successfull file handle
If hFile = 0 Then
MsgBox "Internet - Failed!"
ShowError
FTPGet = False
GoTo Exit_Function
End If
' Set Download Flag to True
FTPGet = True
' Set file size
iSize = FtpGetFileSize(hFile, iMaxSize)
' Get next file handle number
iFile = FreeFile
' Open local file
Open LocalFileName For Binary Access Write As iFile
' Iinitialise progress meter
Retval = SysCmd(acSysCmdInitMeter, "Downloading File '" & RemoteFileName & "' - " & iCnt & " of " & iTot, iSize / 1000)
' Loop file size
For iLoop = 1 To iSize \ BUFFER_SIZE
' Update progress meter
Retval = SysCmd(acSysCmdUpdateMeter, (BUFFER_SIZE * iLoop) / 1000)
' Read chunk from FTP checking for success
If InternetReadFile(hFile, FileData(0), BUFFER_SIZE, iRead) = 0 Then
MsgBox "Download - Failed!"
ShowError
FTPGet = False
GoTo Exit_Function
Else
' Check buffer was read
If iRead <> BUFFER_SIZE Then
MsgBox "Download - Failed!"
ShowError
FTPGet = False
GoTo Exit_Function
End If
End If
'put file data
Put iFile, , FileData
Next iLoop
' Handle remainder using MOD
' Update progress meter
Retval = SysCmd(acSysCmdUpdateMeter, iSize / 1000)
' Write remainder to file checking for success
If InternetReadFile(hFile, FileData(0), iSize Mod BUFFER_SIZE, iRead) = 0 Then
MsgBox "Download - Failed!"
ShowError
FTPGet = False
GoTo Exit_Function
Else
' Check buffer was read
If iRead <> iSize Mod BUFFER_SIZE Then
MsgBox "download - Failed!"
ShowError
FTPGet = False
GoTo Exit_Function
End If
End If
' Put file data
Put iFile, , FileData
Exit_Function:
' remove progress meter
Retval = SysCmd(acSysCmdRemoveMeter)
'close local file
Close iFile
'close remote file
Call InternetCloseHandle(hFile)
' Close Internet Connection
Call InternetCloseHandle(hOpen)
Call InternetCloseHandle(hConnection)
Exit Function
Err_Function:
MsgBox "Error in FTPGet : " & err.Description
GoTo Exit_Function
End Function