ftp list all dir and related file in

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

ftp list all dir and related file in

Post by sal21 »

I just have name of ftp, username and password.

I need to enumerate alls dir and related files from the ftp server

Tks

User avatar
SpeakEasy
4StarLounger
Posts: 535
Joined: 27 Jun 2021, 10:46

Re: ftp list all dir and related file in

Post by SpeakEasy »

Here's one way

Code: Select all

Public Sub Sal21Example()
    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 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

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: ftp list all dir and related file in

Post by sal21 »

SpeakEasy wrote:
19 Feb 2023, 12:02
Here's one way

Code: Select all

Public Sub Sal21Example()
    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 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
i bro, tks for code.
But a lot of mistakes...
can i sent in pvt the username and password and ftp name?

User avatar
SpeakEasy
4StarLounger
Posts: 535
Joined: 27 Jun 2021, 10:46

Re: ftp list all dir and related file in

Post by SpeakEasy »

Sure

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: ftp list all dir and related file in

Post by sal21 »

SpeakEasy wrote:
19 Feb 2023, 19:03
Sure
I sent tomorrow.
Sorry for miy inglish

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: ftp list all dir and related file in

Post by sal21 »

SpeakEasy wrote:
19 Feb 2023, 19:03
Sure
sent now in pvt

User avatar
SpeakEasy
4StarLounger
Posts: 535
Joined: 27 Jun 2021, 10:46

Re: ftp list all dir and related file in

Post by SpeakEasy »

Ah. Ok .. so, something you need to know: "'@" is a special character in FTP URLs, so you need to escape it if you happen to use it in a username or password. That's fairly easily done manually by replacing "@" with "%40"

eg

myusername@somewhere.org => myusername%40somewhere.org

and

MySecretP@ssword => MySecretP%40ssword

So why don't you start with that, and see if your errors disappear

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: ftp list all dir and related file in

Post by sal21 »

SpeakEasy wrote:
20 Feb 2023, 10:48
Ah. Ok .. so, something you need to know: "'@" is a special character in FTP URLs, so you need to escape it if you happen to use it in a username or password. That's fairly easily done manually by replacing "@" with "%40"

eg

myusername@somewhere.org => myusername%40somewhere.org

and

MySecretP@ssword => MySecretP%40ssword

So why don't you start with that, and see if your errors disappear
with this tips, the code work great!

but in other case the code go in infinity loop!!!

hummm.

possible to insert dir and sub dir and related file name into a treeview?

similar:

.. rooth

dir
fileAname
fileBname

... ecc

subdir (if exists)

file1name
file2name

.... ecc

User avatar
SpeakEasy
4StarLounger
Posts: 535
Joined: 27 Jun 2021, 10:46

Re: ftp list all dir and related file in

Post by SpeakEasy »

I'll provide a basic version - which differs very little from the original code (which I had assumed you'd be able top modify yourself for your purposes). It assumes Form1 with Treeview1,

Code: Select all

Option Explicit

Public Sub Sal21Example()
    WalkFTP "ftp.gnu.org/pub/non-gnu" ', "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)
    
    ' Add root FTP folder to Treeview as root
     Form1.TreeView1.Nodes.Add , tvwFirst, ftproot.Self.Path, ftproot.Title
    
    'Walk directory tree from root
    WalkDirTree ftproot

End Function

' recursive walk through 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
        ' For example ... add to Treeview
        Form1.TreeView1.Nodes.Add myFolderItem.Parent.Self.Path, tvwChild, myFolderItem.Path, myFolderItem.Name
        If myFolderItem.IsFolder Then
            level = level + 1
            WalkDirTree myFolderItem.GetFolder ' recurse
            level = level - 1
        End If
    Next
End Sub
Note that the FTP site you are trying to access has three complete websites in the root. FTP is relatively slow getting a folder listing, which makes walking the entire folder structure of multiple large websites really slow. (it's why Filezilla only does each folder on demand)


You'll need to add a lot of logic to handle e.g timeouts and other errors, restricted access, and long loops.

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: ftp list all dir and related file in

Post by sal21 »

SpeakEasy wrote:
20 Feb 2023, 19:19
I'll provide a basic version - which differs very little from the original code (which I had assumed you'd be able top modify yourself for your purposes). It assumes Form1 with Treeview1,

Code: Select all

Option Explicit

Public Sub Sal21Example()
    WalkFTP "ftp.gnu.org/pub/non-gnu" ', "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)
    
    ' Add root FTP folder to Treeview as root
     Form1.TreeView1.Nodes.Add , tvwFirst, ftproot.Self.Path, ftproot.Title
    
    'Walk directory tree from root
    WalkDirTree ftproot

End Function

' recursive walk through 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
        ' For example ... add to Treeview
        Form1.TreeView1.Nodes.Add myFolderItem.Parent.Self.Path, tvwChild, myFolderItem.Path, myFolderItem.Name
        If myFolderItem.IsFolder Then
            level = level + 1
            WalkDirTree myFolderItem.GetFolder ' recurse
            level = level - 1
        End If
    Next
End Sub
Note that the FTP site you are trying to access has three complete websites in the root. FTP is relatively slow getting a folder listing, which makes walking the entire folder structure of multiple large websites really slow. (it's why Filezilla only does each folder on demand)


You'll need to add a lot of logic to handle e.g timeouts and other errors, restricted access, and long loops.
bro tks for code.
But the treeview have only a name of ftp in root!

Can ypu send in pvt a little project tested with the account, posted in my pvt?

User avatar
SpeakEasy
4StarLounger
Posts: 535
Joined: 27 Jun 2021, 10:46

Re: ftp list all dir and related file in

Post by SpeakEasy »

I'd rather keep answers to forum questions in public, where possible.

Didn't realise that you didn't know how the TreeView control actually works. By default Nodes are NOT expanded. You have to expand them yourself, either in code, or by clicking the nodes.

Here's one way of expanding all the nodes

Just add

Code: Select all

    Dim expandme As Node
    Form1.TreeView1.Visible = False 'for performance with large treeviews
    For Each expandme In Form1.TreeView1.Nodes
        expandme.Expanded = True ' note: a bit inefficient as we futilely expand childless nodes as well as one that do have children
    Next
    Form1.TreeView1.Visible = True
after

Code: Select all

    WalkDirTree ftproot
in the WalkDirTree function

You might also, if you have not already done so, set the style to something appropriate for expanding and collapsing, such as tvwTreelinesPlusMinusText

User avatar
SpeakEasy
4StarLounger
Posts: 535
Joined: 27 Jun 2021, 10:46

Re: ftp list all dir and related file in

Post by SpeakEasy »

Did you get this working?

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: ftp list all dir and related file in

Post by sal21 »

SpeakEasy wrote:
24 Feb 2023, 12:29
Did you get this working?
Not tested.,.
Im busy.
Sorry bro.

User avatar
SpeakEasy
4StarLounger
Posts: 535
Joined: 27 Jun 2021, 10:46

Re: ftp list all dir and related file in

Post by SpeakEasy »

Still busy?