FtpList item.Size

0 votes
asked Jan 6, 2014 by Tim (390 points)
edited Jan 7, 2014

If I calculate the item.Size for all items in a FtpList will that give me the correct size of all files and sub-folders in the root folder path, or do I need to iterate all of the sub-folders to get the proper size for a total download size?

Applies to: Rebex FTP/SSL

1 Answer

+1 vote
answered Jan 7, 2014 by Jan Sotola (17,310 points)
edited Jan 7, 2014
 
Best answer

It depends on how do you obtain the FtpList collection:

  • The Ftp.GetList method always returns items just from the current directory.
  • The Ftp.GetItems method retrieves the whole directory hierarchy (including subfolders) by default.
    Note: Even here you can enforce browsing just the current directory by calling:
    Ftp.GetItems(path, TraversalMode.NonRecursive).

Note: You even don't have to calculate the item.Size of all items - there is a method FtpList.GetTotalSize() to do it for you.

So to obtain the correct size of all files in the root folder and all its sub-folders, use the following code:

var lst = ftp.GetItems("/*");
Console.WriteLine("Total size: {0}", lst.GetTotalSize());

The same code works as well for the SFTP component.

commented Jan 7, 2014 by Tim (390 points)
edited Jan 7, 2014

Thanks Jan. that sure does make life easier. So if I wanted to do a specific folder and all of its sub-folders would I need to write, ftp.GetItems(myPathVariable + "/*")

commented Jan 7, 2014 by Jan Sotola (17,310 points)
edited Jan 7, 2014

Yes, exactly.

...