×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Get sub-folders from FTP

Get sub-folders from FTP

Get sub-folders from FTP

(OP)
Code taken from thread705-1703957: VBA to get list of files on ftp server, thanks strongm smile

CODE

Option Explicit
' Requires references to Microsoft Shell Controls and Automation
Public Sub Daniel()
    Dim myFolderItem As FolderItem
    Dim localfolder As Folder
    Dim myShell As New Shell
    
    For Each myFolderItem In ftpList("123.546.543.98/", "SomeUser", "MyPasswd") 'Each item could be a folder or a file
        Debug.Print myFolderItem.Name, myFolderItem.IsFolder ' just for fun to illustrate stuff we can do
        
        If myFolderItem.IsFolder Then
            'Get Sub-Folders and/or files in this folder
            
        End If
    Next
End Sub

' Returns a FolderItems collection from the FTP server
Private Function ftpList(strFTPlocation As String, Optional strUser As String, Optional strPassword As String) As FolderItems
    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 

That code does give me a list of Folders of the 'root', but how can I get deeper into each of these Folders to get the Sub-Folders / files in there?

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

RE: Get sub-folders from FTP

Here's, have a version of my FTP recursive folder walk:

CODE -->

Public Sub AndyExample()
    WalkFTP "ftp.gnu.org" ', "username", "password"
End Sub

Private Function WalkFTP(strFTPFolder As String, Optional strUser As String, Optional strPassword As String) As Boolean
    Dim myShell As New Shell
    Dim ftproot As Folder
    Dim strConnect As String
    
    If strUser <> "" Then strConnect = strUser & ":" & strPassword & "@"
    
    ' get root ftp folder we will start walk from
    Set ftproot = myShell.Namespace("FTP://" & strConnect & strFTPFolder)
    
    ' Walk directory tree from this root
    WalkDirTree ftproot

End Function

' recursive walk through shell folders. In this case these folders are FTP folders
Private Sub WalkDirTree(CurrentLevel As Folder)
    Static level As Long
        Dim myFolderItem As FolderItem
        For Each myFolderItem In CurrentLevel.Items 'Each item could be a folder or a file
        Debug.Print String(level, vbTab); myFolderItem.Name, myFolderItem.IsFolder, myFolderItem.Path ' You can do what you like with this info
        If myFolderItem.IsFolder Then
            level = level + 1
            WalkDirTree myFolderItem.GetFolder ' recurse
            level = level - 1
        End If
    Next
End Sub 

You can target the start point by setting strFTPFolder - this does not have to be the FTP root, in can be anywhere in the hierarchy ...

e.g

WalkFTP "ftp.gnu.org/pub/video/"

RE: Get sub-folders from FTP

(OP)
Great! thumbsup2
That's a lot better than what I came up with.

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

RE: Get sub-folders from FTP

>ftproot points to "002 on 123.546.543.98"

No, it doesn't. That's just a (derived) folder display name, not the path, and is the default property for a shell Folder object. Try examining ftproot.self.path

RE: Get sub-folders from FTP

(OP)
Sorry, my fault.
I forgot to pass strConnect in:
Set ftproot = myShell.Namespace("FTP://" & strConnect & strFTPFolder)
banghead

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close