Results 1 to 20 of 20

Thread: count files extensions files for each month based on modified date

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Administrator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,493
    Location
    Maybe this might do what you want?

    Sub CountFileExtensionsByMonth()
        Dim fso As Object, folder As Object, subFolder As Object, file As Object
        Dim wb As Workbook, ws As Worksheet, newWs As Worksheet
        Dim dict As Object, ext As String, monthYear As String
        Dim rowNum As Long
        Dim FolderPath As String
        ' Initialize FileSystemObject and Dictionary
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set dict = CreateObject("Scripting.Dictionary")
        Set wb = ThisWorkbook ' This workbook
        ' Prompt the user for the folder path
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Select the folder to process"
            .AllowMultiSelect = False
            If .Show = True Then
                FolderPath = .SelectedItems(1)
            Else
                MsgBox "Folder selection cancelled.", vbCritical
                Exit Sub
            End If
        End With
        If Right(FolderPath, 1) <> "\" Then FolderPath = FolderPath & "\"
        ' Create a new worksheet for the results
        Set newWs = wb.Sheets.Add
        newWs.Name = "File Extension Counts"
        ' Add headers to the worksheet
        newWs.Cells(1, 1).Value = "Month/Year"
        newWs.Cells(1, 2).Value = "Extension"
        newWs.Cells(1, 3).Value = "Count"
        rowNum = 2 ' Start writing data from row 2
        ' Recursive function to process folders and subfolders
        Sub ProcessFolder(ByRef folderPath As String)
            Dim currentFolder As Object, currentFile As Object, subFolderObj As Object
            Set currentFolder = fso.GetFolder(folderPath)
            ' Loop through each file in the current folder
            For Each currentFile In currentFolder.Files
                ext = LCase(fso.GetExtensionName(currentFile.Path)) ' Get and lowercase the extension
                monthYear = Format(currentFile.DateLastModified, "YYYY-MM") ' Get month and year
                If ext <> "" Then 'avoid counting files with no extension.
                    If Not dict.exists(monthYear & "|" & ext) Then
                        dict.Add monthYear & "|" & ext, 1
                    Else
                        dict(monthYear & "|" & ext) = dict(monthYear & "|" & ext) + 1
                    End If
                End If
            Next currentFile
            ' Recursively process subfolders
            For Each subFolderObj In currentFolder.SubFolders
                ProcessFolder subFolderObj.Path
            Next subFolderObj
        End Sub
    End Sub
    Last edited by Aussiebear; 05-10-2025 at 09:23 PM.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •