3

I have a function that does a recursive directory search for files but when I search a Drive I get Access denied errors which stops the search. How can I avoid these errors?

Here's the function I use:

lstSearch = GetFilesRecursive(FolderBrowserDialogMain.SelectedPath)

Private Function GetFilesRecursive(ByVal path As String) As List(Of String)
    Dim lstResult As New List(Of String)
    Dim stkStack As New Stack(Of String)
    stkStack.Push(path)
    Do While (stkStack.Count > 0)
        Dim strDirectory As String = stkStack.Pop
        Try
            lstResult.AddRange(Directory.GetFiles(strDirectory, "*.mp3"))
            Dim strDirectoryName As String
            For Each strDirectoryName In Directory.GetDirectories(strDirectory)
                stkStack.Push(strDirectoryName)
            Next
        Catch ex As Exception
        End Try
    Loop
    Return lstResult
End Function

Thanks for any solutions.

Hadi
  • 36,233
  • 13
  • 65
  • 124
NPS
  • 31
  • 1
  • 5
  • Why the `Stack`? – Trevor May 22 '17 at 02:10
  • `Catch ex As Exception` what's the point of catching an exception and do nothing about it? – Xaqron May 22 '17 at 02:11
  • Xaquon, I thought by not catching any errors it would continue to run through search. – NPS May 22 '17 at 02:14
  • Zaggler, I saw an example on recursion and used the code. It works great for everything besides drives. – NPS May 22 '17 at 02:16
  • 3
    There's no recursion there. Recursion requires that a method call itself, either directly or indirectly, and that's not happening there. You need to do a bit more research on recursion. – jmcilhinney May 22 '17 at 02:18
  • As for why the code doesn't do what you expect, you should debug it to find out. If you don't understand what the code does then you need to do the appropriate research to find out. SO is not the place to have people fix code that you didn't write and don't understand. – jmcilhinney May 22 '17 at 02:35
  • Since it says `Access Denied` error, have you made sure you have proper access rights to the location where you are searching your files? Have you tried running the application/solution as admin? – Malcolm Salvador May 22 '17 at 02:54

3 Answers3

8

Thanks for the code, it worked, but after taking a closer look, i found this single line would do the job:

myfiles = IO.Directory.GetFiles(strpath, "*.*", IO.SearchOption.AllDirectories)

just changing the search option from TopDirectoryOnly to AllDirectories. I always look to use native functions.

d_kennetz
  • 5,219
  • 5
  • 21
  • 44
Chema
  • 81
  • 1
  • 2
1

You can achieve this by looping over files and directories recursively and adding some try catch logic.

Public Class MainClass

    Private Function GetAllFiles(ByVal strPath As String) As List(Of String)

        Dim lst As New List(Of String)

        GetFiles(strPath, lst)

        Return lst
    End Function


    Public Sub GetFiles(ByVal strpath As String, ByRef lstfiles As List(Of String))



        Try

            Dim str As String() = IO.Directory.GetFiles(strpath, "*.*", IO.SearchOption.TopDirectoryOnly)
            'Get Current Directory files
            lstfiles.AddRange(str)

            'Loop  over sub-directories
            For Each strDirectory As String In IO.Directory.GetDirectories(strpath, "*.*", IO.SearchOption.TopDirectoryOnly)


                Me.GetFiles(strDirectory, lstfiles)


            Next

        Catch ex As UnauthorizedAccessException
            'Access Denied exception

        Catch ex1 As Exception
            'Other exceptions

        End Try

    End Sub



End Class
Hadi
  • 36,233
  • 13
  • 65
  • 124
0

The only thing I changed to avoid the Access Denied errors was to use Try/Catch for UnauthorizedAccessException and then I just didn't handle it. When it's done searching use lstResult anyway you like. I have this code in a BackgroundWorker so it doesn't mess with the UI.

    Dim lstResult As New List(Of String)
    Dim stkStack As New Stack(Of String)
    stkStack.Push(SearchSelectedPath)
    Do While (stkStack.Count > 0)
        Dim strDirectory As String = stkStack.Pop
        Try
            lstResult.AddRange(Directory.GetFiles(strDirectory, "*.mp3"))
            Dim strDirectoryName As String
            For Each strDirectoryName In Directory.GetDirectories(strDirectory)
                stkStack.Push(strDirectoryName)
            Next
        Catch ex As UnauthorizedAccessException

        End Try
    Loop

I searched my C/D Drive's in about a minute and a half and found near 150 MP3's in each of the drives with no errors.

NPS
  • 31
  • 1
  • 5