Search file in directory using complex pattern
-
03-07-2019 - |
Question
I am looking for a C# library for getting files or directory from a directory using a complex pattern like the one used in Ant:
dir1/dir2/**/SVN/*
--> Matches all files in SVN directories that are located anywhere in the directory tree under dir1/dir2**/test/**
--> Matches all files that have a test element in their path, including test as a filename.- ...
Do I need to code it myself? extract what I want from NAnt? Or this library exists and my google skill sucks.
Directory.GetFiles(String path, String searchPattern)
doesn't handle directory pattern and NDepend.Helpers.FileDirectoryPath neither (it's a great library for path manipulation by the way)
Solution
Coding it yourself wouldnt be that hard.
Just use a correctly formulated regular expression with System.IO methods to build the full path
OTHER TIPS
Are you comfortable with defining "*" as "anything but slash" and "**" as "anything at all"? If so, the regex conversion seems straightforward.
* -> [^\/]*
** -> .*
Then it's a matter of recursively enumerating all files, and checking if their paths match the regex.