Link to home
Start Free TrialLog in
Avatar of narmi2
narmi2

asked on

Simple File Search

Hi

I would like to search my FULL hard drive "C:\" and list all files with the extension ".doc".  These files could be in the root of "C:\" or in directories or sub directories.  It should search in all sub directories to all depths and all the matching extension paths and filename in a listview.

Does anyone know how to do this?

Thanks
Avatar of flavo
flavo
Flag of Australia image

Based on this PAQ : http:Q_21329033.html

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        GetSubdirectoriesAndFiles("c:\") '// Change to your path
    End Sub


    Sub GetSubdirectoriesAndFiles(ByVal directory As String)
        Dim di As New System.IO.DirectoryInfo(directory)
        For Each fi As System.IO.FileInfo In di.GetFiles()
            If fi.Extension = ".doc" Then
                Me.ListBox1.Items.Add(fi.FullName)
            End If
        Next

        For Each dir As System.IO.DirectoryInfo In di.GetDirectories()
            GetSubdirectoriesAndFiles(dir.FullName)
        Next
    End Sub

Dave
Avatar of narmi2
narmi2

ASKER

How would you do this with a listview?  If the listview had 3 columns, how would i populate the listviews columns like this

col1 - Full Path
col2 - File Name
col3 - Extension
Avatar of narmi2

ASKER

Ok I got the following

------------------------------------
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        GetSubdirectoriesAndFiles(System.IO.Directory.GetDirectories("C:\"), ListView1) '// Change to your path
    End Sub

    Sub GetSubdirectoriesAndFiles(ByVal directory() As String, ByVal lv As ListView)
        Dim MyCountSearch As Integer = 1
        Dim Dir As String
        Dim lv2 As ListViewItem

        For Each Dir In directory
            Dim Files() As String = System.IO.Directory.GetFiles(Dir, "*")
            Dim File As String
            Dim Dirc As String

            For Each File In Files
                If Path.GetFileName(File).EndsWith(".doc") Or Path.GetFileName(File).EndsWith(".DOC") Then
                    lv2 = New ListViewItem
                    lv2.Text = Path.GetFullPath(File)
                    lv2.SubItems.Add(Path.GetFileName(File))
                    lv2.SubItems.Add(Path.GetExtension(File))
                    lv.Items.Add(lv2)
                End If
            Next
            GetSubdirectoriesAndFiles(System.IO.Directory.GetDirectories(Dir), lv)
        Next
    End Sub
---------------------------------------

But this misses out all the .doc files in the root of C:\ ???

How do I search the root also?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of flavo
flavo
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of narmi2

ASKER

Your method looks better than the one i posted.  Give me a few moments to test it out! :D
Avatar of narmi2

ASKER

That's perfect!

Thanks! :D
:^)

Good luck with the rest of your project(s)

Dave