tags:

views:

77

answers:

3

Hi all:

I was wondering if I want to keep track of the number of folders in a recursive method, how can I made it so the counter would not get reseted each time in the loop in below's code?

// how to keep track of the number of sourceFolder that has been processed?
public void recursiveMethod(SPFolder sourceFolder, SPFolder destinationFolder)
{
    int totalNumberOfFiles = sourceFolder.SubFolders.Count;

    foreach (SPFolder sourceSubFolder in sourceFolder.SubFolders)
    {
        if (true)
        {
            SPFolder destSubFolder = null;
            if (true)
            {
                destSubFolder = doSomething();
            }
            else
            {
                destSubFolder = doSomethingElse();
            }
        }
        recursiveMethod(sourceSubFolder, destSubFolder);
    }
}

The goal here is to display something like "processing folder 15 / 100" to the user. I'm having trouble keeping the folder count without getting it resetted. Assumption: the folders themselves dont have a unique ID.

Thanks.

+1  A: 

Can you just pass the count variable as a parameter to the method?

James Gaunt
Sure, and then have the method return the count. If that's not convenient, the count can be passed by reference.
Steven Sudit
+2  A: 

This code allows you to show how many folders have been processed so far. However it doesn't get the total amount of folders as is tricky: you can't know that until whole recursion has finished:

public void recursiveMethod(SPFolder sourceFolder, SPFolder destinationFolder)
{
    int folderIndex = 1;

    recursiveMethod(sourceFolder, destinationFolder, ref folderIndex);
}

public void recursiveMethod(SPFolder sourceFolder, SPFolder destinationFolder, ref folderIndex)
{
    int totalNumberOfFiles = sourceFolder.SubFolders.Count;

    foreach (SPFolder sourceSubFolder in sourceFolder.SubFolders)
    {
        // Display processed folder
        Console.WriteLine("Processing folder " + folderIndex);

        if (true)
        {
            SPFolder destSubFolder = null;

            if (true)
            {
                destSubFolder = doSomething();
            }
            else
            {
                destSubFolder = doSomethingElse();
            }
        }

        // Increase processed folder index
        folderIndex++;

        // Recursive call
        recursiveMethod(sourceSubFolder, destSubFolder, ref folderIndex);
    }
}
antur123
I'm not sure how this is any better than passing in the current count and returning the potentially incremented count.
Steven Sudit
A: 

You could first 'collect' all folders, and after that you know how many folders you have you can start working.

untested pseudocode:

var foldersToDoSomethingWith = new List<SPFolder>() {startSourceFolder};
var folders = new Stack<SPFolder>();
folders.Push(startSourceFolder);
while(folders.Any())
{
    foreach (SPFolder sourceSubFolder in folders.Pop().SubFolders)
        folders.Push(sourceSubFolder);
        foldersToDoSomethingWith.Add(sourceSubFolder)
}

var i = 0;
var c = folders.Count();
foreach(SPFolder f in foldersToDoSomethingWith)
{
    Console.WriteLine(String.Format("processing folder {0} / {1}", i, c));
    ...doSomething with SPFolder f...
    i++;
}
dkson
I'm not convinced that converting recursion to iteration with an explicit stack is a win.
Steven Sudit
@Steven Sudit This way you now how many folders you have to work on before starting to actually do something, since BeraCim wants to print a message like "processing folder 15 / 100", which is much easier this way than using a recursive solution. Beside that i would agree with you that there's no win in using a stack over a recursive function.
dkson
There's no connection between using an iterative method and having a progress bar. Nothing stops you from calling a recursive method whose "do something" is simply to accumulate the folders into a list. Then you know the length of the list before iterating over it to perform the task you actually care about. It's a red herring.
Steven Sudit