Solved How many itens in folder

sergio vieira

Member
Local time
Today, 11:53
Joined
Apr 22, 2024
Messages
35
Good afternoon. I have a folder located in My Documents called "Backups". When I open the folder, I need it to inform me of how many backups are in the folder and, if there are more than 6, notify me to delete some. Thank you very much.
 
Will there be other files in there or just backups?
I have Google on this computer.
See if any of these help?

If you are extremely lazy ask CHATGPT to write it for you.
Code:
Function CountFilesInFolder(ByVal FolderPath As String) As Integer
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim FileCount As Integer
    
    ' Create FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    ' Check if folder exists
    If objFSO.FolderExists(FolderPath) Then
        Set objFolder = objFSO.GetFolder(FolderPath)
        FileCount = objFolder.Files.Count
    Else
        FileCount = -1 ' Return -1 if folder does not exist
    End If
    
    ' Return file count
    CountFilesInFolder = FileCount
    
    ' Clean up
    Set objFolder = Nothing
    Set objFSO = Nothing
End Function
 

Users who are viewing this thread

Back
Top Bottom