0

Is it possible to get all folders that match pattern including wildcards? I have this folder structure:

C
....logs
........v1
............api1
............api2
........v2
............api1
............api2
........other

I would like to get all folders that match this pattern:

c:\logs\v*\api*

So I should get theese directories:

c:\logs\v1\api1
c:\logs\v1\api2
c:\logs\v2\api1
c:\logs\v2\api2

I tried with this:

Directory.GetDirectories(@"c:\logs\v*\api*");

but this gives me an exception:

System.IO.IOException: 'The filename, directory name, or volume label syntax is incorrect. : 'c:\logs\v*\api*''

I also tried this:

string directory = @"c:\logs\v*\api*";
string rootDirectory = Directory.GetDirectoryRoot(directory);
Console.WriteLine($"rootDirectory: {rootDirectory}");

string remainingPath = directory.Substring(rootDirectory.Length);
Console.WriteLine($"remainingPath: {remainingPath}");

var result = Directory.GetDirectories(rootDirectory, remainingPath);

Which gives an output:

rootDirectory: c:\
remainingPath: logs\v*\api*

But at the end, exception is thrown:

System.IO.IOException: 'The filename, directory name, or volume label syntax is incorrect. : 'c:\logs\v*''

Any ideas?

dafie
  • 951
  • 7
  • 25
  • Have you tried using this `public static string[] GetDirectories (string path, string searchPattern, System.IO.SearchOption searchOption);` (see [this](https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getdirectories?view=net-6.0)) – limserhane Jun 07 '22 at 13:54

1 Answers1

3

Use the searchPattern argument of the GetDirectories() method to search for directories with a specific search pattern.

var result = Directory.GetDirectories(@"C:\logs\", "v*");

Adjust the start directory and search pattern for your needs.

You can also split the given string path into the root directory and search path and use them as arguments for the Directory.GetDirectories() method:

string directory = @"c:\logs\v*\api";
string rootDirectory = Directory.GetDirectoryRoot(directory);
//Console.WriteLine(rootDirectory);
string remainingPath = directory.Substring(rootDirectory.Length);
//Console.WriteLine(remainingPath);
var result = Directory.GetDirectories(rootDirectory, remainingPath);
    
Progman
  • 16,827
  • 6
  • 33
  • 48
  • The problem is, that `c:\logs\v*\api` comes from user input, so I would like to keep that unchanged – dafie Jun 07 '22 at 14:01
  • @dafie Adjusted the answer. – Progman Jun 07 '22 at 14:26
  • Unfortunately it doesn't work - I get an exception: `System.IO.IOException: 'The filename, directory name, or volume label syntax is incorrect. : 'c:\logs\v*''`. I found out, that this works: `Directory.GetDirectories(@"c:\logs", "v*");`, but this doesn't `Directory.GetDirectories(@"c:\logs", "v*\api");` – dafie Jun 07 '22 at 16:51
  • @dafie You might want to use https://stackoverflow.com/questions/370267/get-the-drive-letter-from-a-path-string-or-fileinfo to get the drive letter from the path to later split it up into `rootDirectory` and `remainingPath`. – Progman Jun 07 '22 at 16:56
  • @dafie "\a" is an escape sequence, you have to use `\\ ` or use the `@` prefix. – Progman Jun 07 '22 at 16:59
  • I tried with `@`, but it still doesn't work. Does it work on your pc? – dafie Jun 07 '22 at 17:02
  • @dafie Yes, but I'm testing it on Linux with different paths. You might want to edit your original questions to include ALL attempts on how you use `Directory.GetDirectories()` (and `Directory.GetDirectoryRoot()`) and the result and/or complete error messages you get. – Progman Jun 07 '22 at 17:13
  • I created another question with more detailed info: https://stackoverflow.com/questions/72542599/enumerate-directories-that-match-wildcard – dafie Jun 08 '22 at 08:40