ftp list all dir and related file in
-
- PlatinumLounger
- Posts: 4334
- Joined: 26 Apr 2010, 17:36
ftp list all dir and related file in
I just have name of ftp, username and password.
I need to enumerate alls dir and related files from the ftp server
Tks
I need to enumerate alls dir and related files from the ftp server
Tks
-
- 4StarLounger
- Posts: 535
- Joined: 27 Jun 2021, 10:46
Re: ftp list all dir and related file in
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
-
- PlatinumLounger
- Posts: 4334
- Joined: 26 Apr 2010, 17:36
Re: ftp list all dir and related file in
i bro, tks for code.SpeakEasy wrote: ↑19 Feb 2023, 12:02Here'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
But a lot of mistakes...
can i sent in pvt the username and password and ftp name?
-
- 4StarLounger
- Posts: 535
- Joined: 27 Jun 2021, 10:46
-
- PlatinumLounger
- Posts: 4334
- Joined: 26 Apr 2010, 17:36
-
- PlatinumLounger
- Posts: 4334
- Joined: 26 Apr 2010, 17:36
-
- 4StarLounger
- Posts: 535
- Joined: 27 Jun 2021, 10:46
Re: ftp list all dir and related file in
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
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
-
- PlatinumLounger
- Posts: 4334
- Joined: 26 Apr 2010, 17:36
Re: ftp list all dir and related file in
with this tips, the code work great!SpeakEasy wrote: ↑20 Feb 2023, 10:48Ah. 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
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
-
- 4StarLounger
- Posts: 535
- Joined: 27 Jun 2021, 10:46
Re: ftp list all dir and related file in
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,
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.
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
You'll need to add a lot of logic to handle e.g timeouts and other errors, restricted access, and long loops.
-
- PlatinumLounger
- Posts: 4334
- Joined: 26 Apr 2010, 17:36
Re: ftp list all dir and related file in
bro tks for code.SpeakEasy wrote: ↑20 Feb 2023, 19:19I'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,
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)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
You'll need to add a lot of logic to handle e.g timeouts and other errors, restricted access, and long loops.
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?
-
- 4StarLounger
- Posts: 535
- Joined: 27 Jun 2021, 10:46
Re: ftp list all dir and related file in
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
after
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
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
Code: Select all
WalkDirTree ftproot
You might also, if you have not already done so, set the style to something appropriate for expanding and collapsing, such as tvwTreelinesPlusMinusText
-
- 4StarLounger
- Posts: 535
- Joined: 27 Jun 2021, 10:46
Re: ftp list all dir and related file in
Did you get this working?
-
- PlatinumLounger
- Posts: 4334
- Joined: 26 Apr 2010, 17:36
-
- 4StarLounger
- Posts: 535
- Joined: 27 Jun 2021, 10:46
Re: ftp list all dir and related file in
Still busy?