I'm trying to write a simple program that will compare the files in separate folders. I'm currently using LINQ to Objects to parse the folder and would like to included information extracted from the string in my result set as well.
Here's what I have so far:
FileInfo[] fileList = new DirectoryInfo(@"G:\Norton Backups").GetFiles();
var results = from file in fileList
orderby file.CreationTime
select new { file.Name, file.CreationTime, file.Length };
foreach (var x in results)
Console.WriteLine(x.Name);
This produces:
AWS025.sv2i
AWS025_C_Drive038.v2i
AWS025_C_Drive038_i001.iv2i
AWS025_C_Drive038_i002.iv2i
AWS025_C_Drive038_i003.iv2i
AWS025_C_Drive038_i004.iv2i
AWS025_C_Drive038_i005.iv2i
...
I would like to modify the LINQ query so that:
- It only includes actual "backup" files (you can tell the backup files because of the
_C_Drive038
in the examples above, though038
and possibly the drive letter could change). - I want to include a field if the file is the "main" backup file (i.e., it doesn't have
_i0XX
at the end of the file name). - I want to include the "image number" of the file (e.g. in this case it's
038
). - I want to include the increment number if it's an incrememnt of a base file (e.g.
001
would be an increment number)
I believe the basic layout of the query would look like the following, but I'm not sure how best to complete it (I've got some ideas for how some of this might be done, but I'm interested to heard how others might do it):
var results = from file in fileList
let IsMainBackup = \\ ??
let ImageNumber = \\ ??
let IncrementNumber = \\ ??
where \\ it is a backup file.
orderby file.CreationTime
select new { file.Name, file.CreationTime, file.Length,
IsMainBackup, ImageNumber, IncrementNumber };
When looking for the ImageNumber
and IncrementNumber
, I would like to assume that the location of this data is not always fixed, meaning, I'd like to know of a good way to parse this (If this requires RegEx, please explain how I might use it).
NOTE: Most of my past experience in parsing text involved using location-based string functions, such as LEFT
, RIGHT
, or MID
. I'd rather not fall back on those if there is a better way.
Using regular expressions:
Here is a description of the regular expression:
Backup is:
Increment is:
Here is the test code I used:
And here is the output I get:
It was a bit of fun working out a good answer for this one :)
The below piece of code gives you what you need. Note the use of the search pattern when retrieving the files - there is no point retrieving more files than necessary. Also notice the use of the parseNumber() function, this was just to show you how to change the string result from the regex to a number should you need it in that format.
Note that with the regexs i am assuming some consistency in the file names, if they were to deviate from the format you mentioned then you would have to adjust them.