Make DirectoryInfo.GetFiles Method Ignore Hidden Files/Folders

Is there an easy way to make DirectoryInfo.GetFiles Method Ignore Hidden Files/Folders?
Ray TurnerSenior ConsultantAsked:
Who is Participating?
 
NorieVBA ExpertCommented:
Ray

Don't know if it's an easy way but you could use LINQ to filter out the hidden files/folders.
Dim di As DirectoryInfo = New DirectoryInfo("C:\test\")
Dim allfl() As FileInfo

        allfl = di.GetFiles()

        Dim visfl = From f In allfl Where f.Attributes.HasFlag(FileAttributes.Hidden) = False

Open in new window

0
 
it_saigeDeveloperCommented:
Norie, is spot on, neither GetFiles nor EnumerateFiles offer a method which will exclude files of specific attributes (you can exclude/include files using a search filter but that matches based on name/extension).  In order to filter by attribute you will have to filter the resulting set of files returned from the methods.  Using GetFiles will block until all files are returned while using EnumerateFiles will return an Enumerated collection.

Example implementation:
Imports System.IO

Module Module1
    Sub Main()
        Dim files = GetFiles("C:\_admin", FileAttribute.Hidden)
        Dim files2 = New DirectoryInfo("C:\_admin").EnumerateFiles().Where(Function(file) (file.Attributes And FileAttribute.Hidden) = 0)

        For Each file In files
            Console.WriteLine(file)
        Next

        For Each file In files2
            Console.WriteLine(file)
        Next

        Console.ReadLine()
    End Sub

    Iterator Function GetFiles([directory] As String, exclude As FileAttribute) As IEnumerable(Of FileInfo)
        For Each file In New DirectoryInfo([directory]).GetFiles()
            If (file.Attributes And exclude) = 0 Then
                Yield file
            End If
        Next
    End Function
End Module

Open in new window

For the following folder:Capture.PNGProduces the following output:Capture.PNGChanging the second call to only include hidden files:
Imports System.IO

Module Module1
    Sub Main()
        Dim files = GetFiles("C:\_admin", FileAttribute.Hidden)
        Dim files2 = New DirectoryInfo("C:\_admin").EnumerateFiles().Where(Function(file) (file.Attributes And FileAttribute.Hidden) = FileAttribute.Hidden)

        For Each file In files
            Console.WriteLine(file)
        Next

        For Each file In files2
            Console.WriteLine(file)
        Next

        Console.ReadLine()
    End Sub

    Iterator Function GetFiles([directory] As String, exclude As FileAttribute) As IEnumerable(Of FileInfo)
        For Each file In New DirectoryInfo([directory]).GetFiles()
            If (file.Attributes And exclude) = 0 Then
                Yield file
            End If
        Next
    End Function
End Module

Open in new window

Produces the following output -Capture.PNG
-saige-
0
 
Ray TurnerSenior ConsultantAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.