How to retrieve info about all files and directories(sub directories and files and so on) present in a remote directory

0 votes
asked May 10, 2012 by srinu (460 points)
edited Jun 21, 2012

Hi,

I want to get info about all directories and sub directories and files in those directories and sub directories from a remote folder

Thanks

Applies to: Rebex FTP/SSL

4 Answers

0 votes
answered May 11, 2012 by Tomas Knopp (58,890 points)
edited Jun 11, 2012

Hello, you can retrieve info about all items from the remote directory "dir" (including all files, subdirectories, subdirectories of subdirectories and so on) with the GetItems method as follows:

        Ftp ftp = new Ftp();
        // connect and login

        FtpItemCollection items = ftp.GetItems("/remotepath/dir/*", TraversalMode.Recursive);
        foreach (var item in items) // iterate through the collection
            Console.WriteLine("{0} {1} {2}Bytes", item.Path, item.Modified, item.Length); //use Properites to access the attributes.

        ftp.Disconnect();

The GetItems method can be used also with Sftp class, then it returns SftpItemCollection.

commented May 11, 2012 by srinu (460 points)
edited May 11, 2012

Hi,

Thanks for your replay. but i don't want download functionality.

Actually my requirement is to know how many folders and sufolders and also how many files in root dir before downloading functinality.

Example remote dir structure :

dir-A --> contains dir-B and file-1 dir-B --> contains dir-C and file-2

and so on

i want all these details.

Thanks & Regards Srinu

commented May 11, 2012 by Tomas Knopp (58,890 points)
edited May 11, 2012

Hello, I just updated my original answer above.

commented May 11, 2012 by srinu (460 points)
edited May 11, 2012

Hi,

Thanks for your guidence

commented May 11, 2012 by Tomas Knopp (58,890 points)
edited May 11, 2012

You're welcome.

0 votes
answered Jun 21, 2012 by smoore4 (180 points)
edited Jun 21, 2012

I have a related question. Is it possible to "combine" item collections so that the item collection is made up from two different GetItems() calls, like the example below. The item list ends up being just the last call, and I understand why. Looking for away around it. Thanks.

Steve

SftpItemCollection list = client.GetItems(
    new Rebex.IO.FileSet("/Volumes/DAM_RAID_1/Internal_Repository/", "*",
                            Rebex.IO.TraversalMode.Recursive));

list = client.GetItems(
    new Rebex.IO.FileSet("/Volumes/DAM_RAID_4/repository2/", "*",
                            Rebex.IO.TraversalMode.Recursive));

Something like this seems to work...

            SftpItemCollection list0 = client.GetItems(new Rebex.IO.FileSet("/Volumes/DAM_RAID_1/Internal_Repository/", "*", Rebex.IO.TraversalMode.Recursive));

            SftpItemCollection list1 = client.GetItems(new Rebex.IO.FileSet("/Volumes/DAM_RAID_4/repository2/", "*", Rebex.IO.TraversalMode.Recursive));
            foreach (SftpItem item in list1) { list0.Add(item); };

But I am not sure it is the best way (obviously no error handling).

0 votes
answered Jun 21, 2012 by Lukas Matyska (55,430 points)
edited Jun 21, 2012

Copying items from one collection to another as you shown in the second example is correct solution and moreover it is the most speed optimal solution.

You can achieve the similar result with the single GetItems call like this:

// prepare FileSet
Rebex.IO.FileSet set = new FileSet("/Volumes/");
set.Include("DAM_RAID_1/Internal_Repository/");
set.Include("DAM_RAID_4/repository2/");

// use the FileSet to retrieve all wanted items
SftpItemCollection list = client.GetItems(set);

This leads to less speed effective solution, because the base-path for GetItems is "/Volumes/" so more server directories are listed.

0 votes
answered Jun 21, 2012 by smoore4 (180 points)
edited Jun 21, 2012

Thank you!

This tool is the best thing going. It gives me a way to script against Mac and Unix servers using .NET. Pretty sweet!

...