Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / WPF
Alternative
Tip/Trick

List Only Files Created or Modified Yesterday in C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
27 Apr 2019CPOL 27.1K   13   9
This is an alternative for "List Only Files Created or Modified Yesterday in C#"

Introduction

How to create a list of files in a folder and its sub-folders that were created or modified yesterday only, not today or two days ago. This is an alternative to the published version, which is over commented, making it look like student code. It should be self documenting, if you write it correctly. It's also rather ... "single use" where I much prefer a generic solution.

Using the Code

Rather than "fixing" it to "yesterday", make it generic, so you feed it a start and end date and it returns all between. That's easy to do. First, an Extension method to provide a Between test:

C#
public static class Extensions
    {
    public static bool Between(this DateTime dt, DateTime start, DateTime end)
        {
        if (start < end) return dt >= start && dt <= end;
        return dt >= end && dt <= start;
        }
    }

Then, a method to use it:

C#
public IEnumerable<FileInfo> GetFilesBetween(string path, DateTime start, DateTime end)
     {
     DirectoryInfo di = new DirectoryInfo(path);
     FileInfo[] files = di.GetFiles();
     return files.Where(f => f.CreationTime.Between(start, end) ||
                             f.LastWriteTime.Between(start, end));
     }

To include subdirectories, use the GetFiles overload:

C#
FileInfo[] files = di.GetFiles("*.*", SearchOption.AllDirectories);

Then the specific "yesterday" case becomes trivial:

C#
public IEnumerable<FileInfo> GetYesterdayFiles(string path)
     {
     DateTime today = DateTime.Today;
     return GetFilesBetween(path, today.AddDays(-1), today);
     }

History

  • 2019-04-27 V1.2 Forgot to update history when published V1.1 ...
  • 2019-04-27 V1.1 Typo: I typed "wheer" for "where" 
  • 2019-04-27 Original version. I tried suggesting this to the OP in moderation, but ...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
QuestionComplete code-listing Pin
Knecht Ruprecht4-Jan-21 0:44
Knecht Ruprecht4-Jan-21 0:44 
QuestionGetYesterdayFiles Pin
Klaus-Werner Konrad6-May-19 2:50
Klaus-Werner Konrad6-May-19 2:50 
GeneralMy vote of 5 Pin
nospam19611-May-19 11:21
nospam19611-May-19 11:21 
QuestionClear and concise! Thanks! Pin
Mark Miller29-Apr-19 9:17
Mark Miller29-Apr-19 9:17 
Questionuse EnumerateFiles instead of GetFiles Pin
netclectic28-Apr-19 4:26
netclectic28-Apr-19 4:26 
AnswerRe: use EnumerateFiles instead of GetFiles Pin
obermd29-Apr-19 8:12
obermd29-Apr-19 8:12 
QuestionThe way things should be done. Pin
LightTempler28-Apr-19 2:25
LightTempler28-Apr-19 2:25 
QuestionTypo Pin
Nelek27-Apr-19 8:13
protectorNelek27-Apr-19 8:13 
AnswerRe: Typo Pin
OriginalGriff27-Apr-19 9:59
mveOriginalGriff27-Apr-19 9:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.