tags:

views:

57

answers:

1

Hi,

i am using following code to search my computer for files with certian extensions. I currenty searching for .eml files. This code lists all files found. I need to enter 10 most current files' path to my list box. How can I do it? Thanks

Dim DriveLetter As String = "c:\" '"
Dim Ext As String = ".eml"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.SearchPath(DriveLetter)
End Sub

Sub SearchPath(ByVal Path As String)
    Dim DirParent As New IO.DirectoryInfo(Path)
    ''# Me.AddResults("Searching: " & DirParent.FullName)
    Dim ListFiles() As IO.FileInfo

    Try
        ListFiles = DirParent.GetFiles("*" & Ext)
        For Each SingleFile In ListFiles
            Me.AddResults("Found: " & SingleFile.FullName)
            Application.DoEvents()
        Next

    Catch ex As Exception
        ''# Me.AddResults("GET FILES ERROR: " & ex.Message)

    End Try

    Try
        Dim DirChildren() As IO.DirectoryInfo = DirParent.GetDirectories

        For Each DirEnum In DirChildren
            SearchPath(DirEnum.FullName)
        Next

    Catch ex As Exception
        ''# Me.AddResults("GET DIRS ERROR: " & ex.Message)

    End Try
End Sub

Sub AddResults(ByVal ResultString As String)
    lstFiles.Items.Add(ResultString)
    Application.DoEvents()
End Sub
+1  A: 

Instead of adding the found files to list box directory add the found FileInfo to a list first. When all the files have been found use a linq query to find the 10 most recent files and add that to the list.

Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq

Private Sub Button1_Click(ByVal sender As System.Object,
                          ByVal e As System.EventArgs
) Handles button1.Click

    ' Note C:\\ is required to stackoverflow.com formatting '
    Dim foundFiles = SearchPath("C:\\", ".eml")
    Dim recentFiles = From file In foundFiles
                      Order By File.LastWriteTime Descending
                      Take 10

    lstFiles.Items.Clear()

    For Each file In recentFiles

        lstFiles.Items.Add(file.FullName)

    Next

End Sub

Function SearchPath(ByVal path As String,
                    ByVal ext As String
) As List(Of FileInfo)

    Dim dirParent = New IO.DirectoryInfo(path)
    Dim foundFiles = New List(Of FileInfo)

    foundFiles.AddRange(dirParent.GetFiles("*" & ext))

    For Each directory In dirParent.GetDirectories()

        Try

            foundFiles.AddRange(SearchPath(directory.FullName, ext))

        Catch ex As System.UnauthorizedAccessException

            ' Ignore these exceptions. '

        End Try

    Next

    Return foundFiles

End Function
Tim Murphy
Thanks a lot. But I donot have any idea of LINQ. Kindly modify my code how it should be used. This will be a great favour. Thanks
@Tim: The `ToList` is redundant in any case. The rest looks good.
Konrad Rudolph
The code gives following errors
Dim foundFiles = Me.SearchPath("C:\", ".eml") On ".eml" Too many arguments to public subsearch path (Path as string)Function SearchPath(ByVal path As String, ByVal ext As String) As List(Of FileInfo)File Info is not defined
I've fixed the recursive call to SearchPath for extension plus added import statements. Does it compile now?
Tim Murphy
Yeah code works now but now it gives error that access is denied to a few folders. I think I can manage it. Thanks a lot.
Corrected code to handle access denied exception. Don't forget to accept the answer.
Tim Murphy
Great work........ Thank you sir !