简体   繁体   中英

Using Directory.GetFiles with regex-like filter

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 ?

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);

Awesome.File.????????_??????.txt

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

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 (ie add "Awesome.File" to the pattern start.)

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

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.

If you don't want to use Linq, here's one way.

public void FileChecker(string filePath)
{
    DirectoryInfo di = new DirectoryInfo(filePath);
    int _MatchCounter;
    string RegexPattern = "^[a-zA-Z_a-zA-Z_a-zA-Z_0-9_0-9_0-9.csv]*$";
    Regex RegexPatternMatch = new Regex(RegexPattern, RegexOptions.IgnoreCase);

    foreach (FileInfo matchingFile in di.GetFiles())
    {
        Match m = RegexPatternMatch.Match(matchingFile.Name);

        if ((m.Success))
        {
            MessageBox.Show(matchingFile.Name);
            _MatchCounter += 1;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM