0

I'm trying to find write a recursive method to find a file in given directory. I'm actually a beginner in programming and C#, this my first time writing a recursive method. The method will go through each file and all sub directories to find the file.

This is what I wrote:

  static string GetPath(string currentDirectory)
    {
        string requiredPath = "";            
                      
        string[] files = Directory.GetFiles(currentDirectory);
        string[] dirs = Directory.GetDirectories(currentDirectory);
                
        foreach (string aPath in files)
        {
            if (aPath.Contains("A.txt"))
            {
                requiredPath = aPath;                    
            }                                
        }

        foreach (string dir in dirs)
        {
            requiredPath = GetPath(dir);
        }
                                     
        return requiredPath;                        
    }

Now the problem is when I pass currentDirectory as C:\users\{Enviroment.UserName}\OneDrive\Desktop\TestFolder, which contains a few more test folders, it works. But when I try to run it in a folder that I want to find the file in, it doesn't return anything. Path to that folder is C:\users\{Enviroment.UserName}\OneDrive\Data\SomeFolder that folder has some subdirectories and files and also the file A.txt

I even tried:

 foreach (string aPath in files)
        {
            if (aPath.Contains("A.txt"))
            {
                requiredPath = aPath;                    
            }                                
        }

but it doesn't work. Am I doing something wrong? I don't understand why it works in the test folders and why it doesn't work When I'm trying to run it in a real folder. Again, I'm just a beginner teenage learning programming, this is my first time writing recursive methods so yeah. This is also my first question in this community.

Mak
  • 1
  • 4
  • `Directory.GetFiles` takes a pattern, and a flag indicating if you want subfolders e.g. `Directory.GetFiles("c:\\temp", "*.png", SearchOption.AllDirectories)`. Do you specifically want a recursive method, because you're learning recursion? Or would this one-line approach suffice for your needs? – Caius Jard Jan 15 '22 at 11:26
  • Take a look at : https://stackoverflow.com/questions/12332451/list-all-files-and-directories-in-a-directory-subdirectories All You Need with more than 15 sample code – AliNajafZadeh Jan 15 '22 at 11:29
  • I already tried `SeachOptions.AllDirectores` but it gives an UnauthorizedAccessException that I can't figure how to handle. @CaiusJard – Mak Jan 15 '22 at 11:31
  • Did you read.. https://stackoverflow.com/questions/172544/ignore-folders-files-when-directory-getfiles-is-denied-access - if you're on NETCore/5+ you can use `EnumerateFiles` instead, passing `IgnoreInaccessible` – Caius Jard Jan 15 '22 at 11:34
  • yes I did read that and no, I am not on NetCore/5+ that's why it isn't working for me. yeah I already have looked up the link yesterday, it is helpful. But I wasn't able to implement the solutions in my project. I just couldn't understand them. but yeah I already completed my method by myself and it works like I want it too. Thanks a lot for your input and yours too @Caius Jard. I really appreciate it. – Mak Jan 15 '22 at 12:44

1 Answers1

0

Okay I found the solution to this, I just had to put an if command when I was recursing it.

        foreach (string dir in dirs)
        {
            requiredPath = GetPath(dir);
            if (requiredPath.Contains("PasswordGenerator.py"))
                return requiredPath;
        }

It wasn't checking that if the file is in requiredPath it just kept on searching.

Well I showed my father my method and he made another simpler one for me.

 static string GetFilePath(string folderToSearch, string fileName)
    {
        if (Directory.GetFiles(folderToSearch).Any(x => x.Contains(fileName)))
            return folderToSearch;
        var folders = Directory.GetDirectories(folderToSearch);
        foreach(var folder in folders)
        {
            var filePath = GetFilePath(folder, fileName);
            if (filePath != null)
                return filePath;
        }
        return null;
    }
Mak
  • 1
  • 4