Results 1 to 11 of 11

Thread: Recursive file searching

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    87

    Recursive file searching

    Hi.



    I have some code which recursively searches for files on a folder. The problem is I want it to change a boolean value once it has completed. I don't mean once it gets to the end of the sub, I mean once it's literally finished. I had to code it the way it is otherwise I would have gotten Exception errors and I need it to scan all the files it does (recycling bin etc) because it needs to do behaviour analysis which I have already done. (it already scans, just need that value thing).

    The code I have is:
    Code:
    Dim strFolders() As String = System.IO.Directory.GetDirectories(strPath)  
            Dim strFiles() As String = System.IO.Directory.GetFiles(strPath, strPattern)
    It's initialized like: RecursiveSearch("C:\Users\profilename\Desktop\foldertest\", "*.*", ListBox1)



    When all the files are added I need it to change a boolean value. You can't use things to count files as at some points I do it for the whole drive and you'd get a error. The boolean value will represent whether it's finished scanning or not.

    Cheers, this forum is really helpful and hopefully someone will be able to help! I've been trying for days. Tried resetting boolean at end of sub etc. By the way, the recursive file search runs on a thread, and yes I have tried making a check if it's alive but it switches when it recalls so that doesn't work.
    Last edited by visualBEER310; Sep 14th, 2014 at 12:10 PM.

  2. #2
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: Recursive file searching

    What you might try is have a global variable that is used as a counter. Make it a long variable which would handle 9 e+18, which should be plenty big.

    Start out with a counter = 1 for the starting folder, and decrement it when you process a folder. Add the count of subfolders to the the counter. At the end of the recursive subroutine, if the counter = 0, you have exhausted all the folders on the drive.

    I hope that helps.

  3. #3
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: Recursive file searching

    The code that I imagine would be something like the following:
    Code:
    Class Search
    dim ctr as long = 1
    dim finishflag as boolean = false
    
    sub RecursiveSearch (strFolder as string, strPattern as string, lstTarget as listbox) 
    
    Dim strFolders() As String = System.IO.Directory.GetDirectories(strPath)  
            Dim strFiles() As String = System.IO.Directory.GetFiles(strPath, strPattern)  
      
            cnt = cnt - 1
            'Add the files  
            For Each strFile As String In strFiles  
                Try  
                    ListBox1.Items.Add(strFile.ToString())  
                    ' total = ListBox1.Items.Count  
                    ' ListBox1.Refresh()  
                Catch ex As Exception  
                End Try  
            Next  
      
            'Look through the other folders  
    cnt = cnt + strfolders.count
            For Each strFolder As String In strFolders  
                Try  
                    'Call the procedure again to perform the same operation  
                    RecursiveSearch(strFolder, strPattern, lstTarget)  
      
                    '    ListBox1.Refresh()  
                Catch ex As Exception  
                End Try  
            Next
    if cnt = 0 then finishflag = true
    end sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    87

    Re: Recursive file searching

    Thanks, that was really helpful. I'll try it now and see if it works and report back the results

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    87

    Re: Recursive file searching

    Next

    Catch ex As Exception


    processedfiles.Text += 1

    Dim creationtime As String = fi.CreationTime

    Catch ex As Exception
    End Try


    watch.Reset()
    End If[/CODE]
    Last edited by visualBEER310; Sep 14th, 2014 at 12:11 PM.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    87

    Re: Recursive file searching

    Quote Originally Posted by John_SC View Post
    The code that I imagine would be something like the following:
    Code:
    Class Search
    dim ctr as long = 1
    dim finishflag as boolean = false
    
    sub RecursiveSearch (strFolder as string, strPattern as string, lstTarget as listbox) 
    
    Dim strFolders() As String = System.IO.Directory.GetDirectories(strPath)  
            Dim strFiles() As String = System.IO.Directory.GetFiles(strPath, strPattern)  
      
            cnt = cnt - 1
            'Add the files  
            For Each strFile As String In strFiles  
                Try  
                    ListBox1.Items.Add(strFile.ToString())  
                    ' total = ListBox1.Items.Count  
                    ' ListBox1.Refresh()  
                Catch ex As Exception  
                End Try  
            Next  
      
            'Look through the other folders  
    cnt = cnt + strfolders.count
            For Each strFolder As String In strFolders  
                Try  
                    'Call the procedure again to perform the same operation  
                    RecursiveSearch(strFolder, strPattern, lstTarget)  
      
                    '    ListBox1.Refresh()  
                Catch ex As Exception  
                End Try  
            Next
    if cnt = 0 then finishflag = true
    end sub
    Wait, there seems to be a problem. Basically, the problem is that it seems to only get the files in the first directory (root directory) but it gets them over and over about 30 - 50 times and doesn't add any files in the other subfolders etc. Any ideas?

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Recursive file searching

    Hello,

    Here is a working example for recursively iterating a selected folder and sub-folders for a specific file type and can be modified to look for a file via a full name. Think of this code as a base that can be modified as needed. Lastly the searching works in a backgrounder worker that provides the ability to cancel and report progress.

    V2012 project.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    87

    Re: Recursive file searching

    Quote Originally Posted by kevininstructor View Post
    Hello,

    Here is a working example for recursively iterating a selected folder and sub-folders for a specific file type and can be modified to look for a file via a full name. Think of this code as a base that can be modified as needed. Lastly the searching works in a backgrounder worker that provides the ability to cancel and report progress.

    V2012 project.
    Thank you, I'll look at it now

  9. #9
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Recursive file searching

    i do not really understand what the problem is. you say
    It's initialized like: RecursiveSearch("C:\Users\profilename\Desktop\foldertest\", "*.*", ListBox1)
    so its finished once this line of code has executed. no?

    John_SC's idea is missing something: it needs to increase the count on the function entry and decrease on function return and only set the flag when after the decrease the counter is 0. so the counter will actually count the recursion depth. this can as well be done using a static var instead of a global var.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Aug 2014
    Posts
    87

    Re: Recursive file searching

    Quote Originally Posted by digitalShaman View Post
    i do not really understand what the problem is. you say


    so its finished once this line of code has executed. no?

    John_SC's idea is missing something: it needs to increase the count on the function entry and decrease on function return and only set the flag when after the decrease the counter is 0. so the counter will actually count the recursion depth. this can as well be done using a static var instead of a global var.
    No. It calls RecurseSearch inside of the method recursively for each Folder to add the files in the folder. Look at the method and you'll see after files are added in strPath it calls itself and changes strPath to the new folder found in the directory and it will do this for every folder in the root directory set as strPath to start it off.

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Recursive file searching

    Quote Originally Posted by visualBEER310 View Post
    No. It calls RecurseSearch inside of the method recursively for each Folder to add the files in the folder. Look at the method and you'll see after files are added in strPath it calls itself and changes strPath to the new folder found in the directory and it will do this for every folder in the root directory set as strPath to start it off.
    No, a recursive search of folders will return to the original caller when all folders have been searched. e.g.

    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ListBox1.Items.Clear()
            SearchFolders(startFolder, thePattern, ListBox1)
        End Sub
    
        Dim startFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        Dim thePattern As String = "*.*"
    
        Private Sub SearchFolders(folder As String, pattern As String, aListBox As ListBox)
            Try
                Dim strFolders() As String = System.IO.Directory.GetDirectories(folder)
                Dim strFiles() As String = System.IO.Directory.GetFiles(folder, pattern)
                Me.Invoke(Sub()
                              aListBox.Items.Add(folder)
                          End Sub)
                Try
                    For Each f As String In strFiles
    
                    Next
                    For Each d As String In strFolders
                        SearchFolders(d, pattern, aListBox)
                    Next
                Catch ex As Exception
                    Debug.WriteLine(ex.Message)
                    'Stop
                End Try
    
            Catch unauth As UnauthorizedAccessException
                Debug.WriteLine(unauth.Message)
                'Stop
            Catch ex As Exception
                Debug.WriteLine(ex.Message)
                'Stop
            End Try
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width