STEPBYSTEEP
MIS
I am using the solution given by 1DMF in the below thread:
The problem is that, when the last chunk of the file is read, unused parts of the buffer is keeping the previous values and write it in the new file ending with some repeated data. I have done some modifications but I cant get across the files with identical size and content (last part is some blank content and file is usually larger)
I don't want to use the shell commands because I have no control on when the files are finishing the ftp transfer.
Any suggestion?
Copied just the retrieval code...
The problem is that, when the last chunk of the file is read, unused parts of the buffer is keeping the previous values and write it in the new file ending with some repeated data. I have done some modifications but I cant get across the files with identical size and content (last part is some blank content and file is usually larger)
I don't want to use the shell commands because I have no control on when the files are finishing the ftp transfer.
Any suggestion?
Copied just the retrieval code...
Code:
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 As Integer = 1, Optional ByRef iTot As Integer = 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
Dim LastChunk() As Byte ' to capture the last chunk
' 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
' Initialise 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 remaining chunk using MOD if exists
If iSize Mod BUFFER_SIZE > 0 Then
' Update progress meter
Retval = SysCmd(acSysCmdUpdateMeter, iSize / 1000)
'Empty the array to prevent polution of the file with data from previous loop
'Erase FileData
ReDim LastChunk((iSize Mod BUFFER_SIZE) - 1)
' Write remainder to file checking for success
'If InternetReadFile(hFile, FileData(0), iSize Mod BUFFER_SIZE, iRead) = 0 Then
If InternetReadFile(hFile, LastChunk(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, , LastChunk
'Put iFile, , FileData
End If
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:
GoTo Exit_Function
End Function