Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to get a list of files in a directory by created date using LINQ

Tags:

c#

linq

I am trying to get a list of all files in a directory by write date:

private void Form1_Load(object sender, EventArgs e) {
  DateTime LastCreatedDate = 
               Properties.Settings.Default["LastDateTime"].ToDateTime();

  string [] filePaths = Directory.GetFiles(@"\\Pontos\completed\", "*_*.csv")
                        .OrderBy(p => System.IO.File.GetLastWriteTime(p))
                        .Where(p>=LastCreatedDate);
}

Questions

  1. How do I properly do a WHERE clause to get only files greater than or equal to the date in my settings?
  2. string [] is not working for this one because it does not know how to do conversion. which data type should I be using?
like image 624
Alex Gordon Avatar asked Oct 08 '22 14:10

Alex Gordon


1 Answers

Is there any reason you're not using DirectoryInfo instead of Directory - this will save you having to parse the file paths back into Files to get the dates, or store the dates in a separate variable:

DateTime lastCreatedTime = new DateTime(2012, 01, 30, 05, 12, 00);

var files = new DirectoryInfo(@"\\Pontos\completed\").GetFiles("*_*.csv")
            .Where(f => f.LastWriteTime >= lastCreatedTime)
            .OrderBy(f => f.LastWriteTime)
            .Select(f => new {f.FullName});

foreach (var file in files) {
  Console.WriteLine(file.FullName);
}
like image 118
Zhaph - Ben Duguid Avatar answered Oct 12 '22 12:10

Zhaph - Ben Duguid