Loop through files on FTP location

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

Luis

Hello.
I'm using InetTransferLib to download some files from an FTP location.

Until now i usually spcify the source and target filename. From now on the
number of files of that FTP location can vary and also their names.

How can I loop through the files of that location and read their filenames
to use as source and target files?

Thanks.

Luis
 
Luis said:
Hello.
I'm using InetTransferLib to download some files from an FTP location.

Until now i usually spcify the source and target filename. From now on the
number of files of that FTP location can vary and also their names.

How can I loop through the files of that location and read their filenames
to use as source and target files?

Thanks.

Luis

Take a look at:

http://www.smccall.demon.co.uk/Downloads.htm#FTPClient

It uses wininet.dll directly, as does the InetTransferLib (I believe). If
you need help with anything, please post to this thread.
 
Hi Stuart.
After taking a look at the MDB you provide, i have a couple of questions.
I've looked at "btnDownload_Click()" function, and this code reads the local
and remote filename from a form (frmUpDownTest).

What i need is that the filename will be set looping through all the files
of the FTP location. How can i do this?
The other question is that this form is disconnected. Is there a form in the
database that calls the other forms, where we connect to a location?
 
Hi Luis

I had a similar requirement and struggled to find a solution.

In the end I used an FTP client ActiveX control (Evans FTP) from Evans
Programming which offers the functionality you need (plus a lot more!).

Have a look at www.evansprogramming.com

Cheers.

BW
 
Luis said:
Hi Stuart.
After taking a look at the MDB you provide, i have a couple of questions.
I've looked at "btnDownload_Click()" function, and this code reads the
local
and remote filename from a form (frmUpDownTest).

What i need is that the filename will be set looping through all the files
of the FTP location. How can i do this?
The other question is that this form is disconnected. Is there a form in
the
database that calls the other forms, where we connect to a location?

Take a look at the form frmFtpRecurse. That does exactly what you need. If
you take a squint at the form's code you'll find that:

There's a particular required declaration:
Private WithEvents ftp As FTPClient

That's the connection to the FTPClient class, which is the bit that does the
work. Once this is declared, you can drop down the code window's left hand
combo and select "ftp". You can then select an event from the right hand
combo. The event you'll be most interested in is "ftp_FileFound". Do
whatever you want to the file passed through the FileName parameter in this
event procedure (for example purposes I just add the file path to a
textbox).

Also, this is a "connected" form as you require.

Play around with that, perhaps make your own form and "crib" from mine. Any
more problems, don't hesitate...
 
You can download a trial demo version that includes all the control itself,
example applications, documentation etc. from the site below.
 
Hello Luis,

I find an example on

http://vbnet.mvps.org/index.html?code/internet/ftplist.htm

where a extracted the code you need which you can find below.

grtz

*********************************************************

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 Function SQLDataSources Lib "ODBC32.DLL" ( _
ByVal henv As Long, ByVal fDirection As Integer, _
ByVal szDSN As String, ByVal cbDSNMax As Integer, _
pcbDSN As Integer, ByVal szDescription As String, _
ByVal cbDescriptionMax As Integer, pcbDescription As Integer _
) As Integer

' 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 ftpForLuis()
Dim hConnection, hCurDir, hDir, hFile, hOpen As Variant
Dim WFD As WIN32_FIND_DATA

Const hostname = "www.domain.org"
Const path = "/yourpath/yoursubpath/"
Const username = "yourusername"
Const password = "yourpassword"

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)

hDir = FtpSetCurrentDirectory(hConnection,
"/tbr-umcu/www/cobra2000/downloads/")

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
If WFD.dwFileAttributes And vbDirectory Then
MsgBox (tmp) ' List1.AddItem tmp & sSlash
Else
MsgBox (tmp) ' List1.AddItem tmp
End If
End If


'continue while valid
Loop While InternetFindNextFile(hFile, WFD)

End If

InternetCloseHandle (hFile)
InternetCloseHandle (hOpen)
InternetCloseHandle (hConnection)

End Sub
 
Thank you all for your help.
I'm trying to use this method but i'm having some dificulties using
FTPGetFile method. Can you give me a hint ?

By the way Stuart, I'm sure going to use your database on another tasks i
have. That a really great application. Congratulations and thank you.
 
Luis said:
Thank you all for your help.
I'm trying to use this method but i'm having some dificulties using
FTPGetFile method. Can you give me a hint ?

By the way Stuart, I'm sure going to use your database on another tasks i
have. That a really great application. Congratulations and thank you.

Thanks for your kind words. Re the FTPGetFile method, take a look at this
site:

http://www.example-code.com/vb/ftp.asp

specifically, the "simple ftp download" page. Plenty of easy-to-understand
examples here.

You can find an example of FTPGetFile use in my FTPClient class module, in
the GetFile procedure.
 
Please ignore my previous. That site's code examples are for use with an
ActiveX that they sell.
 
Back
Top