I have a program that will look for a specific file in a folder and if it exists, it will copy that file into a designated file path. I would like to go one step further and take my current code and have a check that happens prior to it. This check would be looking at a specified folder and determine if it's last modified date matches today's date. If the specified folder's last modified date matches the current date, I would like for the code to go into that folder and then my current code would execute.
Sub sbCopyingAFile()
'Declare Variables
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String
'This is Your File Name which you want to Copy
sFile = "testfile.xlsx"
'Change to match the source folder path
sSFolder = "C:\Dropbox (###)\BD SQL\"
'Change to match the destination folder path
sDFolder = "C:\Dropbox (###)\BD SQL\testpath\"
'Create Object
Set FSO = CreateObject("Scripting.FileSystemObject")
'Checking If File Is Located in the Source Folder
If Not FSO.FileExists(sSFolder & sFile) Then
MsgBox "Specified File Not Found", vbInformation, "Not Found"
'Copying If the Same File is Not Located in the Destination Folder
ElseIf Not FSO.FileExists(sDFolder & sFile) Then
FSO.CopyFile (sSFolder & sFile), sDFolder, True
MsgBox "Specified File Copied Successfully", vbInformation, "Done!"
Else
MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
End If
Debug.Print (sSFolder & sFile)
End Sub
CodePudding user response:
Please, try the next function:
Function GetDateLastModif(strFold As String) As Date
Dim fso As Object, gf As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set gf = fso.GetFolder(strFold)
GetDateLastModif = gf.DateLastModified
End Function
It can be tested as:
Debug.Print GetDateLastModif(ThisWorkbook.path) 'with or without the ending backslash
Or, having fso
object already, you may use GetFolder
it in your existing code:
Dim gf as Object
Set gf = fso.GetFolder(sSFolder)
If Day(gf.DateLastModified) = Date Then
'do whatever you need...
End If