【问题标题】:How to compare if a folder's last modified date matches the current date? VBA如何比较文件夹的最后修改日期是否与当前日期匹配? VBA
【发布时间】:2021-11-12 19:41:33
【问题描述】:

我有一个程序会在文件夹中查找特定文件,如果存在,它将将该文件复制到指定的文件路径中。我想更进一步,采用我当前的代码并在它之前进行检查。此检查将查看指定文件夹并确定它的最后修改日期是否与今天的日期匹配。如果指定文件夹的最后修改日期与当前日期匹配,我希望代码进入该文件夹,然后执行当前代码。

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

【问题讨论】:

    标签: vba


    【解决方案1】:

    请尝试下一个功能:

    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
    

    可以这样测试:

    Debug.Print GetDateLastModif(ThisWorkbook.path) 'with or without the ending backslash
    

    或者,已经拥有fso 对象,您可以在现有代码中使用GetFolder

         Dim gf As Object, d As Date
         Set gf = fso.GetFolder(sSFolder)
         d = gf.DateLastModified
         If DateSerial(Year(d), month(d), Day(d)) = Date Then
            'do whatever you need...
         End If
    

    你也可以使用下一个(更简单的)方式:

        If CLng(cg.DateLastModified) = Date Then 'eliminate the decimals!
            'do what you want
         End If
    

    但最优雅的解决方案是直接比较它们。基于DateLastModified 不能是未来的日期这一事实。由于小时:分钟:秒“贡献”,它将(就Single/Double 值而言)大于或等于当前日期...

         If gf.DateLastModified >= Date Then  
             'do what you need...
         End If
    

    【讨论】:

    • 您好 FaneDuru,感谢您的回复。正如您所提到的,由于我已经声明了 fso 对象,因此我继续并选择使用您建议的底部代码。我正在测试该 Day(gf.DateLastModified) = Date 是否有效,但它没有,但那是因为我需要添加更多内容。由于日期返回 mm/dd/yyyy,因此我也使用了 MonthYear 函数并将它们全部连接成一个字符串,然后进行比较并得到所需的结果。非常感谢!
    • @bgado 很高兴我能帮上忙!从理论上讲,DateLastModified 返回时间,其中代码试图仅提取日期。格式应该无关紧要。您的本地化是否与标准英语不同?
    • 不,我的本地化设置为标准英语。所以,我很抱歉,但我有点失落,如果 DateLastModified 返回时间并且代码只提取日期,假设文件夹的日期是 08,你将它与日期进行比较,等式不是 08 =今天[2021 年 11 月 12 日] ?这就是我在使用方便的 debug.print 的帮助下理解该过程如何工作的方式。如果我超出范围,请随时纠正我。
    • @bgado Ups... 当然,你是对的。我怎么可能不专心。我非常确信我让它返回/提取Date 然后我什至没有考虑在如此简单的代码行中出现错误的可能性。我会在几分钟后打开我的笔记本电脑,我需要提供至少两个更优雅的解决方案......第一个让我头疼的是直接比较它们...... :)
    猜你喜欢
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    相关资源
    最近更新 更多