-1

I'm trying to display an Excel file that was created last week, between Monday-Friday. For instance, if it's Monday, I would like to look for files between last Monday and Friday. If it's Friday of the same week, I want to query the same time frame.

I know the following code will give me yesterday results, but how would I get a date range? Thanks.

string[] files = Directory.GetFiles(FBD.SelectedPath, "*.xls")
   .Where(file => new FileInfo(file).CreationTime.Date == DateTime.Today.AddDays(-1))
   .ToArray();
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
user2839067
  • 11
  • 1
  • 5

4 Answers4

1

First you would need to check the DateTime.Now.Day to get the Day of the week, then add or subtract x number of days to create your range and add an AND clause to your .where:

  //Add AND clause to .where and add or subtract days occordingly to create range, instead of using == you would use <= end and >= start
    string[] files = Directory.GetFiles(FBD.SelectedPath, "*.xls")
       .Where(file => new FileInfo(file).CreationTime.Date >= DateTime.Today.AddDays(-1) && new FileInfo(file).CreatTime.Date <= DateTime.Today.AddDays(1)
       .ToArray();
Ryan Wilson
  • 10,223
  • 2
  • 21
  • 40
  • I doubt that `DateTime.Today.AddDays(-1)` until `AddDays(1)` is the range OP wants. He wants to select last week if today is monday, in all other cases he wants to select the current week from monday-friday. – Tim Schmelter Mar 12 '18 at 13:06
  • @TimSchmelter I gave that as an example of how to create a range, I explained in my comment above the answer and in my comments above that he would need to get the DateTime.Now.Day to get the current day of the week, then add and subtract x from DateTime.Today to create the correct range. – Ryan Wilson Mar 12 '18 at 13:07
0

DateTime is comparable not only with == but also with < or >. I'd use the cheaper File.GetCreationTime method:

DateTime lastMonday = DateTime.Today.StartOfWeek(DayOfWeek.Monday);
if(lastMonday == DateTime.Today)
   lastMonday = lastMonday.AddDays(-7);
DateTime lastFriday = lastMonday.AddDays(4);
string[] files = Directory.GetFiles(FBD.SelectedPath, "*.xls")
    .Select(f => new { File = f, CreationDate = File.GetCreationTime(f).Date })
    .Where(x => x.CreationDate >= lastMonday && x.CreationDate <= lastFriday)
    .Select(x => x.File)
    .ToArray();

Uses this StartOfWeek method.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0
var fileInfos = Directory.GetFiles(FBD.SelectedPath, "*.xls").Select(file => new FileInfo(file));

var files = fileInfos
                .Where(fi => fi.CreationTime.Date >= DateTime.Today.AddDays((int)DayOfWeek.Monday - (int)DateTime.Today.DayOfWeek - 7) 
                       && 
                       fi.CreationTime.Date < DateTime.Today.AddDays((int)DayOfWeek.Saturday - (int)DateTime.Today.DayOfWeek) - 7)
                .ToArray();

Check that it has to be < DateTime.Today.AddDays((int)DayOfWeek.Saturday - (int)DateTime.Today.DayOfWeek) - 7) if you take into account the Hour because DateTime.Today will give you 12:00:00am so if the field has the hour will not work ok if you put <= DateTime.Today.AddDays((int)DayOfWeek.Friday - (int)DateTime.Today.DayOfWeek) - 7).

Otherwise (not taking into account hour) you can do it <= DateTime.Today.AddDays((int)DayOfWeek.Friday - (int)DateTime.Today.DayOfWeek) - 7)

fmaccaroni
  • 3,846
  • 1
  • 20
  • 35
-1

Something like that :

string[] files = Directory.GetFiles(FBD.SelectedPath, "*.xls") .Where(file => new FileInfo(file).CreationTime.Date >= DateTime.Today.AddDays(-2) && new FileInfo(file).CreationTime.Date <= DateTime.Today.AddDays(-1)) .ToArray();

jeancallisti
  • 1,046
  • 1
  • 11
  • 21