Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA to get list of files on ftp server 3

Status
Not open for further replies.

DevelopV

Technical User
Mar 16, 2012
113
ZA
In Access 2010 I need to get a list of all files in a specific directory on an ftp server and download them to my hard drive.
I never know how many files, or their file names, there are on the ftp server at time of download.

What is the best way to accomplish this?

Many thanks
 
FTPGet gets the file but the file size is 0.
The file is created in the local folder but it is empty!
 
>It is almost as though the files names on the first run are kept in cache

They are.
 
How do I clear the cache so that I get the true list of files?
 
Ah well, it isn't a cache that the shell can directly control. For example, on my setup using IIS6 as the FTP server there is not problem with the list. It updates correctly, whether I add or delete files from the FTP folder through Explorer on the server in question, or through IIS . It does however take a few seconds for the FTP server to correctly update itself and thus provide the correct info back to the client software.
 
when attempting to transfer multiple files I get the following error:

"An error occurred copying a file to the FTP server. Make sure that you have permission to put file on the server.
Details: The system cannot find the file specified"

However if I step through code and wait for the copying to finish the error does not appear and the files are copied.

How can I pause the code and wait for the transfer to be completed?
 
in your global module
Code:
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Then any where in VBA you can use
Code:
' sleep 1 second
Sleep 1000

But not an ideal solution pausing code trying to guess the time it might take before something finishes and is able to continue!

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Ah well, it isn't a cache that the shell can directly control. For example, on my setup using IIS6 as the FTP server there is not problem with the list. It updates correctly, whether I add or delete files from the FTP folder through Explorer on the server in question, or through IIS . It does however take a few seconds for the FTP server to correctly update itself and thus provide the correct info back to the client software.

After half an hour I still could not get the correct file list.
This is a BIG problem
 
1DMF, did you manage to update your version for both Get & Put?

strongm solutions is very, very neat, but has two flaws:
* I can't update the file list on the ftp site without restarting the database
* I can't upload multiple files using a loop

 
>strongm solutions is very, very neat, but has two flaws:
* I can't update the file list on the ftp site without restarting the database
* I can't upload multiple files using a loop

I'd love to see your exact code.
 
To Get File list:
Code:
Public Sub GetFileList()

Dim myFolderItem As FolderItem
Dim myFiles As String

myFiles = ""
For Each myFolderItem In ftpList("[URL unfurl="true"]www.mydomain.com/public_html/tim/webexport/",[/URL] pubInternetFTPUserName, pubInternetFTPPassword)  
    myFiles = myFiles & myFolderItem.Name & ";"
Next

Me.lstAvialableFilesForDownload.RowSource = myFiles
End Sub

To Upload Files:
Code:
Private Sub cmdUploadFiles_Click()

Dim FileToCopy As String
Dim FileToCopyFrom As String
Dim FileToCopyTo As String
Dim lstCount As Integer

For varItem = 0 To Me.lstAvialableFilesForUpload.ListCount - 1
    FileToCopy = Me.lstAvialableFilesForUpload.Column(0, varItem)
    FileToCopyFrom = pubWebOrderUploadDataFromLocalFolder & "\"
    FTPPut FileToCopyFrom, pubInternetDomainName & "/" & pubWebOrderUploadDataToInternetFolder & "/", FileToCopy, pubInternetFTPUserName, pubInternetFTPPassword
    Kill FileToCopyFrom & "\" & FileToCopy
Next

End Sub
 
Code:
Public Sub FTPPut(strSourcePath As String, strDestPath As String, strFilename As String, Optional strUser As String, Optional strPassword As String)

Dim strFTPConnect As String

If strUser <> "" Then strFTPConnect = strUser & ":" & strPassword & "@"
strDestPath = "FTP://" & strFTPConnect & strDestPath
FTPCopy strSourcePath, strDestPath, strFilename, strUser, strPassword

End Sub

Code:
Public Sub FTPCopy(strSourcePath As String, strDestPath As String, strFilename As String, Optional strUser As String, Optional strPassword As String)

Dim SourceFolder As Shell32.Folder
Dim DestFolder As Shell32.Folder
Dim myShell As New Shell32.Shell

Set SourceFolder = myShell.Namespace(strSourcePath)
Set DestFolder = myShell.Namespace(strDestPath)
DestFolder.CopyHere SourceFolder.Items.item(strFilename)

End Sub
 
Code:
Public Function ftpList(strFTPLocation As String, Optional strUser As String, Optional strPassword As String) As FolderItems
' Returns a FolderItems collection from the FTP server

'Works. Bug in that list is not updated after 1st rub. Database needs to be restarted

Dim myShell As Shell
Dim strConnect As String

Set myShell = New Shell
If strUser <> "" Then strConnect = strUser & ":" & strPassword & "@"
Set ftpList = myShell.Namespace("FTP://" & strConnect & strFTPLocation).Items   '("ftp://user:password@ftp.site.com")

End Function
 
1DMF, did you manage to update your version for both Get & Put?
Yes and I posted above (18 Feb 13 4:38) and I highlighted where the changes were made and I also included the counter (x of x) optional parameters for both routines.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
1DMF

Many, many thanks
I can know get files uploaded, downloaded, file list etc

I can't get the progress meter on downloads to work. I am populating the Optional ByRef iCnt As Integer = 1, Optional ByRef iTot As Integer = 1 in the function
 
No probs, glad you found it usefull.

How big is the file?

Do you see it flash so quick the green bar doesn't move?

Try the function with a large file 10mb for example up and down, does it appear then?



"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top