I need to get list of files on some drive with paths that matches specific pattern, for example FA\d\d\d\d.xml where \d is digit (0,1,2..9). So files can have names like FA5423.xml.
What is the most efficient name to do this?
In order to search files using a regular expression, select the 'File Name' file matching rule, select the 'RegEx' pattern matching operator and enter a regular expression that should be matched. For example, the '\. (JPG|BMP|PNG)$' regular expression will match all JPG, BMP and PNG image files.
2.1 Matching a Single Character The fundamental building blocks of a regex are patterns that match a single character. Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" .
Match any specific character in a setUse square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.
All modes after the minus sign will be turned off. E.g. (? i-sm) turns on case insensitivity, and turns off both single-line mode and multi-line mode. Not all regex flavors support this.
Are you using C# 3?
Regex reg = new Regex(@"^FA[0-9]{4}\.xml$");
var files = Directory.GetFiles(yourPath, "*.xml").Where(path => reg.IsMatch(path));
You could do something like:
System.IO.Directory.GetFiles(@"C:\", "FA????.xml", SearchOption.AllDirectories);
Then from your results just iterate over them and verify them against your regex i.e. that the ?
characters in the name are all numbers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With