简体   繁体   中英

Counter inside a recursive function

I need to count the number of files deleted in this recursive function. Since it's recursive I cannot use if statements, and C# does not support global variables. Any alternatives?

static void DirSearch(string path)
{
    try
    {
        foreach (string dirPath in Directory.GetDirectories(path))
        {
            foreach (string filePath in Directory.GetFiles(dirPath))
            {
                string filename = Path.GetFileName(filePath);
                if (filename.Equals("desktop.txt"))
                {
                    File.Delete(filePath);
                    //count++
                }
                Console.WriteLine(filePath); // print files
            }
            Console.WriteLine(dirPath); // print directories
            DirSearch(dirPath);
        }
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);
    }
}

Try a recursive count as follows. So DirSearch returns the count of deleted files.

static int DirSearch(string path)
{
    int count = 0;

    try
    {
        foreach (string dirPath in Directory.GetDirectories(path))
        {
            foreach (string filePath in Directory.GetFiles(dirPath))
            {
                string filename = Path.GetFileName(filePath);
                if (filename.Equals("desktop.txt"))
                {
                    File.Delete(filePath);

                    count++;
                }
                Console.WriteLine(filePath); // print files
            }
            Console.WriteLine(dirPath); // print directories
            count += DirSearch(dirPath);
        }
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);
    }

    return count;
}

One way is to pass in something for it to count into. I'd do this using ref , for example:

static void DirSearch(string path, ref int count)
{
    try
    {
        foreach (string dirPath in Directory.GetDirectories(path))
        {
            foreach (string filePath in Directory.GetFiles(dirPath))
            {
                string filename = Path.GetFileName(filePath);
                if (filename.Equals("desktop.txt"))
                {
                    File.Delete(filePath);
                    count++
                }
                Console.WriteLine(filePath); // print files
            }
            Console.WriteLine(dirPath); // print directories
            DirSearch(dirPath,ref count);
        }
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);
    }
}

Then call it:

int count = 0;

DirSearch(@"C:\SomePath",ref count);

Then you can use count as normal as you had commented out in your code.

Without a ref variable (so you don't need to pass something that is correctly initialized):

static int DirSearch(string path)
{
    try
    {
        int count = 0;
        foreach (string dirPath in Directory.GetDirectories(path))
        {
            foreach (string filePath in Directory.GetFiles(dirPath))
            {
                string filename = Path.GetFileName(filePath);
                if (filename.Equals("desktop.txt"))
                {
                    File.Delete(filePath);
                    count++;
                }
                Console.WriteLine(filePath); // print files
            }
            Console.WriteLine(dirPath); // print directories
            count += DirSearch(dirPath);
        }
        return count;
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);
    }
}

Did You also consider a non-recursive solution like presented here? https://stackoverflow.com/a/929418/2979680

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM