Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
4.9k views
in .NET FTP by
Hello, does anyone have a code sample of recursively scanning an entire ftp directory tree and listing it to text or to a treeview (ideally)?

Thanks
vip.

5 Answers

0 votes
by
in VB.NET?
0 votes
by (162k points)
edtFTPnet/PRO's next release will have recursive directory utilities included with it - so far recursive put, get and delete.

To recursively list a directory, what format would you expect the listing be in, i.e. how do you want different levels of directories represented?

in VB.NET?
0 votes
by
Sorry to step into someone else's thread, but for myself...

Probably the easiest/best thing would be if the current ftpFile class had an extra field: parent directory. The current DirDetails would return an array of ftpFile for the selected directory, with each ftpFile indicating the PWD that the file was found in. A new command, or a new parameter for DirDetails, for recursive listing would likewise return an array of ftpFiles. The only difference would be that the ftpFile objects would have potentially different PWD entries represented. My own code could then interpret this list however I want it to be interpreted.
0 votes
by (162k points)
Recursive dirs are now implemented in edtFTPnet/PRO.

It is not that difficult to implement yourself using the current FTPFile, but if you want a cheap, debugged option that means you don't have to write it yourself, edtFTPnet/PRO might suit you.

Sorry to step into someone else's thread, but for myself...

Probably the easiest/best thing would be if the current ftpFile class had an extra field: parent directory. The current DirDetails would return an array of ftpFile for the selected directory, with each ftpFile indicating the PWD that the file was found in. A new command, or a new parameter for DirDetails, for recursive listing would likewise return an array of ftpFiles. The only difference would be that the ftpFile objects would have potentially different PWD entries represented. My own code could then interpret this list however I want it to be interpreted.
0 votes
by
Just FYI... I had to write something like that for an app of mine. If the user wants to empty the root upload folder (i.e. get rid of old files) before re-uploading new ones, I had to figure out a way to recursively delete all files/folders in the root. Here's what I came up with:

I just pass ClearFTP the FTPClient object, and the root folder that I want to empty... it will loop through all subfolders and empty them, then delete the subfolder. (if you want to empty files that are in the root folder as well, just do the same type of DirDetails loop before calling ClearFTP) It then passes back a string containing any errors that occured while it was trying to delete.

    Private Shared Function ClearFTP(ByRef ftp As FTPClient, ByVal sFldr As String) As String
        Dim fl, fld() As FTPFile, sErr As String, i As Integer
        Try
            fld = ftp.DirDetails(sFldr)
            For i = 0 To UBound(fld)
                If fld(i).Dir And (Not fld(i).Name = "." And Not fld(i).Name = "..") Then
                    For Each fl In ftp.DirDetails(sFldr & "/" & fld(i).Name)
                        Try
                            If Not fl.Dir Then ftp.Delete(sFldr & "/" & fld(i).Name & "/" & fl.Name)
                        Catch ex As Exception
                            sErr &= "Could not delete: " & sFldr & "/" & fld(i).Name & "/" & fl.Name & ". Details: " & ex.Message & vbCrLf
                        End Try
                    Next
                    sErr &= ClearFTP(ftp, sFldr & "/" & fld(i).Name)
                    Try
                        ftp.RmDir(sFldr & "/" & fld(i).Name)
                    Catch
                    End Try
                End If
            Next
            fld = Nothing
            ClearFTP = sErr
        Catch ex As Exception
            ClearFTP = sErr & "An error occured while trying to delete from " & sFldr & ". Details: " & ex.Message & vbCrLf
        End Try
    End Function


This works great except for a bug in DirDetails which doesn't always list all the files in a folder (not sure why). I would use Dir (which correctly lists the files every time), but then I can't differentiate between what's a folder and what's a file.



WATYF

Categories

...