Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

List files and folders - Dir$ (pattern)


Dim X As Collection
Set X = GetAllFiles( "c:\data" , "*.*" )
Print X.Count

Function GetAllFiles( ByVal path As String , ByVal filespec As String ) As Collection
  
    Dim file As Variant
    Dim folder As Variant
    Dim folders As New Collection
   
    ' initialize the result
    Set GetAllFiles = New Collection
   
    If Right$(path, 1) <> "\" Then path = path & " \ "
   
    'search path for files And folder
   file = Dir$(path & filespec, vbDirectory)
    Do While Len(file)
        If Left$(file, 1) <> "." Then   'ignore . And .. entries
           file = path & file
            If (GetAttr(file) And vbDirectory) <> 0 Then folders.Add file, file
           GetAllFiles.Add file, file
        End If
       file = Dir$
   Loop
   
    'get files in each folder
    For Each folder In folders
        For Each file In GetAllFiles(folder, filespec)
           GetAllFiles.Add file, file
        Next
    Next
   
End Function