Using Directory.GetFiles with a Regular Expression Filter

I have a folder with two files:

  • Awesome.File.20091031_123002.txt
  • Awesome.File.Summary.20091031_123152.txt

In addition, a third-party application processes files as follows:

  • Reads folderPath and searchPattern from the database
  • Performs Directory.GetFiles(folderPath, searchPattern) , processes all files matching the filter, and then moves the files to the archive folder.

It turns out that I need to move two files to different archive folders, so I need to process them separately, providing different search parameters to select them separately. Please note that I cannot change the third-party application, but I can change the addresses of searchPattern and files in my database.

What does searchPattern allow me to select Awesome.File.20091031_123002.txt without including Awesome.File.Summary.20091031_123152.txt ?

+7
c #
source share
5 answers

Awesome.File. ???????? _ ??????. Txt

The question mark (?) Acts as a single character holder.

+8
source share

If you are going to use LINQ, then ...

  var regexTest = new Func<string, bool>(i => Regex.IsMatch(i, @"Awesome.File.(Summary)?.[\d]+_[\d]+.txt", RegexOptions.Compiled | RegexOptions.IgnoreCase)); var files = Directory.GetFiles(@"c:\path\to\folder").Where(regexTest); 
+9
source share

I wanted to try my meager linq skills here ... I'm sure there is a more elegant solution, but here's mine:

 string pattern = ".SUMMARY."; string[] awesomeFiles = System.IO.Directory.GetFiles("path\\to\\awesomefiles"); IEnumerable<string> sum_files = from file in awesomeFiles where file.ToUpper().Contains(pattern) select file; IEnumerable<string> other_files = from file in awesomeFiles where !file.ToUpper().Contains(pattern) select file; 

This assumes that there are no files other than two in another directory, but you can customize the template here to suit your needs (ie add β€œAwesome.File” to the top of the template.)

When you repeat the assembly of each of them, you should get what you need.

+6
source share

According to the documentation , searchPattern only supports ***** and ? Wildcards . You will need to write your own regular expression filter that will take the results of Directory.GetFiles and apply further filtering logic.

+5
source share

If you do not want to use Linq, here is one way.

Public Sub FileChecker (ByVal filePath As String)

  Dim di As New DirectoryInfo(filePath) Dim _MatchCounter As Integer Dim RegexPattern As String = "^[a-zA-Z_a-zA-Z_a-zA-Z_0-9_0-9_0-9.csv]*$" Dim RegexPatternMatch As New Regex(RegexPattern, RegexOptions.IgnoreCase) For Each matchingFile As FileInfo In di.GetFiles() Dim m As Match = RegexPatternMatch.Match(matchingFile.Name) If (m.Success) Then MessageBox.Show(matchingFile.Name) _MatchCounter += 1 End If Next End Sub 
0
source share

All Articles