Recursive folders exploration?

Dec 15, 2009 at 10:14 PM

Hi!

Is it possible to get a complete list of files stored on the FTP server with this library?

I think a recursive method that explore all folders on the FTP server.

Thanks in advance for your answers ;-).

++
PlaTyPuS 

Coordinator
Dec 16, 2009 at 1:31 AM
Edited Dec 16, 2009 at 4:42 PM

Of course :-)

here's a simple example:

 

using AlexPilotti.FTPS.Client;
using AlexPilotti.FTPS.Common;

...

    private static void TraversePath()
    {
        try
        {
            using (FTPSClient c = new FTPSClient())
            {
		// Set the SSL settings as needed, here's a plain FTP sample
                c.Connect("ftp.yoursite.com", ESSLSupportMode.ClearText);
                TraversePath(c, "");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    private static void TraversePath(FTPSClient c, string path)
    {
        IList<DirectoryListItem> l = c.GetDirectoryList();

        foreach (DirectoryListItem i in l)
            Console.WriteLine(path + "/" + i.Name);

        foreach (DirectoryListItem i in l)
            if (i.IsDirectory)
            {
		c.PushCurrentDirectory();
                c.SetCurrentDirectory(i.Name);
                TraversePath(c, path + "/" + i.Name);
		c.PopCurrentDirectory();
            }
    }

Alessandro Pilotti

[MVP / IIS]

 

 

 

Dec 16, 2009 at 11:20 AM
Edited Dec 16, 2009 at 11:21 AM

Hi Alessandro! Thanks for your fast reply :-).

I wrote more or less the same code as you, but it doesn't work :-(.

I have the following files/folders structure on my FTP:

/New Rich Text Document.rtf
/New Text Document.txt
/Test
/Test Inc
     blablabla.rtf
/Test - Copy
   blobloblo.rtf 

And OUTPUT in Console is as follow:

/New Rich Text Document.rtf
/New Text Document.txt
/Test
/Test - Copy
/Test/Test Inc
/Test/Test Inc/blablabla.rtf
/Test/Test Inc/Test - Copy: No such file or directory.

 "Test - Copy" is at root level of my ftp server.... I don't understand why it tries to find the "Test - Copy" folder under "/Test/Test Inc/" ??? (I also try with the code you gave me, but the result is the same!)

Thanks for you help!

PlaTyPuS

Coordinator
Dec 16, 2009 at 4:44 PM

Hi,

sorry, I forgot to paste the code for going back to the previous directory during path traversal. I edited the previous post to add it. :-)

Let me know if it works for you!

 

Cheers,

Alessandro

[MVP / IIS ]

Dec 16, 2009 at 7:49 PM

Wowww nice, it works :-))).

!!! THANKS !!!

Jan 6, 2010 at 3:28 PM

re ;-)

I was wondering if there's a way to explore the entire FTP Server much faster??

With the method exposed before, it takes me about 4 hours to explore a huge FTP Server (2TO, with a lot of directories).....

Thx, +++

Coordinator
Jan 6, 2010 at 3:39 PM

You could gain a little bit of performance by avoiding the PushCurrentDirectory / PopCurrentDirectory pairs, but keep in mind that ftp is a verbose protocol.

Most of the time is spent in issuing commands and awaiting for the response. Furthermore, any time you issue a data command (including directory listings), a separate data connection needs to be enstablished, which is very time consuming!

 

That said, you might gain a *lot* of performance by using threading: explore different paths in parallel. Note that some servers refuse concurrent connections from the same client.

 

Cheers,

Alessandro Pilotti

[ MVP / IIS ]