Parsing the canonical path using wildcards

I am writing a C # function that retrieves some associated files.

Input: C: \ abc * \ abc? \ Testfile. *
Exit: all files match.

I thought I could do it by recursion. But it was not easy :(

Do you have a good algorithm?

Update :
I did it. Thanks Kieren :)

void PrintAllFiles(DirectoryInfo currentDir, 
                   string currentPattern, string nextPatten)
{
  DirectoryInfo[] dis = currentDir.GetDirectories(currentPattern);

  if (dis.Length > 0)
  {
    string[] remainPattern = nextPatten.Split("\\".ToCharArray());
    if (remainPattern.Length > 0)
    {
      foreach (DirectoryInfo di in dis)
      {
        PrintAllFiles(di, remainPattern.First(), 
                       string.Join("\\", remainPattern.Skip(1).ToArray()));
      }
    }
  }

  FileInfo[] fis = currentDir.GetFiles(currentPattern);
  foreach (FileInfo fi in fis)
  {
    Console.WriteLine(fi.DirectoryName + "\\" + fi.Name);
  }
}
+3
source share
1 answer

The simplest is using recursion; you first get the base folder (C: \), then pass C:\as the current path, abc*to the "current template" abc?\testfile.*parameter and to the "Next templates" parameter.

, 'c:\abc123': , C:\abc123 , abc? " " testfile.* " ".

, , :)

, .

+3

Source: https://habr.com/ru/post/1755918/


All Articles