We help IT Professionals succeed at work.

Simple File Search

narmi2
narmi2 asked
on
Medium Priority
261 Views
Last Modified: 2010-04-23
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
Comment
Watch Question

Commented:
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

Author

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

Author

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
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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

Author

Commented:
That's perfect!

Thanks! :D

Commented:
:^)

Good luck with the rest of your project(s)

Dave
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.