I had recently posted on 7 New methods to Enumerate Directory and Files in .NET 4.0
A user commented asking if it was possible to returns a list of files from a directory for a given date range (i.e. start date – end date). Here’s how to do so:
C#
using System; using System.Linq; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { DirectoryInfo DirInfo = new DirectoryInfo(@"D:\Articles\Pics\jQuery"); DateTime dt1 = new DateTime(2009, 07, 15); DateTime dt2 = new DateTime(2010, 04, 15); // LINQ query for files between 15-July 2009 and 15-April 2010. var files = from file in DirInfo.EnumerateFiles() where file.CreationTimeUtc > dt1 & file.CreationTimeUtc < dt2 select file; // Show results. foreach (var file in files) { Console.WriteLine("{0} created on {1}", file.Name, file.CreationTimeUtc); } Console.ReadLine(); } } }
Namespace ConsoleApplication1 Friend Class Program Shared Sub Main(ByVal args() As String) Dim DirInfo As New DirectoryInfo("D:\Articles\Pics\jQuery") Dim dt1 As New Date(2009, 07, 15) Dim dt2 As New Date(2010, 04, 15) ' LINQ query for files between 15-July 2009 and 15-April 2010. Dim files = From file In DirInfo.EnumerateFiles() Where file.CreationTimeUtc > dt1 And file.CreationTimeUtc < dt2 Select file ' Show results. For Each file In files Console.WriteLine("{0} created on {1}", file.Name, file.CreationTimeUtc) Next file Console.ReadLine() End Sub End Class End Namespace