Basically I have a directory of DWG files stored in a List that I need to search through and find files with the same Well Name but newest date in the File name. Examples of file names are as follows:
Smith 08-14 #3-14H11 FINAL 02-05-2011.dwg
Scroggins 07-14 3-14 SDWD FINAL 10-28-2009.dwg
Smith 08-14 #3-14H11 FINAL 03-11-2011.dwg
Russel 10-15 #4-33H21 FINAL 10-07-2009.dwg
Scroggins 0开发者_开发问答7-14 3-14 SDWD FINAL 11-29-2010.dwg
Notice after Smith there is a number 08-14, that is not the date. The date I need to somehow compare to similar files is at the end of the File Name. So for instance out of Smith 08-14 #3-14H11 FINAL 02-05-2011.dwg
and Smith 08-14 #3-14H11 FINAL 03-11-2011.dwg
, I would need to remove Smith 08-14 #3-14H11 FINAL 02-05-2011.dwg
from my List because the other File with same Well name has a newer date.
Assuming your file names are in some kind of List<String>
or you can use them as that.
make a small class to store information for each file
public class WellFile
{
public DateTime FileDate { get; set; }
public String WellName { get; set; }
public String OriginalFile {get; set; }
public WellFile(String FileName)
{
var fileHeader = FileName.Split('.').First().Split(' ').ToList();
WellName = fileHeader.First();
FileDate = DateTime.Parse(fileHeader.Last());
OriginalFile = FileName;
}
}
then using this class and your list of files
var ConvertedFileList = YourFileList.Select(x => new WellFile(x));
var NewestWellFile = ConvertedFileList.GroupBy(x => x.WellName)
.Select(group => group.OrderByDescending(x => x.FileDate).First().OriginalFile).ToList();
I haven't tested it but that should work for you
Working Example Program
using System;
using System.Linq;
using System.Collections.Generic;
public class Foo
{
public static void Main()
{
var wellList = new List<String>() {"Smith 08-14 #3-14H11 FINAL 02-05-2011.dwg",
"Scroggins 07-14 3-14 SDWD FINAL 10-28-2009.dwg",
"Smith 08-14 #3-14H11 FINAL 03-11-2011.dwg",
"Russel 10-15 #4-33H21 FINAL 10-07-2009.dwg",
"Scroggins 07-14 3-14 SDWD FINAL 11-29-2010.dwg"};
var ConvertedFileList = wellList.Select(x => new WellFile(x));
var NewestWellFile = ConvertedFileList.GroupBy(x => x.WellName)
.Select(group => group.OrderByDescending(x => x.FileDate).First().OriginalFile).ToList();
NewestWellFile.ForEach(x => Console.WriteLine(x));
Console.Read();
}
}
public class WellFile
{
public DateTime FileDate { get; set; }
public String WellName { get; set; }
public String OriginalFile {get; set; }
public WellFile(String FileName)
{
var fileHeader = FileName.Split('.').First().Split(' ').ToList();
WellName = fileHeader.First();
FileDate = DateTime.Parse(fileHeader.Last());
OriginalFile = FileName;
}
}
Make an data table and to break the file name into various components:
Smith 08-14 #3-14H11 FINAL 02-05-2011.dwg
becomes
Name = 'Smith'
Code1 = '08-14 #3-14H11'
Revision = 'FINAL'
Date = '02-05-2011'
For example, take the last 14 characters of the file name 02-05-2011.dwg
and then removing the .dwg
to make it 02-05-2011
.
Now you will sort by (Name, Date) then then just use the ones that match your criteria.
精彩评论