3

I am populating a datagrid control using files in a specified path (DirectoryInfo).
I would like to filter the files based on a user specified date range (start date & end date).

While search S/O, I found this post, but I am getting an error on DateComparer ("'DateComparer' is a type and cannot be used as an expression.")

Any other suggestions on how to filter by date?

Here is my code:

        Dim dirInfo As New DirectoryInfo(strDirectoryPath)
        Dim dStartDate As DateTime = "03/01/2011"
        Dim dEndDate As DateTime = "6/30/2011"
        Dim Files As FileInfo = dirInfo.GetFiles().Where(Function(Files) Files.CreationTime >= (dStartDate) AndAlso Files.CreationTime <= dEndDate)

            datagrid.DataSource = Files
            datagrid.DataBind()
Community
  • 1
  • 1
Troy
  • 1,659
  • 4
  • 19
  • 33

1 Answers1

2
DateTime your_start_date = new DateTime(2011,1,1);
DateTime your_end_date = new DateTime(2011,10,1);
FileInfo [] files = new DirectoryInfo(@"c:\").GetFiles().Where(x=>x.CreationTime>=(your_start_date) && x.CreationTime<=(your_end_date)).ToArray();

foreach(var item in files)
{
 Console.WriteLine(item.Name);
}

On my test case prints out:

copy_one.jpg
copy_one_one.jpg
copy_one_one_one.jpg
hiberfil.sys
one.jpg
pagefile.sys
PcapDotNet.snk

UPDATE (VB version):

Dim your_start_date As New DateTime(2011, 1, 1)
Dim your_end_date As New DateTime(2011, 10, 1)
Dim files As FileInfo() = New DirectoryInfo("c:\").GetFiles().Where(Function(x) x.CreationTime >= (your_start_date) AndAlso x.CreationTime <= (your_end_date)).ToArray()

For Each item As FileInfo In files
    Console.WriteLine(item.Name)
Next
Icarus
  • 63,293
  • 14
  • 100
  • 115
  • what is "x" supposed to be in your last line? – Troy Oct 09 '11 at 16:42
  • The `GetFiles()` method returns an array of type `FileInfo`; therefore in `Where(x...)`, `x` is a `FileInfo` object. – Icarus Oct 09 '11 at 16:47
  • so it would be like "files=>files.CreationTime" ? – Troy Oct 09 '11 at 16:48
  • @Troy You are trying to get files within a specified date range, correct? If so, then yes, files=>files.CreationTime>= somedate andalso files.CreationTime<=someotherdate. – Icarus Oct 09 '11 at 16:53
  • yes, trying to get files within a date range. Check out my original post, I added my code. – Troy Oct 09 '11 at 16:54
  • Getting an error at dirInfo.GetFiles().Where..... "Overload resolution failed because no accessible 'Where' can be called with these arguments" – Troy Oct 09 '11 at 16:55
  • @Troy, I tested my VB.NET version (see update on my answer) and it works fine. What version of the .NET Framework are you using? – Icarus Oct 09 '11 at 16:58
  • @Troy Should work just fine. Copy and paste my version of the code adapted to your needs. – Icarus Oct 09 '11 at 17:03
  • @Troy this is asp.net; it's not enough to set DataSource=files you also need to do dgInvoiceList.DataBind() right after that. – Icarus Oct 09 '11 at 17:11
  • I am doing the databind, but no records are showing (updated original post) – Troy Oct 09 '11 at 17:12
  • @Troy That can only mean one of 3 things: You did not set AutoGenerateColumns =true to your gridview; there are no files within the date range specified or you don't have permissions to the path specified in `strDirectoryPath` and an exception is being thrown but you are eating it. Are you using Cassini or IIS to debug your app? – Icarus Oct 09 '11 at 17:18
  • AutoGenerateColumns is set to True; Files do exist that should show up in that date range; I have a try catch block with a Msgbox(ex.tostring()) so I should see any exceptions; I have permissions (since I got records before I started to filter by date) – Troy Oct 09 '11 at 17:21
  • This is my local machine, so I am using VS 2008 to dev and debug – Troy Oct 09 '11 at 17:23
  • @Troy What do you mean by `MSgBox`? `MsgBox` is for `Winforms`, is it not? It will never display anything on a Web app. You tagged the question with `asp.net` – Icarus Oct 09 '11 at 17:26
  • yes, I am using asp.net. The MsgBox seems to work on my machine (I guess since its a local web server). I have a breakpoint at "Dim dirInfo As New DirectoryInfo(strDirectoryPath) " so when I walk thru the code, there are no expections. – Troy Oct 09 '11 at 17:28
  • nevermind... my bad. My code had "CreationTime" and the datagrid was using "LastWriteTime". It works now. Thanks very much for your help !! – Troy Oct 09 '11 at 17:30
  • dude... you went out of your way to see me thru to the end... I appreciate that. Thanks again. – Troy Oct 09 '11 at 17:34