[英]How to check all files in the folder if modified this month?
我需要對我的代碼實施一個條件,其中它檢查所有文件是否在代碼運行的同一個月被修改。 如何修改此代碼以檢查月份?
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
請嘗試下一個改編代碼:
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
您要求“所有文件都在同一個月修改”並且您的代碼檢查創建日期。 我的,檢查你問的...
為了避免返回上一年同月修改過的文件,還必須檢查年份。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.