Search for files within certain date range

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 9
Reputation: sdimantova is an unknown quantity at this point 
Solved Threads: 0
sdimantova sdimantova is offline Offline
Newbie Poster

Search for files within certain date range

 
0
  #1
Jun 18th, 2008
I am new to VB and am trying to create a program that will search for files in a user specified directory within a specified date range, and then list the files in a list view. I've managed to figure most of it out except how to specify the date range I want.
Simplified a bit, this is what I have:
  1. Private Sub SearchBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchBtn.Click
  2. Dim dirInfo As New DirectoryInfo(DirSearchTxtBox.Text)
  3. Dim CutOffDate As Date = Date.FromOADate(4 / 1 / 2008)
  4. FilesSearch(dirInfo, CutOffDate, FilesList)
  5. End Sub
  6. Public Sub FilesSearch(ByVal dirInfo As DirectoryInfo, ByVal CutOffDate As Date, ByVal List As ListView)
  7. Dim resultFiles() As FileInfo
  8. resultFiles = dirInfo.GetFiles(CutOffDate)
  9.  
  10. For Each resultFile As FileInfo In resultFiles
  11. Dim colItem(5) As String
  12. Dim attributes As ListViewItem
  13. colItem(0) = (resultFile.Name)
  14. colItem(1) = (resultFile.DirectoryName)
  15. colItem(2) = (resultFile.LastAccessTime)
  16. colItem(3) = (resultFile.Length / 1000)
  17. colItem(4) = (resultFile.Attributes)
  18. attributes = New ListViewItem(colItem)
  19. FilesList.Items.Add(attributes)
  20. Next resultFile
  21.  
  22. Dim SubDirs() As DirectoryInfo = dirInfo.GetDirectories()
  23. For Each SubDir As DirectoryInfo In SubDirs
  24. FilesSearch(SubDir, CutOffDate, List)
  25. Next SubDir
  26. End Sub

I want the search to produce all files that are older than 2 years, and maybe provide a field where the user can specify a date, and then the search would find all files older than the date they enter. Any help is greatly appreciated!
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

Re: Search for files within certain date range

 
0
  #2
Jun 18th, 2008
Try this:
Imports System.IO

Public Class Form1

    Private Sub SearchBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchBtn.Click
        Dim dirInfo As New DirectoryInfo(DirSearchTxtBox.Text)
        Dim CutOffDate As Date = "#6 / 1 / 2008#"        
        FilesSearch(dirInfo, CutOffDate, FilesList)
    End Sub

    Public Sub FilesSearch(ByVal dirInfo As DirectoryInfo, ByVal CutOffDate As Date, ByVal List As ListView)
        Dim resultFiles() As FileInfo
        resultFiles = dirInfo.GetFiles()

        For Each resultFile As FileInfo In resultFiles
            If resultFile.CreationTime.Date < CutOffDate Then
                Dim colItem(5) As String
                Dim attributes As ListViewItem
                colItem(0) = (resultFile.Name)
                colItem(1) = (resultFile.DirectoryName)
                colItem(2) = (resultFile.LastAccessTime)
                colItem(3) = (resultFile.Length / 1000)
                colItem(4) = (resultFile.Attributes)
                attributes = New ListViewItem(colItem)
                FilesList.Items.Add(attributes)
            End If
        Next resultFile

        Dim SubDirs() As DirectoryInfo = dirInfo.GetDirectories()
        For Each SubDir As DirectoryInfo In SubDirs
            FilesSearch(SubDir, CutOffDate, List)
        Next SubDir
    End Sub


End Class
Last edited by waynespangler; Jun 18th, 2008 at 1:50 pm.
Wayne

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC