Applecore Pages on Microsoft Access

Counting The Number Of Files In A Directory

Normally, when doing file handling from within Access, you do not need to know the number of files within a directory, you merely loop through each file until there are no more left. However, if you wish to count the files, you will need to use a function such as this:

Function fCountFilesInDir(strDir As String, Optional varFileType As Variant) As Long
'   A function to count the number of files in a directory (excluding directories)
'   Accepts:
'       strDir - the directory to count the files in, i.e. "C:\Folder\"
'       varFileType (optional) - a file extension if you only wish to count certain file types, i.e. "exe"
'   Returns:
'       The number of files in the directory

    On Error GoTo E_Handle
    Dim strFile As String
    Dim lngCount As Long
    strFile = Dir(strDir, vbNormal)
    Do While strFile <> ""
        If Not IsMissing(varFileType) Then
            If Right(strFile, Len(varFileType)) = varFileType Then lngCount = lngCount + 1
        Else
            lngCount = lngCount + 1
        End If
        strFile = Dir
    Loop
    fCountFilesInDir = lngCount
fExit:
    Exit Function
E_Handle:
    MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
    Resume fExit
End Function

Top

 


HOME | NEW | TABLES | QUERIES | FORMS | REPORTS | GENERAL | API | DOWNLOADS | TUTORIAL | RESOURCES
E-MAIL
Copyright & Disclaimer

 

Last modified at 06/06/2006 14:55:45