Linq Exchange
LINQ and Lamda Expressions Explained

Recommendations


Search


Tags


Sometimes we have to write applications which need to retrieve a list of files filtered by the date the file was created or last written to. A common example is for applications which need to delete a list of files older than a certain date. For example, I have an application which creates a new log file every time the application is launched. The customer does not want to have log files older than seven days, so each time the application launches, it must first delete log files which are older than seven days.

For this example, we will be using the System.IO namespace. I have included the full namespace in the examples, but obviously you will want to add it to your using statements, instead.

We will create a function which takes two parameters: the full path of the directory where the log files are stored, and the date at which files should NOT be selected. Files older than this date will be selected. 

Below are two examples of how to accomplish this task. The first example demonstrates the C# 2.0 method. The second example demonstrates using LINQ to filter the file list.

Filter a File List by Date using C# 2.0

/// <summary>
/// Outputs the list of log files in a directory
/// </summary>
/// <param name="FileDir">the directory to search</param>
/// <param name="NewFileDate">
///    files older than this date will be outputted.
///    newer files will be skipped.
/// </param>
public static void OutputLogFileList(string FileDir, DateTime NewFileDate)
{
    // get the list of all log files in FileDir
    List<string> LogFiles = System.IO.Directory.GetFiles(FileDir, "*.log").ToList();

    // filter the list to only include files older than NewFileDate
    List<string> OutputList = new List<string>();
    foreach (string file in LogFiles)
    {
        // add this file if it's creation date is less than NewFileDate
        if (System.IO.File.GetCreationTime(file) < NewFileDate)
            OutputList.Add(file);
    }

    // output the list
    foreach (string logFile in OutputList)
        Console.WriteLine(logFile);
}

 

 Using LINQ to Filter a File List by Date

/// <summary>
/// Outputs the list of log files in a directory
/// </summary>
/// <param name="FileDir">the directory to search</param>
/// <param name="NewFileDate">
///    files older than this date will be outputted.
///    newer files will be skipped.
/// </param>
public static void OutputLogFileList(string FileDir, DateTime NewFileDate)
{
    // get the list of all log files in FileDir
    List<string> LogFiles = System.IO.Directory.GetFiles(FileDir, "*.log").ToList();

    // filter the list to only include files older than NewFileDate
    List<string> OutputList = LogFiles.Where(x => System.IO.File.GetCreationTime(x) < NewFileDate).ToList();

    // output the list
    foreach (string logFile in OutputList)
        Console.WriteLine(logFile);
}

 

As you can see, using LINQ removed 5 lines of code. We could further remove another line by using the following instead (notice I removed the System.IO namespace):

// get the list of all log files in FileDir
List<string> OutputList = GetFiles(FileDir, "*.log").Where(x => File.GetCreationTime(x) < NewFileDate).ToList();

If you have questions, comments, or suggestions, please feel to post them in the LINQ Exchange Forum


 

Posted by: LinqMaster
Posted on: 06/03/2008 at 12:04 PM
Tags: , ,
Categories: LINQ
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Related posts