Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2748
  • Last Modified:

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

0
drtopserv
Asked:
drtopserv
  • 3
  • 2
1 Solution
 
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
 
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
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now