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
LVL 1
narmi2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

flavoCommented:
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
0
narmi2Author Commented:
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
0
narmi2Author Commented:
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
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

flavoCommented:
Something like this?
 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ListView1.View = View.Details
        ListView1.Columns.Add("Full Name", 200, HorizontalAlignment.Left)
        ListView1.Columns.Add("Name", 200, HorizontalAlignment.Left)
        ListView1.Columns.Add("Extension", 50, HorizontalAlignment.Left)
        GetSubdirectoriesAndFiles("D:\")
    End Sub


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

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
narmi2Author Commented:
Your method looks better than the one i posted.  Give me a few moments to test it out! :D
0
narmi2Author Commented:
That's perfect!

Thanks! :D
0
flavoCommented:
:^)

Good luck with the rest of your project(s)

Dave
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.