【问题标题】:How to check all files in the folder if modified this month?如果本月修改,如何查看文件夹中的所有文件?
【发布时间】:2020-12-22 08:54:10
【问题描述】:

我需要对我的代码实施一个条件,其中它检查所有文件是否在代码运行的同一个月被修改。如何修改此代码以检查月份?

Sub LookForNew()
Dim n As String, msg As String, d As Date
msg = ""
Set fso = CreateObject("Scripting.FileSystemObject")
Set fils = fso.GetFolder("C:\TestFolder").Files
For Each fil In fils
    n = fil.Name
    d = fil.DateCreated
    If d >= Date - 1 Then
        msg = msg & n & vbTab & d & vbCrLf
    End If
Next fil
If msg = "" Then
    MsgBox "No new files"
Else
    MsgBox msg
End If
Set fso = Nothing
End Sub

【问题讨论】:

标签: excel vba


【解决方案1】:

请尝试下一个改编代码:

Sub LookForNew()
 Dim FSO As Object, fils As Object, fil As Object, msg As String

 Set FSO = CreateObject("Scripting.FileSystemObject")
 Set fils = FSO.GetFolder(ThisWorkbook.Path).Files
 For Each fil In fils
    If Month(fil.DateLastModified) = Month(Date) And _
            Year(fil.DateLastModified) = Year(Date) Then
        msg = msg & fil.Name & vbTab & _
                            fil.DateLastModified & vbCrLf
    End If
 Next fil
 If msg = "" Then
    MsgBox "No new files"
 Else
    MsgBox msg
 End If
 Set FSO = Nothing
End Sub

您要求“所有文件都在同一个月修改”,并且您的代码会检查创建日期。我的,检查你问的...

为了避免返回上一年同月的修改文件,还必须检查年份。

【讨论】:

  • @Dumont:上面的代码没有解决你的问题吗?你有时间测试一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 2017-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多