FTPGetFile strange problem

  • Thread starter Thread starter Luis
  • Start date Start date
L

Luis

Hello.
I'm trying to use FTPGetFile to download some files from an FTP location,
but i run into a strange problem.
The function works for one ftp location but if i try to use the same funtion
to another location, the script creates the files in the target directory,
but the files are empty. Can anyone give me a hint on this?

The code i'm using is the following:


Option Compare Database

Global Const FTP_TRANSFER_TYPE_ASCII = &H1
Global Const FTP_TRANSFER_TYPE_BINARY = &H2
Global Const INTERNET_DEFAULT_FTP_PORT = 21
Global Const INTERNET_SERVICE_FTP = 1
Global Const INTERNET_FLAG_PASSIVE = &H8000000
Global Const GENERIC_WRITE = &H40000000
Global Const BUFFER_SIZE = 100
Global Const PassiveConnection As Boolean = True
Global Const MAX_PATH As Long = 260

' Declare wininet.dll API Functions
Public Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias _
"FtpSetCurrentDirectoryA" _
(ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean

Public Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias _
"FtpGetCurrentDirectoryA" _
(ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, _
lpdwCurrentDirectory As Long) As Boolean

Public Declare Function InternetWriteFile Lib "wininet.dll" _
(ByVal hFile As Long, ByRef sBuffer As Byte, ByVal lNumBytesToWite As Long, _
dwNumberOfBytesWritten As Long) As Integer

Public Declare Function FtpOpenFile Lib "wininet.dll" Alias "FtpOpenFileA" _
(ByVal hFtpSession As Long, ByVal sBuff As String, ByVal Access As Long, _
ByVal Flags As Long, ByVal Context As Long) As Long

Public Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" _
(ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _
ByVal lpszRemoteFile As String, _
ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean

Public Declare Function FtpDeleteFile Lib "wininet.dll" _
Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, _
ByVal lpszFileName As String) As Boolean
Public Declare Function InternetCloseHandle Lib "wininet.dll" _
(ByVal hInet As Long) As Long

Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As
String, _
ByVal sProxyBypass As String, ByVal lFlags As Long) As Long

Public Declare Function InternetConnect Lib "wininet.dll" Alias
"InternetConnectA" _
(ByVal hInternetSession As Long, ByVal sServerName As String, ByVal
nServerPort As Integer, _
ByVal sUsername As String, ByVal sPassword As String, ByVal lService As
Long, _
ByVal lFlags As Long, ByVal lContext As Long) As Long


Public Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" _
(ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal
dwFlagsAndAttributes As Long, _
ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean

Declare Function InternetGetLastResponseInfo Lib "wininet.dll" _
Alias "InternetGetLastResponseInfoA" _
(ByRef lpdwError As Long, _
ByVal lpszErrorBuffer As String, _
ByRef lpdwErrorBufferLength As Long) As Boolean

Declare Function FtpFindFirstFile Lib "wininet" _
Alias "FtpFindFirstFileA" _
(ByVal hConnect As Long, _
ByVal lpszSearchFile As String, _
lpFindFileData As Any, _
ByVal dwFlags As Long, _
ByVal dwContext As Long) As Long

Public Declare Function InternetFindNextFile Lib "wininet" _
Alias "InternetFindNextFileA" _
(ByVal hFind As Long, _
lpFindFileData As WIN32_FIND_DATA) As Long

Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type


Public Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type

Sub ftpFiles()
Dim hConnection, hCurDir, hDir, hFile, hOpen As Variant
Dim WFD As WIN32_FIND_DATA

Const hostname = "MyFTPHost"
Const path = "/ACIE/ACIE_BLexport/ACIE_BSS11export_Dir1/"
Const username = "myUser"
Const password = "myPwd"

hOpen = InternetOpen("FTP", 1, "", vbNullString, 0)
hConnection = InternetConnect(hOpen, hostname,
INTERNET_DEFAULT_FTP_PORT, username, password, INTERNET_SERVICE_FTP,
IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
hFile = FtpFindFirstFile(hConnection, path, WFD,
INTERNET_FLAG_RELOAD Or INTERNET_FLAG_NO_CACHE_WRITE, 0&)
If hFile Then

Do
tmp = Left(WFD.cFileName, InStr(WFD.cFileName, Chr(0)))
If Len(tmp) Then
k = FtpGetFile(hConnection, tmp, "g:\Temp\" & tmp, False,
1, FTP_TRANSFER_TYPE_BINARY, 1)
End If
Loop While InternetFindNextFile(hFile, WFD)

End If
InternetCloseHandle (hFile)
InternetCloseHandle (hOpen)
InternetCloseHandle (hConnection)
End Sub
 
Back
Top