0

I have the bit of code that searches the entire computer for a file and once found, should return that file. The problem is in the second function, the recursive one. Once the file is found, it should return, which it does, but for some reason, even after returning the value, it continues the recursive search.

I don't get it. I'd still consider myself new to programming so please explain in detail if you see what I'm doing wrong.

public string SearchDirectory(string dir, string fileName)
{
    string foundDir = "";
    bool fileFound = false;
    // Gets all files from directory and creates list of matches to fileName
    try
    {
        foreach (string match in Directory.GetFiles(dir, fileName))
        {
            // Returns the first found match as a path and breaks loop
            Console.WriteLine("Checked path: " + dir + ".");
            if (File.Exists(dir + @"\" + fileName))
            {
                Console.WriteLine("FOUND!!");
                fileFound = true;
                foundDir = dir;
                break;
            }
            if (fileFound == true)
            {
                break;
            }
        }
    }
    catch
    {
        Console.WriteLine("Access to path: " + dir + " denied.");
    }

    // If fileName isn't found in directory, it searches each new directory
    // The last directory it will check is the last one in the original directory
    if (fileFound == false)
    {
        try
        {
            foreach (string newDirectory in Directory.GetDirectories(dir))
            {
                Console.WriteLine("Checked path: " + dir + ".");
                SearchDirectory(newDirectory, fileName);
            }
        }
        catch
        {
            Console.WriteLine("Access to path: " + dir + " denied.");
        }
        // fileName does not exist in starting directory
    }
    else
    {
        return foundDir;
    }
    return "";
}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • Note: I tried actually returning where it was Found, but that didn't work either, it just kept going. – JoshuaWashua Feb 18 '20 at 19:52
  • 2
    You're not even doing anything with the return value when you recursively call `SearchDirectory`. – juharr Feb 18 '20 at 19:55
  • @JoshuaWashua Please refer: https://stackoverflow.com/questions/929276/how-to-recursively-list-all-the-files-in-a-directory-in-c . It does exactly what you want – sam Feb 18 '20 at 19:56
  • You don't need to do the directory recursion yourself, just do `Directory.GetFiles(dir, fileName, SearchOption.AllDirectories);` – nollidge Feb 18 '20 at 20:07

2 Answers2

5

Your recursive call is ignoring the returned value. You should instead check to see if it found something so you can stop searching more sub-directories.

foreach (string newDirectory in Directory.GetDirectories(dir))
{
    Console.WriteLine("Checked path: " + dir + ".");
    var result = SearchDirectory(newDirectory, fileName);
    if(result != "") return result;
}
juharr
  • 31,741
  • 4
  • 58
  • 93
0

You could just do this instead:

public string SearchDirectory(string dir, string fileName)
{
  return Directory
    .EnumerateFiles(dir, fileName, SearchOption.AllDirectories)
    .FirstOrDefault() ?? "";
}
Robert McKee
  • 21,305
  • 1
  • 43
  • 57