Home
dbPix
Download
Order
Testimonials
Support
Tutorials
Samples
KnowledgeBase
Links
Revision History
Documentation
Search
Contact
Site Map

Graph: dbPix image storage vs OLE Embedding and Linking in Microsoft Access


    
The Access Image FAQ < Previous ArticleNext Article >

Recursive Folder Search - Find Files in Subdirectories


    
Sample Newsgroup Questions 
Access 2000 doing a recursive file search
Parsing Subdirectories
Using Nested Dir
Making a list of files from folders and subfolders
How can I list the files in a directory and sub directory
Move through directories
Looping through subfolders in a folder
Directories, recursive search in Access 97 / VBA
Files, recursively searching directories for Access 97


Sometimes it can be useful to find all the images beneath a certain point in the directory tree. For example, a common approach to storing digital photo files is to place the files in date folders according to the date the photo was taken, e.g.

Photo root:  c:\photos\
Photos taken on 15th January 2005:  c:\photos\2005-01-15\
Photos taken on 20th August 2006:  c:\photos\2006-08-20\


The 'Dir' function offers a convenient solution to finding files in a single folder, however, due to Dir's internal implementation it can't be called recursively.

The 'RecursiveDir' function below allows us to find all files beneath a certain point in the directory tree (or all .jpg files, for example). The code is based on an example by Albert Kallall, adapted by Allen Browne.


Usage Example:

    Dim colFiles As New Collection
    RecursiveDir colFiles, "C:\Photos", "*.jpg", True

    Dim vFile As Variant
    For Each vFile In colFiles
        Debug.Print vFile
    Next vFile

Output:

C:\Photos\2006-10-28\IMG_2851.JPG
C:\Photos\2006-10-28\IMG_2852.JPG
C:\Photos\2006-11-04\IMG_2853.JPG
C:\Photos\2006-11-04\IMG_2854.JPG
C:\Photos\2006-11-04\IMG_2855.JPG


Public Function RecursiveDir(colFiles As Collection, _
                             strFolder As String, _
                             strFileSpec As String, _
                             bIncludeSubfolders As Boolean)

    Dim strTemp As String
    Dim colFolders As New Collection
    Dim vFolderName As Variant

    'Add files in strFolder matching strFileSpec to colFiles
    strFolder = TrailingSlash(strFolder)
    strTemp = Dir(strFolder & strFileSpec)
    Do While strTemp <> vbNullString
        colFiles.Add strFolder & strTemp
        strTemp = Dir
    Loop

    If bIncludeSubfolders Then
        'Fill colFolders with list of subdirectories of strFolder
        strTemp = Dir(strFolder, vbDirectory)
        Do While strTemp <> vbNullString
            If (strTemp <> ".") And (strTemp <> "..") Then
                If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0 Then
                    colFolders.Add strTemp
                End If
            End If
            strTemp = Dir
        Loop

        'Call RecursiveDir for each subfolder in colFolders
        For Each vFolderName In colFolders
            Call RecursiveDir(colFiles, strFolder & vFolderName, strFileSpec, True)
        Next vFolderName
    End If

End Function


Public Function TrailingSlash(strFolder As String) As String
    If Len(strFolder) > 0 Then
        If Right(strFolder, 1) = "\" Then
            TrailingSlash = strFolder
        Else
            TrailingSlash = strFolder & "\"
        End If
    End If
End Function



Related Articles

Parse the Folder or Filename from a Full Path
How to display the Common 'Browse for Folder' Dialog to Choose a Folder




Imaging for Access that's Easy, Efficient & Fast
  • NO OLE Bloat
  • NO App Dependencies
  • NO Complex Coding
  • NO Performance Penalty
  •  DBPix logo
    Read More


    Microsoft and the Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries.