Take the 2-minute tour ×

I have a folder with two files:

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

Additionally, a third-party app handles the files as follows:

  • Reads a folderPath and a searchPattern out of a database
  • Executes Directory.GetFiles(folderPath, searchPattern), processing whatever files match the filter in bulk, then moving the files to an archive folder.

It turns out that I have to move my two files into different archive folders, so I need to handle them separately by providing different searchPatterns to select them individually. Please note that I can't modify the third-party app, but I can modify the searchPattern and file destinations in my database.

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

share|improve this question
Are they always of the format Name.File.date_time.txt and Name.File.Summary.date_time.txt ? –  Russ Cam Nov 3 '09 at 20:23
add comment

4 Answers

up vote 7 down vote accepted


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

share|improve this answer
+1, +answer: awesomeness :) –  Juliet Nov 3 '09 at 20:31
add comment

If your were 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);
share|improve this answer
add comment

According to the documentation, searchPattern only supports the * and ? wildcards. You would need to write your own regex filter that takes the results of Directory.GetFiles and applies further filtering logic.

share|improve this answer
add comment

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 there aren't any other files in the directory other than the two, but you can adjust the pattern here to suit your needs (i.e. add "Awesome.File" to the pattern start.)

When you iterate the collection of each, you should get what you need.

share|improve this answer
sniff I started answering before I saw the acceptance. Oh well. Enjoy if you can use :). –  Todd Richardson Nov 3 '09 at 20:45
+1 to cheer you up :-) –  ralf.w. Aug 26 '11 at 6:49
add comment

Your Answer


