Link to home
Start Free TrialLog in
Avatar of kbay808
kbay808Flag for United States of America

asked on

VBA-How to modify code to count only files in a folder with a specific date?

The current code that I use works great, but I need to modify it for a different task.  The current code will count all of the files of a file type in a folder, but I need it to only count the files from a certain date provided in sheet1 range A1.

Private Sub CountFilesInFolder(strDir As String, Optional strType As String)
'DEVELOPER: Ryan Wells (wellsr.com)
'DESCRIPTION: This macro counts the files in a folder and retuns the result in a msgbox
'INPUT: Pass the procedure a string with your directory path and an optional
' file extension with the * wildcard
'EXAMPLES: Call CountFilesInFolder("C:\Users\Ryan\")
' Call CountFilesInFolder("C:\Users\Ryan\", "*txt")
    Dim file As Variant, i As Integer
    If Right(strDir, 1) <> "\" Then strDir = strDir & "\"
    file = Dir(strDir & strType)
    While (file <> "")
        i = i + 1
        file = Dir
    Wend
    'MsgBox i
    Count = i
End Sub

Open in new window

Avatar of Norie
Norie

Is it the date the file was created, the data it was last changed or last accessed?
Avatar of kbay808

ASKER

The date it was created, but none of the files are modified after created.
Private Sub CountFilesInFolder(strDir As String, Optional strType As String)
'DEVELOPER: Ryan Wells (wellsr.com)
'DESCRIPTION: This macro counts the files in a folder and retuns the result in a msgbox
'INPUT: Pass the procedure a string with your directory path and an optional
' file extension with the * wildcard
'EXAMPLES: Call CountFilesInFolder("C:\Users\Ryan\")
' Call CountFilesInFolder("C:\Users\Ryan\", "*txt")
    Dim file As Variant, i As Integer
    If Right(strDir, 1) <> "\" Then strDir = strDir & "\"
    file = Dir(strDir & strType)
    While (file <> "")
        i = i + 1

         If Format(FileDateTime(strDir & File), "dd/mm/yyyy") = "01/01/2020" Then
            filedateCount = filedateCount + 1
        End If


        file = Dir
    Wend
    'MsgBox i
    Count = i
End Sub
You could try this function.
Function CountFilesInFolder(strDir As String, Optional strType As String, Optional dt As Date) As Long
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim cnt As Long

    If IsMissing(dt) Then
        dt = Date
    End If
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Set objFolder = objFSO.getFolder(strDir)
    
    For Each objFile In objFolder.Files
        If objFile.Name Like "*" & strType And DateValue(objFile.DateCreated) = dt Then
            cnt = cnt + 1
        End If
    Next objFile
    
    CountFilesInFolder = cnt
    
End Function

Open in new window


You would call it like this to check the no of Excel files created on 2020-11-12 in the folder C:\Test.
MsgBox CountFilesInFolder("C:\Test", "*.bat*", DateSerial(2020, 11, 12))

Open in new window

Avatar of kbay808

ASKER

I changed the date in the function to today and ran the below macro, but the count was blank.  Is a * needed to account for the time portion of the datetime?

Example of a file name.  IMW123456 Order Form.txt

Private Sub CountFilesInFolder(strDir As String, Optional strType As String)
'DEVELOPER: Ryan Wells (wellsr.com)
'DESCRIPTION: This macro counts the files in a folder and retuns the result in a msgbox
'INPUT: Pass the procedure a string with your directory path and an optional
' file extension with the * wildcard
'EXAMPLES: Call CountFilesInFolder("C:\Users\Ryan\")
' Call CountFilesInFolder("C:\Users\Ryan\", "*txt")
    Dim file As Variant, i As Integer
    If Right(strDir, 1) <> "\" Then strDir = strDir & "\"
    file = Dir(strDir & strType)
    While (file <> "")
        i = i + 1

         If Format(FileDateTime(strDir & file), "dd/mm/yyyy") = "11/27/2020" Then
            filedateCount = filedateCount + 1
        End If


        file = Dir
    Wend
    'MsgBox i
    Count = i
End Sub

Open in new window

Sub Get_Counts()
    Call CountFilesInFolder("G:\Orders\Submitted\", "*txt")
    MsgBox Count
End sub

Open in new window

Avatar of kbay808

ASKER

I also tried the below code, but I get the following compile error.  "Wrong number of arguments or invalid property assignment"
MsgBox CountFilesInFolder("G:\Orders\Submitted\", "*.txt*", DateSerial(2020, 11, 27))

Open in new window

You are missing closing parentheses.
MsgBox CountFilesInFolder("G:\Orders\Submitted\", "*.txt*", DateSerial(2020, 11, 27))

Open in new window

Avatar of kbay808

ASKER

I corrected the comment.  It is correct in my module.
What code are you using, how are you using it and how is it not working?
Avatar of kbay808

ASKER

I running the "Get_Counts" macro and I'm getting the following error.  "Compile error: Wrong number of arguments or invalid property assignment"

Private Sub CountFilesInFolder(strDir As String, Optional strType As String)
'DEVELOPER: Ryan Wells (wellsr.com)
'DESCRIPTION: This macro counts the files in a folder and retuns the result in a msgbox
'INPUT: Pass the procedure a string with your directory path and an optional
' file extension with the * wildcard
'EXAMPLES: Call CountFilesInFolder("C:\Users\Ryan\")
' Call CountFilesInFolder("C:\Users\Ryan\", "*txt")
    Dim file As Variant, i As Integer
    If Right(strDir, 1) <> "\" Then strDir = strDir & "\"
    file = Dir(strDir & strType)
    While (file <> "")
        i = i + 1

         If Format(FileDateTime(strDir & file), "dd/mm/yyyy") = "11/27/2020" Then
            filedateCount = filedateCount + 1
        End If


        file = Dir
    Wend
    'MsgBox i
    Count = i
End Sub

Sub Get_Counts()
    MsgBox CountFilesInFolder("G:\Orders\Submitted", "*.txt*", DateSerial(2020, 11, 27))
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kbay808

ASKER

How do I modify it to use the date in cell A1?
Avatar of kbay808

ASKER

I figured it out.
MsgBox CountFilesInFolder("G:\Orders\Submitted\", "*.txt*", Sheets(Sheets("Sheets("Sheet1"),Range("A1")

Open in new window

Do you want you to see the result in a cell on a worksheet?