VBA counting & listing files in a folder

RoundRocket

New Member
Joined
Nov 28, 2013
Messages
24
Below is a handy bit of code in Excel 2007 which simply lists all the files it finds in a folder in the first column of the worksheet, I'd like to somehow adapt this code to just 'count' the types of files in the folder so that it looks to a folder and reports back (see below 'desired result' for example), perhaps printing it to a .txt file instead of adding the files to a worksheet...? i cant see how to do this.

desired result:
There are 23 .docs files in the folder
There are 10 .pdf files in the folder
There are 19 .xls files in the folder
There are 120 .docx files in the folder

Any help you may be able to give on this would be awesome. Also, i do hope the code below is useful to anyone else that might be looking for a 'list files in folder' macro.
Thank you.


Sub Button3_Click()
Call ListDocumentFiles


End Sub


Sub ListDocumentFiles()
Dim fso As New FileSystemObject
Dim fle As file
Dim fldr As folder
Dim i As Integer

Call ThisWorkbook.Worksheets(2).Range("A:B").Clear
With Application.FileDialog(msoFileDialogFolderPicker)
Call .Show
Set fldr = fso.GetFolder(.SelectedItems(1))
End With


For Each fle In fldr.Files
If InStr(1, fle.Name, ".", vbTextCompare) <> 0 Then
Let i = i + 1
Let ThisWorkbook.Worksheets(2).Cells(i, 1) = fle.Name
End If
Next


MsgBox ("Search complete")


Set fle = Nothing
Set fldr = Nothing
Set fso = Nothing


End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this, it doesn't save to a text file as you requested, but that part shouldn't be too hard to add.

Code:
Sub ListDocumentFiles()
    Dim fso As New FileSystemObject
    Dim fle As file
    Dim fldr As folder
    Dim i As Long
    
    Call ThisWorkbook.Worksheets(1).Range("A:A").Clear
    With Application.FileDialog(msoFileDialogFolderPicker)
        Call .Show
        Set fldr = fso.GetFolder(.SelectedItems(1))
    End With
    
    Dim FileExtArr() As String 'stores unique file extensions
    Dim FileCntArr() As Long 'stores counts of files
    For Each fle In fldr.Files
        'get the file extension
        FileExt = Right(fle.Name, Len(fle.Name) - InStr(1, fle.Name, ".", vbTextCompare))
        
        'file has extension
        If FileExt <> "" Then
            'extension not in array
            If InStr(1, Join(FileExtArr, ","), FileExt) = 0 Then
                'expand the array
                'if array isn't empty
                If Join(FileExtArr) <> "" Then
                    ReDim Preserve FileExtArr(UBound(FileExtArr) + 1)
                    ReDim Preserve FileCntArr(UBound(FileCntArr) + 1)
                Else
                    'array is empty so dim to 0
                    ReDim FileExtArr(0)
                    ReDim FileCntArr(0)
                End If
                'save new extension and count of 1
                FileExtArr(UBound(FileExtArr)) = FileExt
                FileCntArr(UBound(FileCntArr)) = 1
            Else
                'find the extension in the array
                ArrIndex = Application.WorksheetFunction.Match(FileExt, FileExtArr(), 0)
                'add 1 to the count
                FileCntArr(ArrIndex - 1) = FileCntArr(ArrIndex - 1) + 1
            End If
        End If
    Next

    'save the extension list to the worksheet
    For i = 0 To UBound(FileExtArr)
        If FileCntArr(i) = 1 Then
            ThisWorkbook.Worksheets(1).Cells(i + 1, 1) = "There is " & FileCntArr(i) & " ." & FileExtArr(i) & " file in the folder"
        Else
            ThisWorkbook.Worksheets(1).Cells(i + 1, 1) = "There are " & FileCntArr(i) & " ." & FileExtArr(i) & " files in the folder"
        End If
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,390
Messages
6,124,670
Members
449,178
Latest member
Emilou

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top