Maro
Maro

Reputation: 2629

Control recursive method depth - get all sub folders

I'm traveling through some shares to get information/permissions .. etc I'm using recursive to travel through all sub shares. it works fine however, the user should be able to limit the sub shares level to specific number which is a parameter in the application?

private static INodeCollection NodesLookUp(string path)
    {
        var shareCollectionNode = new ShareCollection(path);
        // Do somethings

       foreach (var directory in Directory.GetDirectories(shareCollectionNode.FullPath))
        {
            shareCollectionNode.AddNode(NodesLookUp(directory));

        }
        return shareCollectionNode;
    }

this code will go all way to the lowest level, how can i stop it in specific level? for example get all shares until 2 levels only?

Thanks.

Upvotes: 3

Views: 2825

Answers (3)

Ilya Ivanov
Ilya Ivanov

Reputation: 23636

How about passing level variable and increasing it after each level of recursion call? This will allow you to control what are the current recursion level or how many levels left. Don't forget to check for null.

private const int maxDepth = 2;

private static INodeCollection NodesLookUp(string path, int level)
{
   if(level >= maxDepth)
        return null;

   var shareCollectionNode = new ShareCollection(path);
   // Do somethings

   foreach (var directory in Directory.GetDirectories(shareCollectionNode.FullPath))
   {
       var nodes = NodesLookUp(directory, level + 1);

       if(nodes != null)
            shareCollectionNode.AddNode(nodes);

   }
   return shareCollectionNode;
}

Initial level can be zero-indexed, like

NodesLookUp("some path", 0);

Upvotes: 5

Piotr Justyna
Piotr Justyna

Reputation: 4986

What about this:

    private static INodeCollection NodesLookUp(string path, Int32 currentLevel, Int32 maxLevel)
    {
        if (currentLevel > maxLevel)
        {
            return null;
        }

        var shareCollectionNode = new ShareCollection(path);
        // Do somethings

        foreach (var directory in Directory.GetDirectories(shareCollectionNode.FullPath))
        {
            INodeCollection foundCollection = NodesLookUp(directory, currentLevel + 1, maxLevel)

            if(foundCollection != null)
            {                
                shareCollectionNode.AddNode();
            }
        }

        return shareCollectionNode;
    }

In this case you don't have to worry about the state of your private fields being modified each time the method runs. And, as far as the rest of your code is thread-safe, it will be thread-safe.

Upvotes: 0

Jim Mischel
Jim Mischel

Reputation: 134045

Rather than using a global variable to control the level, pass the maxLevel and decrement with each recursive call.

private static INodeCollection NodesLookUp(string path, int maxLevel)
{
    var shareCollectionNode = new ShareCollection(path);
    if (maxLevel > 0)
    {
        foreach (var directory in Directory.GetDirectories(shareCollectionNode.FullPath))
        {
            shareCollectionNode.AddNode(NodesLookup(directory, maxLevel-1));
        }
    }
    return shareCollectionNode;
}

Upvotes: 5

Related Questions