kbay808
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
Is it the date the file was created, the data it was last changed or last accessed?
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:\Use rs\Ryan\")
' Call CountFilesInFolder("C:\Use rs\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
'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:\Use
' Call CountFilesInFolder("C:\Use
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
filedateCount = filedateCount + 1
End If
file = Dir
Wend
'MsgBox i
Count = i
End Sub
You could try this function.
You would call it like this to check the no of Excel files created on 2020-11-12 in the folder C:\Test.
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
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))
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
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
Sub Get_Counts()
Call CountFilesInFolder("G:\Orders\Submitted\", "*txt")
MsgBox Count
End sub
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))
You are missing closing parentheses.
MsgBox CountFilesInFolder("G:\Orders\Submitted\", "*.txt*", DateSerial(2020, 11, 27))
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?
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
How do I modify it to use the date in cell A1?
ASKER
I figured it out.
MsgBox CountFilesInFolder("G:\Orders\Submitted\", "*.txt*", Sheets(Sheets("Sheets("Sheet1"),Range("A1")
Do you want you to see the result in a cell on a worksheet?