?
Solved

Simple File Search

Posted on 2006-03-24
7
Medium Priority
?
245 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
0
Comment
Question by:narmi2
  • 4
  • 3
7 Comments
 
LVL 34

Expert Comment

by:flavo
ID: 16278796
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
 
LVL 1

Author Comment

by:narmi2
ID: 16278851
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
 
LVL 1

Author Comment

by:narmi2
ID: 16278904
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 34

Accepted Solution

by:
flavo earned 2000 total points
ID: 16278906
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
 
LVL 1

Author Comment

by:narmi2
ID: 16278917
Your method looks better than the one i posted.  Give me a few moments to test it out! :D
0
 
LVL 1

Author Comment

by:narmi2
ID: 16278943
That's perfect!

Thanks! :D
0
 
LVL 34

Expert Comment

by:flavo
ID: 16278963
:^)

Good luck with the rest of your project(s)

Dave
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question