Count of Files in a Folder - File Extension Type wise

buvanamali

New member
Joined
Aug 26, 2016
Messages
4
Reaction score
0
Points
0
Excel Version(s)
2007
Dear Sir / Madam

I am a newbie to VBA macros. I found the following macro on the internet, which is very much useful, accurate and perfect. As I am not conversant with VBA, I request you to help me in getting the count of files in a folder file extension type wise as attached in the sample workbook.

Thanks in Advance

Buvanamali

Code:
Function CountFiles_FolderAndSubFolders(strFolder As String, Optional strExt As String) As Double
'Original Author: Ken Puls (www.excelguru.ca)
'http://www.excelguru.ca/content.php?139-Count-Files-%28with-a-specific-extension-or-not%29-in-a-folder-and-subfolders
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim objSubFolder As Object
Dim PosOfLastDot As Long
Dim IncludeSubFolders As Boolean

On Error GoTo EarlyExit
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
   
For Each objFile In objFolder.Files
    PosOfLastDot = InStrRev(objFile, ".") 'to find the position of the last dot
        If (objFile.Attributes And vbHidden) = False Then 'if you list your documents folder (windows 7). there is hidden folders with shortcut arrows, will be skipped, but with a message "Permission denied", and carry on
            If strExt = "" Or (UCase(Right(objFile.path, (Len(objFile.path) - InStrRev(objFile.path, ".")))) = UCase(strExt)) Then
                CountFiles_FolderAndSubFolders = CountFiles_FolderAndSubFolders + 1
            Else
                If Right(objFile, Len(objFile) - PosOfLastDot) = strExt Or (UCase(Right(objFile.path, (Len(objFile.path) - InStrRev(objFile.path, ".")))) = UCase(strExt)) Then
                    CountFiles_FolderAndSubFolders = CountFiles_FolderAndSubFolders + 1
                End If
            End If
        End If
    Next objFile

IncludeSubFolders = True 'change to False if you want to list only the parent folder

If IncludeSubFolders = True Then
 
    For Each objSubFolder In objFolder.SubFolders
        CountFiles_FolderAndSubFolders = CountFiles_FolderAndSubFolders + CountFiles_FolderAndSubFolders(objSubFolder.path, strExt)
    
    Next objSubFolder
End If

Exit Function
EarlyExit:
MsgBox Err.Description
Set objFile = Nothing
Set objSubFolder = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
On Error GoTo 0
End Function

Sub CountFiles()
'Original Author: Ken Puls (www.excelguru.ca)
'http://www.excelguru.ca/content.php?139-Count-Files-%28with-a-specific-extension-or-not%29-in-a-folder-and-subfolders
Dim dblCount As Double

dblCount = CountFiles_FolderAndSubFolders("D:\DESKTOP\Mali", "xltx") 'put your extention between the last 2 "". Will list all files if left like this, also change your folder
             Range("B23").Value = dblCount
             
        
End Sub
 

Attachments

  • Count of Files in a Folder Extn typewise.xlsb
    19.5 KB · Views: 16
Last edited by a moderator:
In cell C5 of your sheet, paste in this formula:
=CountFiles_FolderAndSubFolders($C$2,B5)
and copy down.
 
Thank you Sir. Working Fine. For knowledge purpose, can you please advise vba code for the same.
Thanks in adv.

Buvanamali
 
Code:
Sub blah()
Dim myPath As String
With Sheets("Sheet1")
  myPath = .Range("C2").Value    'or:
  'myPath = "D:\DESKTOP\Mali" 'adjusted as necessary, of course.
  For Each cll In .Range("B5:B27").Cells
    cll.Offset(, 1).Value = CountFiles_FolderAndSubFolders(myPath, cll.Value)
  Next cll
End With
End Sub
 
Dear Sir

Thanks a lot. Working Fine. One more request, in the above sample, the file extensions were given instead let the macro furnish the type of file extensions available and its count in a given folder. Further the macro may be designed to select a folder by the user.

Thanks in advance.

Buvanamali
 
It seems you think that this is a free code- writing service.
 
Dear Sir

Thanks a lot once again. Sorry for the disturbance.

Have a nice day.
Stay Safe

Buvanamali
 
I tried to use this code but it doesn't work properly. I wanted to count each file (by extension) by subfolder which works great if you only have 1 subfolder. If you have more than 1 folder, it counts, and adds up all the files and puts that number in the spread sheet. EG: One folder has 8 files, and the next folder has 3 files, so it puts 11 as the count in every row. I have tried a dozen changes, but nothing works. For example, if I try and zero out the file count before the next loop, it puts a zero in every row. Does anyone have a solution for this bug? BTW, I tried adjustments in the Function and in the sub routine - both give me 0 as the count. I used a msgbox in the Function to tell me the folder name and count - it worked and showed me that the loop does not break until all folders have been gone through. Any help would be greatly appreciated
 
Back
Top