R
RichLaver
Guest
Dear Friends
I am attempting to download data from an FTP server using VB; however the cFileName attribute of the WIN32_FIND_DATA structure returned by calling the FtpFindFirstFile function is returning only the final few characters of the filename. This prevents me from using the FtpGetFile function to download the file.
The number of characters it returns is anywhere from two to eight. For instance, if the filename is FILENAME.XLSX, the cFileName attribute — after trimming nulls — might give SX or AME.XLSX
I have already spent quite a few weeks trying to crack this, including an extensive online search and trialling using different computers but to no avail. I am using a 64-bit Windows 10 Pro operating system.
I have copied the simplest version of the code below, with the connection details obscured for sensitivity. Would anyone be able to help me please?
Best
Rich
Const MAX_PATH = 260
'
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
'
Private Type WIN32_FIND_DATA
dwFileAttributes As LongPtr
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As LongPtr
nFileSizeLow As LongPtr
dwReserved0 As LongPtr
dwReserved1 As LongPtr
cFileName As String * MAX_PATH
cAlternateFileName As String * 14
End Type
'
Private Declare PtrSafe Function InternetOpen Lib "WININET.DLL" Alias "InternetOpenA" _
(ByVal sAgent As String, _
ByVal lAccessType As LongPtr, _
ByVal sProxyName As String, _
ByVal sProxyBypass As String, _
ByVal lFlags As LongPtr) As LongPtr
'
Private Declare PtrSafe Function InternetConnect Lib "WININET.DLL" Alias "InternetConnectA" _
(ByVal hInternetSession As LongPtr, _
ByVal sServerName As String, _
ByVal nServerPort As Integer, _
ByVal sUsername As String, _
ByVal sPassword As String, _
ByVal lService As LongPtr, _
ByVal lFlags As LongPtr, _
ByVal lContext As LongPtr) As LongPtr
'
Private Declare PtrSafe Function FtpSetCurrentDirectory Lib "WININET.DLL" Alias "FtpSetCurrentDirectoryA" _
(ByVal hFtpSession As LongPtr, _
ByVal lpszDirectory As String) As Boolean
'
Private Declare PtrSafe Function FtpFindFirstFile Lib "WININET.DLL" Alias "FtpFindFirstFileA" _
(ByVal hFtpSession As LongPtr, _
ByVal lpszSearchFile As String, _
lpFindFileData As WIN32_FIND_DATA, _
ByVal dwFlags As LongPtr, _
ByVal dwContent As LongPtr) As LongPtr
'
Private Declare PtrSafe Function InternetCloseHandle Lib "WININET.DLL" _
(ByVal hInet As LongPtr) As Integer
'
'
Sub Test()
'
hostName = "ftpaddress"
port = 21
username = "username"
password = "password"
remoteDirectory = "directory/"
remoteMatchFiles = "*.xls*"
'
Dim fileFind As WIN32_FIND_DATA
'
hOpen = InternetOpen("ftp VBA", 1, vbNullString, vbNullString, 0)
Debug.Print hOpen
'
If hOpen > 0 Then hConn = InternetConnect(hOpen, hostName, port, username, password, 1, 0, 0)
Debug.Print hConn
'
If hConn > 0 Then hFind = FtpFindFirstFile(hConn, remoteDirectory & remoteMatchFiles, fileFind, 0, 0)
Debug.Print hFind
Debug.Print fileFind.cFileName
'
InternetCloseHandle hConn
InternetCloseHandle hOpen
'
End Sub
Continue reading...
I am attempting to download data from an FTP server using VB; however the cFileName attribute of the WIN32_FIND_DATA structure returned by calling the FtpFindFirstFile function is returning only the final few characters of the filename. This prevents me from using the FtpGetFile function to download the file.
The number of characters it returns is anywhere from two to eight. For instance, if the filename is FILENAME.XLSX, the cFileName attribute — after trimming nulls — might give SX or AME.XLSX
I have already spent quite a few weeks trying to crack this, including an extensive online search and trialling using different computers but to no avail. I am using a 64-bit Windows 10 Pro operating system.
I have copied the simplest version of the code below, with the connection details obscured for sensitivity. Would anyone be able to help me please?
Best
Rich
Const MAX_PATH = 260
'
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
'
Private Type WIN32_FIND_DATA
dwFileAttributes As LongPtr
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As LongPtr
nFileSizeLow As LongPtr
dwReserved0 As LongPtr
dwReserved1 As LongPtr
cFileName As String * MAX_PATH
cAlternateFileName As String * 14
End Type
'
Private Declare PtrSafe Function InternetOpen Lib "WININET.DLL" Alias "InternetOpenA" _
(ByVal sAgent As String, _
ByVal lAccessType As LongPtr, _
ByVal sProxyName As String, _
ByVal sProxyBypass As String, _
ByVal lFlags As LongPtr) As LongPtr
'
Private Declare PtrSafe Function InternetConnect Lib "WININET.DLL" Alias "InternetConnectA" _
(ByVal hInternetSession As LongPtr, _
ByVal sServerName As String, _
ByVal nServerPort As Integer, _
ByVal sUsername As String, _
ByVal sPassword As String, _
ByVal lService As LongPtr, _
ByVal lFlags As LongPtr, _
ByVal lContext As LongPtr) As LongPtr
'
Private Declare PtrSafe Function FtpSetCurrentDirectory Lib "WININET.DLL" Alias "FtpSetCurrentDirectoryA" _
(ByVal hFtpSession As LongPtr, _
ByVal lpszDirectory As String) As Boolean
'
Private Declare PtrSafe Function FtpFindFirstFile Lib "WININET.DLL" Alias "FtpFindFirstFileA" _
(ByVal hFtpSession As LongPtr, _
ByVal lpszSearchFile As String, _
lpFindFileData As WIN32_FIND_DATA, _
ByVal dwFlags As LongPtr, _
ByVal dwContent As LongPtr) As LongPtr
'
Private Declare PtrSafe Function InternetCloseHandle Lib "WININET.DLL" _
(ByVal hInet As LongPtr) As Integer
'
'
Sub Test()
'
hostName = "ftpaddress"
port = 21
username = "username"
password = "password"
remoteDirectory = "directory/"
remoteMatchFiles = "*.xls*"
'
Dim fileFind As WIN32_FIND_DATA
'
hOpen = InternetOpen("ftp VBA", 1, vbNullString, vbNullString, 0)
Debug.Print hOpen
'
If hOpen > 0 Then hConn = InternetConnect(hOpen, hostName, port, username, password, 1, 0, 0)
Debug.Print hConn
'
If hConn > 0 Then hFind = FtpFindFirstFile(hConn, remoteDirectory & remoteMatchFiles, fileFind, 0, 0)
Debug.Print hFind
Debug.Print fileFind.cFileName
'
InternetCloseHandle hConn
InternetCloseHandle hOpen
'
End Sub
Continue reading...