Scenario:
Our customer is has provided us with files whose names contain an ID number that we need for indexing purposes.
.\root\dir1\a123.txt (ID is 123)
.\root\dir2\abc345.csv (ID is 345)
.\root\dir3\235.xls (ID is 235)
we know what format to expect based on the files location and extension. Our customer would like to be able to add
.\root\dir4\foo556.bar (ID is 556)
meaning we cannot write a custom method for each entry under root.
My Solution:
The solution we are thinking of is to store the formats of the file names in an XML file
<root>
<entry>
...
<format>abc###</format>
...
<entry>
<root>
when the customer want to add a new entry under root they’ll have to give a directory, a file extension and a format. Then on our end implement a getID() method that is able to use the format specified in the XML to retrieve the IDs from the file name.
Question:
Has anyone else dealt with a similar situation? If so is there a better solution than the one I have provided?
Assuming the file name will always be on the form
<letters><digits>.<extension>, I would use a simple regular expression to match the relevant part of the name. E.g..*\\[a-z]*\([0-9]*\)\..*(may vary depending on the RE engine in question).