Search code examples
c#xmlvb.netfileinfo

Select Files From Folder Depending on Name Convention


I receive a bunch of XML files in a folder.

I want to keep checking for files that have the following naming convention:

sr-{first_id}-{second_id}-matchresults.xml

To parse as soon as I receive one.

For example:

sr-40-24-standings.xml
sr-40-24-results.xml
sr-40-24-j7844-matchresults.xml

I should select that one : sr-40-24-j7844-matchresults.xml

What comes after this that helps me select Files depending on their naming convention from a ASP Web Service?

 Dim files As IO.FileInfo() = FolderName.GetFiles("*.xml")

Solution

  • private bool IsValid(string value)
        {
            string regexString = "^sr-([a-z0-9]+)-([a-z0-9-]+)-matchresults.xml";
            return Regex.IsMatch(value, regexString);
        }
    

    This method will give you the files with the specified format (sr-{first_id}-{second_id}-matchresults.xml). Note: your Ids can contain alphanumeric characters also "-" symbol. if you don't want that symbol in id, then code will look like,

    string regexString = "^sr-([a-z0-9]+)-([a-z0-9]+)-matchresults.xml";