giboo
giboo

Reputation: 103

Excluding a directory with specific pattern while using dir.GetDirectories

I want to exclude folder name '.svn" while using the GetDirectories method

DirectoryInfo[] dirs = dir.GetDirectories();

I read somewhere that searchPattern parameter for GetDirectories can support only ? and * wildcards and won't support any other regular expression.

I wanted to populate all the folders except ".svn" folder(for example) using DirectoryInfo[] dirs. Is this possible? If not, what are the other alternatives I have?

Upvotes: 1

Views: 461

Answers (3)

giboo
giboo

Reputation: 103

Answer : DirectoryInfo[] dirs = dir.GetDirectories().Where(x => x.Name != ".svn").ToArray(); (and it required System.Linq; namespace)

Assuming the following is still applicable :
https://meta.stackexchange.com/questions/1555/mark-a-comment-as-answer-to-a-question

Upvotes: 0

irkForce
irkForce

Reputation: 372

Yes, MSDN says (https://msdn.microsoft.com/en-au/library/f3e2f6e5(v=vs.110).aspx) that search pattern does not support regex. But you can filter the results by regex on the client side. For example:

var di = new DirectoryInfo("c:\\");
var dirInfos = di.GetDirectories();
var filtered = from r in dirInfos where !Regex.IsMatch(r.FullName,"$*.svn") select r;

Upvotes: 0

Metin Ogurlu
Metin Ogurlu

Reputation: 51

You can use linq methods after GetDirectories method.

using System.Linq;
...
...
dir.GetDirectories().Where(d => !d.Name.StartsWith(".")).ToList(); //does not starts with dot.
dir.GetDirectories().Where(d => d.Name != ".svn").ToList(); //does not equal .svn

Upvotes: 2

Related Questions