Reputation: 13699
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?
Upvotes: 0
Views: 241
Reputation: 12718
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).
Upvotes: 1
Reputation: 3120
If you want a generic solution which would automatically identify all files that match, Yyou could use file globs in the shell if they are available and work for your particular case:
something like:
ls root/*/ | sed 's/^(.*)([0-9])+(.[A-Za-z][A-Za-z][A-Za-z]+)$/"\1\2\3" \2/' | xargs -n2 runMyProgramHere
if you need to do it programatically, normally directory inquiries are fairly easy in most languages, list everything in /root, of those, list everything, filter by files ending in +.+, there's your list.
in psuedo-code:
for (directory in file.getDirectoryList("/root")) {
for (name in file.getDirectoryList("/root/" + directory)) {
if (name contains a sequence of numbers followed by a dot ending with an extension) {
extract id
store filename and id
}
}
}
you can probably do this with regexes if you really want, but I tend to avoid regexes in programs unless I have a really good reason not to. They are often poorly understood and prone to breaking without good error reporting.
Upvotes: 0