counting files in folder and subfolders

Hi, i put this code into vba in access ,
The function works but it counts also the hidden files which i don`t want.
how can i edit the code to let me count the files in folder and subfolder witout counting the hidden/system files ?
Function CountFiles_FolderAndSubFolders(strFolderAs String, Optional strExt As String = "*.*") As Double
'Author         : Ken Puls (www.excelguru.ca)
'Function purpose: To count files ina folder and all subfolders.  If a file extension is provided,
'   then count only files of thattype, otherwise return a count of all files.
'Stop
    Dim objFso As Object
    Dim objFiles As Object
    Dim objSubFolder As Object
    Dim objSubFolders As Object
    Dim objFile As Object
'Dim fffFiles2 As Double

    'Set Error Handling
    On Error GoTo EarlyExit

    'Create objects to geta count of files in the directory
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set objFiles = objFso.GetFolder(strFolder).Files
    For Each objFile In objFiles
  '      If objFile.AttributesAnd Hidden Then
  '      fffFiles2= objFiles.Count - 1
  '      End If
  '  Next
    Set objSubFolders = objFso.GetFolder(strFolder).SubFolders

    'Count files (that matchthe extension if provided)
    If strExt = "*.*"Then
        CountFiles_FolderAndSubFolders= objFiles.Count
    Else
        For EachobjFile In objFiles
If UCase(Right(objFile.Path, (Len(objFile.Path)- InStrRev(objFile.Path, ".")))) = UCase(strExt) Then
               CountFiles_FolderAndSubFolders = CountFiles_FolderAndSubFolders+ 1
           End If
        Next objFile
    End If

    'Request count of filesin subfolders
    For Each objSubFolderIn objSubFolders
        CountFiles_FolderAndSubFolders= CountFiles_FolderAndSubFolders + _
        CountFiles_FolderAndSubFolders(objSubFolder.Path,strExt)
    Next objSubFolder

EarlyExit:
    'Clean up
    'On Error Resume Next
    Err.Description
    Set objFile = Nothing
    Set objFiles = Nothing
    Set objFso = Nothing
    On Error GoTo 0

End Function

Open in new window

drtopservAsked:
Who is Participating?
 
Bill PrewCommented:
Correcting a couple of errors in that code, that existed before I added to it, and one error in my attribute checking:

Function CountFiles_FolderAndSubFolders(strFolder As String, Optional strExt As String = "*.*") As Double
'Author         : Ken Puls (www.excelguru.ca)
'Function purpose: To count files ina folder and all subfolders.  If a file extension is provided,
'   then count only files of thattype, otherwise return a count of all files.
'Stop
    Dim objFso As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim objSubFolder As Object

    'Set Error Handling
    On Error GoTo EarlyExit

    'Create objects to geta count of files in the directory
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFso.GetFolder(strFolder)

    'Count all matching files (excluding hidden or system files)
    For Each objFile In objFolder.Files
       If (objFile.Attributes And 6) = 0 Then
           If strExt = "*.*" Or (UCase(Right(objFile.Path, (Len(objFile.Path) - InStrRev(objFile.Path, ".")))) = UCase(strExt)) Then
               CountFiles_FolderAndSubFolders = CountFiles_FolderAndSubFolders + 1
           End If
       End If
    Next objFile

    'Request count of filesin subfolders
    For Each objSubFolder In objFolder.SubFolders
        CountFiles_FolderAndSubFolders = CountFiles_FolderAndSubFolders + _
                                         CountFiles_FolderAndSubFolders(objSubFolder.Path, strExt)
    Next objSubFolder
    Exit Function

EarlyExit:
    'Clean up
    'On Error Resume Next
    MsgBox Err.Description
    Set objFile = Nothing
    Set objSubFolder = Nothing
    Set objFolder = Nothing
    Set objFso = Nothing
    On Error GoTo 0

End Function

Open in new window

~bp

0
 
Rey Obrero (Capricorn1)Commented:

try this test

    For Each objFile In objFiles
       If objFile.Attributes And 2 Then   ' << test for hidden file
       fffFiles2= objFiles.Count - 1
       End If
    Next


0
 
drtopservAuthor Commented:
nope , not working!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Bill PrewCommented:
Give this a try, I think it should do what you want, and is a little simpler.

Function CountFiles_FolderAndSubFolders(strFolderAs String, Optional strExt As String = "*.*") As Double
'Author         : Ken Puls (www.excelguru.ca)
'Function purpose: To count files ina folder and all subfolders.  If a file extension is provided,
'   then count only files of thattype, otherwise return a count of all files.
'Stop
    Dim objFso As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim objSubFolder As Object

    'Set Error Handling
    On Error GoTo EarlyExit

    'Create objects to geta count of files in the directory
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFso.GetFolder(strFolder)

    'Count all matching files (excluding hidden or system files)
    for Each objFile In objFolder.Files
       If Not objFile.Attributes And 6 Then
           If strExt = "*.*" Or (UCase(Right(objFile.Path, (Len(objFile.Path) - InStrRev(objFile.Path, ".")))) = UCase(strExt)) Then
               CountFiles_FolderAndSubFolders = CountFiles_FolderAndSubFolders + 1
           End If
       End If
    Next objFile

    'Request count of filesin subfolders
    For Each objSubFolder In objFolder.SubFolders
        CountFiles_FolderAndSubFolders = CountFiles_FolderAndSubFolders + _
                                         CountFiles_FolderAndSubFolders(objSubFolder.Path,strExt)
    Next objSubFolder

EarlyExit:
    'Clean up
    'On Error Resume Next
    Err.Description
    Set objFile = Nothing
    Set objSubFolder = Nothing
    Set objFolder = Nothing
    Set objFso = Nothing
    On Error GoTo 0

End Function

Open in new window

~bp
0
 
drtopservAuthor Commented:
I`ll check this code in the coming 3 days:}
0
 
drtopservAuthor Commented:
works,.
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.