+2 votes
by (1.2k points)

Currently, I'm using the Ftp.GetItems() extension method to get all items of a given path.

I'm looping the result item collection until I find the first item for which IsDirectory is true.

In pseudo-code:

public bool HasChildFolders(string path)
{
    var items = _ftp.GetItems(path);
    foreach ( var item in items )
    {        
        if ( item.IsDirectory ) return true;
    }

    return false;
}

Unfortunately, if the given path contains lots of child folders this takes ages to complete (I have folders with several 10k child folders).

So I'm searching for a faster way to check whether a folder contains at least one child folder.

My question:

How to quickly check whether a folder contains at least one child folder, even for a huge number of child folders?

Applies to: Rebex FTP/SSL

1 Answer

+1 vote
by (58.9k points)
selected by
 
Best answer

Ftp.GetItems is a multi-file method that is by default recursive and the filtering is done at the client side. This means that it will try to search for any items even within all your subfolders. Considering you have thousands of folders at the server, this obviously makes it a really long lasting method call that consisting of thousands of FTP commands/response being sent/received by the FTP client. The recursive GetItems method is good for recursive/advanced listing of directory content, but for your task this is simply overkilling.

There are two possible solutions - either switch to the simple listing methods, e.g. GetList:

var items = Ftp.GetList(path);

or just tell the ftp.GetItems method to do a non-recursive search like this:

var items = ftp.GetItems(path, TraversalMode.NonRecursive);

You can also check this comparison of GetItems and GetList methods.

by (1.2k points)
edited by
Thanks, I'm already doing it non-recursively. I'll try the GetList() method.
by (58.9k points)
Ok! Let me know if the GetList helped.
by (1.2k points)
I've switched to GetList and found no whatsoever improvement in terms of speed. So I'll stick for now with GetItems. Thanks again for your help!
by (58.9k points)
This is really strange, the GetList should be considerably quicker. Could you please create a log (https://www.rebex.net/kb/logging) from the attempt where you use GetList and then send the log file back to us for analysis? I'll look into what is causing the delay with GetList then.
by (1.2k points)
Late update: Using Ants Performance Profiler I was able to detect that my (too) excessive logging was the primary reason that the HasChildFolders() call was so slow. I now temporarily disable logging when calling HasChildFolders() and later enable it again. While this is bad for hunting errors, it is good for performance.
by (73.5k points)
Great that you were able to find the cause of the issue.
You can also try to keep logging but in less verbosity. For example log only main operations and errors.
...