2012-11-22 51 views
-1

该文件夹中的文件,我有一个文件夹中的多个文件有命名约定如何从基于日期

Name_MoreName_DDMMYYYY_SomeNumber_HHMMSS.txt 

我怎样才能得到它有最古老的日期和时间(即最老的DDMMYYYY和HHMMSS文件)。

例:

  • Name_MoreName_22012012_SomeNumber_072334.txt
  • Name_MoreName_22012012_SomeNumber_072134.txt
  • Name_MoreName_24012012_SomeNumber_072339.txt
  • Name_MoreName_22012012_SomeNumber_072135.txt

所以最早的文件将是

Name_MoreName_22012012_SomeNumber_072134.txt 

我怎样才能取得最古老的文件?

编辑 这是我在一个forach循环到目前为止..做我被一个

private void FileInformation(string fileName, ref string concatFile) 
     { 
      try 
      { 
       string completeFileName = fileName.Trim(); 
       string[] fileComponenets = completeFileName.Split('_'); 

       string fileDate = string.Empty; 
       string fileTime = string.Empty; 


       if (fileComponenets.Length > 0) 
       { 
        fileDate = fileComponenets[4].Replace(".txt", "").Trim(); 
        fileTime = fileComponenets[2].ToString(); 
        concatFile = fileDate + "-" + fileTime; 
       } 

      } 

      catch (Exception ex) 
      { 
          } 

     } 

读取文件名一个 - 主要功能

string fileStats = string.Empty; 
foreach (string filePath in arrFileCollection) 
       { 
        if (filePath.ToLower().Contains("Name_MoreName_")&& 
         filePath.ToLower().Contains(".txt")) 
        { 
               string concatFile = string.Empty; 
         FileInformation(filePath.Replace(dirPath, ""), ref concatFile); 
         fileStats = fileStats + "," + concatFile; 
        } 
} 

现在我我正在用逗号分隔值的字符串获取所有日期时间。现在我卡住了。我该如何利用它们之间最小的,并得到了相关文件

EDIT2

注:Framework是.NET 2.0

+0

是预定义的文件名,保证格式相同? – Adil

+0

@Adil是的,它是一个预定义的格式 – Zerotoinfinity

+0

@Zerotoinfinite:我已经根据.NET框架2.0更新了解决方案/代码 – Adil

回答

1
string oldestFile = Directory.EnumerateFiles(path) 
          .OrderBy(file => ExtractDateTimeFrom(file)) 
          .First(); // FirstOrDefault 

而且编写方法,将解析您的文件名和提取日期从它:

public static DateTime ExtractDateTimeFrom(string fileName) 
{ 
    Regex regex = new Regex(@".+_(\d\d\d\d\d\d\d\d)_.+_(\d\d\d\d\d\d).txt"); 
    var match = regex.Match(fileName); 
    string dateString = match.Groups[1].Value + match.Groups[2].Value; 
    return DateTime.ParseExact(dateString, "ddMMyyyyHHmmsss", null); 
} 

.NET 2.0最简单的解决方案:

string oldestFile = ""; 
DateTime oldestDate = DateTime.Max; 

foreach(string fileName in Directory.GetFiles(path)) 
{ 
    DateTime date = ExtractDateTimeFrom(fileName); 
    if (date < oldestDate) 
    { 
     oldestFile = fileName; 
     oldestDate = date; 
    } 
} 
+0

我能够从文件中获取日期和时间,并且还能够将它们在一个字符串中逗号分隔。 – Zerotoinfinity

+0

:(我相信这是用于3.5+框架,但我正在使用.NET2.0 – Zerotoinfinity

1

使用DirectoryInfo和FileInfo类。例如,只给想法:

 IOrderedEnumerable<FileInfo> filesInfo = new DirectoryInfo("D:\\") 
                 .EnumerateFiles() 
                 .OrderBy(f=>f.FullName); 

更新:对于.NET 2.0,我建议你比较逻辑从主代码分开......为什么不创建实现IComparable接口的自定义类型。

public class CustomFileInfo :IComparable<CustomFileInfo> 
{ 
    public string Name { get; set; } 
    public string MoreName { get; set; } 
    public DateTime FileDate { get; set; } 
    public int Number { get; set; } 
    public DateTime FileTime { get; set; } 

    public CustomFileInfo(string fileNameString) 
    { 
     string[] fileNameStringSplited = fileNameString.Split('_'); 
     this.Name = fileNameStringSplited[0]; 
     this.MoreName = fileNameStringSplited[1]; 
     this.FileDate = DateTime.ParseExact(fileNameStringSplited[2], "ddMMyyyy", null); 
     this.Number = int.Parse(fileNameStringSplited[3]); 
     this.FileTime = DateTime.ParseExact(fileNameStringSplited[4], "HHmmss", null); 
    } 

    public int CompareTo(CustomFileInfo other) 
    { 
     // add more comparison criteria here 
     if (this.FileDate == other.FileDate) 
      return 0; 
     if (this.FileDate > other.FileDate) 
      return 1; 
     return -1; 
    } 
} 

然后在你的代码,你可以简单的使用得到DirectoryInfo的文件,并比较每个文件...

FileInfo[] filesInfo = new DirectoryInfo("D:\\").GetFiles(); 
    //set first file initially as minimum 
    CustomFileInfo oldestFileInfo = new CustomFileInfo(filesInfo[0].FullName); 

    for (int i = 1; i < filesInfo.Length; i++) 
    { 
      CustomFileInfo currentFileInfo = new CustomFileInfo(filesInfo[i].FullName); 
     //compare each file and keep the oldest file reference in oldestFileInfo 
      if (oldestFileInfo.CompareTo(currentFileInfo) < 0) 
       oldestFileInfo = currentFileInfo; 
    } 

您可以使用优化的代码和自定义比较代码按你的标准。

1

使用:

更新

List<string> address = new List<string>() { "Name_MoreName_22012011_SomeNumber_072334.txt", 
      "Name_MoreName_22012012_SomeNumber_072134.txt", 
      "Name_MoreName_24012012_SomeNumber_072339.txt", 
      "Name_MoreName_22012012_SomeNumber_072135.txt",}; 
      DateTimeFormatInfo dtfi = new DateTimeFormatInfo(); 
      dtfi.ShortDatePattern = "dd-MM-yyyy"; 
      dtfi.DateSeparator = "-"; 
      address = address.OrderBy(s => Convert.ToDateTime((s.Split('_')[2]).Insert(2, "-").Insert(5, "-"), dtfi)).ToList(); 
      string oldest = address[0]; 
+0

@Zeroottoinfinite:answer updated – KF2

1

这样的可能?

 string[] filePaths = Directory.GetFiles(@"c:\MyDir\"); 
     Regex rex   = new Regex(@"^.*_(\d+)\.txt"); 
     int date   = int.MaxValue; 
     int oldestdate  = int.MaxValue; 
     String oldestfile; 
     foreach(String filePath in filePaths) 
     { 
      Match match = rex.Match(filePath); 

      if(match.Success) 
       date = int.Parse(match.Groups[0].Value); 
      if (date < oldestdate) 
      { 
       oldestdate = date; 
       oldestfile = filePath; 
      } 
     }