Question Latest "best/fastest" way to search for files?

J. Scott Elblein

Well-known member
Joined
Dec 14, 2006
Messages
165
Location
Chicago
Programming Experience
10+
Hey all,

Been a while since I was dev'ing in VB/C# and wondering if anything has changed for the better that I haven't heard about yet, when it comes to searching a hard drive for files.

Context:

I'm about to start developing a "cleaner" type of app that scours hard drives for specific file types, and also containing strings in the file name itself, so I may also need to add some RegEx'ing for each file name as well (we'll see). Naturally, with the TB sized drives these days, this could take a LONG time, so I'm wondering if there's a super-speedy way to do this? I'm trying to get this as close to "instant" as possible. ;) Perhaps searching through the Shadow Copies, or something?

TIA
 
Last edited:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,602
Location
Norway
Programming Experience
10+

J. Scott Elblein

Well-known member
Joined
Dec 14, 2006
Messages
165
Location
Chicago
Programming Experience
10+
Thanks @JohnH

Naturally, the devs of the .NET framework want to make us work out their puzzles for them, :rolleyes:, lol ...

As soon as I give a quick test run of that method:

Search test:
    Sub Main()

        Dim strSrcPath As String = "D:\"
        Dim strExtension As String = "*.po"
        
        '
        SearchForFiles1(strSrcPath, True, strExtension)
        
    End Sub

' Search for Files
Private Sub SearchForFiles1(strSrcPath As String, bSearchSubDirectories As Boolean, Optional strExtension As String = "*")

    Try
        Dim txtFiles = Directory.EnumerateFiles(strSrcPath, strExtension, If(bSearchSubDirectories = True, SearchOption.AllDirectories, SearchOption.TopDirectoryOnly))

        For Each currentFile As String In txtFiles

            Dim fi As FileInfo = New FileInfo(currentFile)
            
            ' Show the file name
            fi.Name

        Next

    Catch e As Exception
        Console.WriteLine(e.Message)
    End Try

End Sub

I'm almost immediately hit with:

Access to the path 'D:\System Volume Information' is denied.

and since it's an exception, it halts further enumeration.

Have you ever dealt with this, and figured out a workaround?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,602
Location
Norway
Programming Experience
10+
If there are inaccessible files/folders you have to EnumerateDirectories yourself recursively with Try-Catch.
VB.NET:
Private Sub SearchFiles(path As String)
    For Each folder In IO.Directory.EnumerateDirectories(path)
        Try
            SearchFiles(folder)
        Catch uaex As UnauthorizedAccessException
            Debug.WriteLine(uaex.Message)
        End Try
    Next
    For Each file In IO.Directory.EnumerateFiles(path)
        Dim name = IO.Path.GetFileName(file)
        
    Next
End Sub
For .Net Core and .Net 5/6 however there is the nice addition EnumerationOptions.IgnoreInaccessible.
 
Top Bottom