Function to Count the Files in a directory:   Return to List

Public Function CountFilesInDir(ByVal strPath As String) As String
' ----------------------------------------------------------------------
' Counts the files in the passed directory
' ----------------------------------------------------------------------
On Error GoTo BYE
' declare locals
Dim sfile As String
Dim intCount As Integer
intCount = 0
' prep pattern from passed path
strPathPattern = strPath & "\*.*"
' run the dir cmd using the pattern
sfile = Dir(strPathPattern)
If Len(sfile) > 0 Then
    ' note the initial file returned
    If sfile <> "." And sfile <> ". ." Then
        intCount = intCount + 1
    End If
    ' Do the remaining files
    Do While sfile <> ""
        sfile = Dir()
        If sfile <> "." And sfile <> ". ." Then
            intCount = intCount + 1
        End If
    Loop
End If
' return result
If intCount = 0 Then
    CountFilesInDir = intCount
Else
    CountFilesInDir = intCount - 1
End If
Exit Function
BYE:
CountFilesInDir = 0
End Function



Note to Webmaster