1

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?

giboo
  • 103
  • 11
  • Hi, What is .svn ? Hidden folder ? then this is duplicate with https://stackoverflow.com/questions/2418270/c-sharp-get-a-list-of-files-excluding-those-that-are-hidden – ilansch Jul 03 '18 at 05:13
  • Possible duplicate of [C# - Get a list of files excluding those that are hidden](https://stackoverflow.com/questions/2418270/c-sharp-get-a-list-of-files-excluding-those-that-are-hidden) – ilansch Jul 03 '18 at 05:13
  • Thanks. In those examples, we have logic for filtering files. Even though we can use similar logic for getDirectories, I wanted a simple solution for creating a DirectoryInfo[] array with all folders except ".svn". – giboo Jul 03 '18 at 05:16
  • .svn is one example. I may need it for a regular folder as well... – giboo Jul 03 '18 at 05:17
  • Alternatively you can either look for that particular folder name and remove from the result set, or filter the whole list as it comes. – PepitoSh Jul 03 '18 at 05:20
  • Thanks. "remove from the results set?" Is it possible to remove from Directoryinfo[] list? If possible great, please let me know (I dont want to re-construct directoryinfo[]). As of now, I had an option to ignore this dir in DirectoryInfo[] while processing (but in several places). But, I wanted a simple solution for this – giboo Jul 03 '18 at 05:27
  • 2
    Try `DirectoryInfo[] dirs = dir.GetDirectories().Where(x => x.Extension != ".svn").ToArray();` – Enigmativity Jul 03 '18 at 05:27
  • That worked.. Thanks.. – giboo Jul 03 '18 at 05:35
  • I used DirectoryInfo[] dirs = dir.GetDirectories().Where(x => x.Name != ".svn").ToArray(); since it is a folder name (and it required System.Linq; namespace) – giboo Jul 03 '18 at 05:36

3 Answers3

2

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
0

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;
irkForce
  • 352
  • 2
  • 12
0

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

giboo
  • 103
  • 11