简体   繁体   English

如果本月修改,如何查看文件夹中的所有文件?

[英]How to check all files in the folder if modified this month?

I need to implement a condition to my code wherein it checks if all files are modified the same month the code is run.我需要对我的代码实施一个条件,其中它检查所有文件是否在代码运行的同一个月被修改。 How do I modify this code to check for month instead?如何修改此代码以检查月份?

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

Try the next adapted code, please:请尝试下一个改编代码:

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

You asked for "all files are modified the same month" and your code checks the creation date.您要求“所有文件都在同一个月修改”并且您的代码检查创建日期。 Mine, checks what you asked...我的,检查你问的...

In order to avoid returning the modified files for the same month of previous year, the year must also be checked.为了避免返回上一年同月修改过的文件,还必须检查年份。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM