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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get sub-folders from FTP 1

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,529
US
Code taken from thread705-1703957, thanks strongm :)

Code:
Option Explicit[green]
' Requires references to Microsoft Shell Controls and Automation[/green]
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") [green]'Each item could be a folder or a file[/green]
        Debug.Print myFolderItem.Name, myFolderItem.IsFolder [green]' just for fun to illustrate stuff we can do[/green]
        
        If myFolderItem.IsFolder Then[green]
            'Get Sub-Folders and/or files in this folder[/green]
            
        End If
    Next
End Sub
[green]
' Returns a FolderItems collection from the FTP server[/green]
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 [green]'("ftp://user:password@ftp.site.com")[/green]

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
 
Here's, have a version of my FTP recursive folder walk:

Code:
[COLOR=blue]Public Sub AndyExample()
    WalkFTP "ftp.gnu.org" [COLOR=green]', "username", "password"[/color]
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 & "@"
    
    [COLOR=green]' get root ftp folder we will start walk from[/color]
    Set ftproot = myShell.Namespace("FTP://" & strConnect & strFTPFolder)
    
    [COLOR=green]' Walk directory tree from this root[/color]
    WalkDirTree ftproot

End Function

[COLOR=green]' recursive walk through shell folders. In this case these folders are FTP folders[/color]
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 [COLOR=green]' You can do what you like with this info[/color]
        If myFolderItem.IsFolder Then
            level = level + 1
            WalkDirTree myFolderItem.GetFolder [COLOR=green]' recurse[/color]
            level = level - 1
        End If
    Next
End Sub[/color]

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/"

 
Great! [thumbsup2]
That's a lot better than what I came up with.

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
>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
 
Sorry, my fault.
I forgot to pass [tt]strConnect[/tt] in:
[tt]Set ftproot = myShell.Namespace("FTP://" & strConnect & strFTPFolder)[/tt]
[banghead]

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top