Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to specify directory path with a wildcard?

Tags:

c#

.net

path

I have the following piece of code:

foreach (string file in Directory.GetFiles(sourcePath))
{
    // whatever
}

That gets files from a specific directory. Would it be possible to match directories using a wildcard? For example:

c:\test\di*

would match all files in the directories:

c:\test\dictionary\
c:\test\directory\
c:\test\dig\

I saw that you can pass a file filter to the GetFiles method, but that applies to files only, not directory names.

like image 972
Abe Miessler Avatar asked Sep 02 '11 16:09

Abe Miessler


1 Answers

You have an overload for this which allows you to specify a search pattern or if you need to specify search options there's another overload:

foreach (string directory in Directory.GetDirectories(sourcePath, "di*"))
{
    // whatever
}
like image 123
Darin Dimitrov Avatar answered Oct 05 '22 01:10

Darin Dimitrov