VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 300,414 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,600 people online right now. Registration is fast and FREE... Join Now!




Recursive Search problem

 

Recursive Search problem

Ameel

2 Jul, 2009 - 01:18 PM
Post #1

D.I.C Head
**

Joined: 19 Jun, 2008
Posts: 160



Thanked: 3 times
My Contributions
hi guys,

code as follows
CODE

    Private Function RecursiveSearch1(ByVal path As String) As Boolean
        On Error Resume Next
        Dim lookfor As String = TextBox1.Text
        Dim extensions As String() = lookfor.Split(New Char() {";"c})
        Dim myfileinfos As New ArrayList()
        Dim dirInfo As New IO.DirectoryInfo(path)
        Dim driinfo As New IO.DriveInfo(path)

        For Each ext As String In extensions

            Dim aryFi As IO.FileInfo() = dirInfo.GetFiles(ext)
            Dim fi As IO.FileInfo

            For Each fi In aryFi
                tempstring = fi.Name
                AccessControl2Copy()
            Next
        Next

        Dim aryFol As IO.DirectoryInfo() = dirInfo.GetDirectories
        Dim di As IO.DirectoryInfo

        For Each di In aryFol
            RecursiveSearch1(di.FullName)
        Next

        Return True

    End Function

    Private Sub AccessControl2Copy()
        If Me.InvokeRequired Then
            Me.Invoke(New MethodInvoker(AddressOf AccessControl2Copy))
        Else

            ListBox1.Items.Add(tempstring)

        End If
    End Sub

When I run with path being a folder, all is good. But if I run with path being a drive instead of a folder in a drive, once the search hits the last file, it keeps repeating "ListBox1.Items.Add(tempstring)". I can't figure out why... Any1 knows? Again, If I select a folder only, and there are thousands of subsubsubsubsub folders, its fine. But as soon as I select a drive, the problem occurs...

please help asap

cheers

User is offlineProfile CardPM
+Quote Post


Ameel

RE: Recursive Search Problem

2 Jul, 2009 - 01:39 PM
Post #2

D.I.C Head
**

Joined: 19 Jun, 2008
Posts: 160



Thanked: 3 times
My Contributions
Btw. This is my first attempt at recursive. there're a few unused codes in there, so dnt worry about these....

also, maybe there's a better way to do it, but that I am unaware of; basically i want to be able to search and list all files within a directory/drive and sub-directories with particular extensions that can be changed.

cheers

This post has been edited by Ameel: 2 Jul, 2009 - 01:40 PM
User is offlineProfile CardPM
+Quote Post

LoveIsNull

RE: Recursive Search Problem

2 Jul, 2009 - 04:02 PM
Post #3

Disbanding my Ignorance
****

Joined: 10 Mar, 2009
Posts: 536



Thanked: 45 times
My Contributions
My first suggestion would be to not use "On Error Resume Next" in .Net, this is retained for backwards compatibility but there is little if any reason to continue using it because vb.net features structured exception handling.
It works like:
CODE
Try
        'Your code
        'Goes here
Catch ex As Exception
        Debug.WriteLine(ex.ToString)
End Try


Also, this code seems like it would be slow, and here especially:
CODE
        For Each ext As String In extensions

            Dim aryFi As IO.FileInfo() = dirInfo.GetFiles(ext)
            Dim fi As IO.FileInfo

            For Each fi In aryFi
                tempstring = fi.Name
                AccessControl2Copy()
            Next
        Next

I don't see why you have to re-iterate through the same directory to get files of each type. You should be able to simply run through it once and check each file extension for the ones that you are looking for. But really, all in all, yes there is a better way to do this.
I personally would use two methods, one to do the recursion through directories and another to just get the files contained within. Like in this little example:

CODE
    Dim strExts() As String = {".jpg", ".jpeg", ".png", ".bmp", ".gif", ".tiff"}

    Public Sub GetDirs(ByVal path As String, Optional ByVal getSubDirs As Boolean = True)
        Try
            For Each subDir As String In Directory.GetDirectories(path)
                GetFiles(subDir)

                If getSubDirs Then
                    GetDirs(subDir)
                End If
            Next
        Catch ex As Exception
            Debug.WriteLine(ex.ToString)
        End Try
    End Sub

    Public Sub GetFiles(ByVal path As String)
        Try
            For Each strFile As String In Directory.GetFiles(path)
                Dim fInfo As New FileInfo(strFile)
                'If fInfo.Extension = ".png" Then
                If strExts.Contains(fInfo.Extension) Then
                    lstFiles.Items.Add(fInfo.FullName)
                End If
            Next
        Catch ex As Exception
            Debug.WriteLine(ex.ToString)
        End Try
    End Sub

Just have a listbox named "lstFiles" on the form and a button to start it like so:
CODE
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        GetFiles("C:\Users") 'Get the files in this path
        GetDirs("C:\Users") 'Start recursion
    End Sub


See if you can learn from that and adapt it to fit your needs.
By the way, make sure to put this line at the very top of your source file so you don't have to keep typing "[System.]IO.-":
CODE
Imports System.IO    'This has to be there

Public Class Form1
    '-----------------------------------
End Class


This post has been edited by LoveIsNull: 2 Jul, 2009 - 04:09 PM
User is offlineProfile CardPM
+Quote Post

Ameel

RE: Recursive Search Problem

5 Jul, 2009 - 10:21 AM
Post #4

D.I.C Head
**

Joined: 19 Jun, 2008
Posts: 160



Thanked: 3 times
My Contributions
thanks for all that. will try them out now. ch33rs
User is offlineProfile CardPM
+Quote Post

Ameel

RE: Recursive Search Problem

5 Jul, 2009 - 11:02 AM
Post #5

D.I.C Head
**

Joined: 19 Jun, 2008
Posts: 160



Thanked: 3 times
My Contributions
Ok. I tried the code. Works orsm. Just that, when I select a drive. After It cathes an error, it basically fails. It doesn't search anymore.

Hence when a drive is selected, there is no permission to access "System Volume Information". And the code stops completely after it catches that error. How do I get around that other than by using "on error resume next", so that the program keeps retrieving other folders?

cheers
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008

RE: Recursive Search Problem

5 Jul, 2009 - 01:04 PM
Post #6

The Bandido Coder
Group Icon

Joined: 29 May, 2008
Posts: 2,686



Thanked: 155 times
Dream Kudos: 3925
Expert In: vb.net, LINQ

My Contributions
Suggest you examine the tutorial: Finding Files
User is offlineProfile CardPM
+Quote Post

Ameel

RE: Recursive Search Problem

7 Jul, 2009 - 11:53 AM
Post #7

D.I.C Head
**

Joined: 19 Jun, 2008
Posts: 160



Thanked: 3 times
My Contributions
thanks Adam. Good stuff
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 11:23PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month