我正在创建excel宏,需要在其中将第一个和最后一个修改的文件时间放在excel工作表中,例如第一个日志文件的修改时间和最后一个修改的时间。(假设目录中有多个日志文件)。我需要修改第一个文件和最后一个文件的时间。
我无法比较日期,请提出建议。
Sub test()
Dim fso As Object
Dim fol As Object
Dim fil As Object
Dim temp As Date
Set fso = CreateObject("Scripting.FileSystemObject")
Set fol = fso.GetFolder("Z:\Logfiles\Monitor\Logon")
For Each fil In fol.Files
temp = fil.DateLastModified
Next fil
MsgBox temp
End Sub
答案 0 :(得分:2)
You just need to save the oldest and newest date as you loop through the directory:
Option Explicit
Sub test()
Dim fso As Object
Dim fol As Object
Dim fil As Object
Dim temp As Date
Set fso = CreateObject("Scripting.FileSystemObject")
Set fol = fso.GetFolder("C:\Temp\")
Dim oldest As Date
Dim oldestFile As String
Dim newest As Date
Dim newestFile As String
For Each fil In fol.Files
temp = fil.DateLastModified
If (newest = #12:00:00 AM#) Or (temp > newest) Then
newest = temp
newestFile = fil.Path
End If
If (oldest = #12:00:00 AM#) Or (temp < oldest) Then
oldest = temp
oldestFile = fil.Path
End If
Next fil
MsgBox "Oldest File: " & oldestFile & " (" & oldest & ")" & vbCrLf & _
"Newest File: " & newestFile & " (" & newest & ")"
End Sub
答案 1 :(得分:0)
Option Explicit
Sub LoopAllFilesInFolder()
Dim strFolder As String
Dim Library As Object, File As Object, Folder As Object
strFolder = "Z:\Logfiles\Monitor\Logon\"
Set Library = CreateObject("Scripting.FileSystemObject")
Set Folder = Library.GetFolder(strFolder)
Set File = Folder.Files
For Each File In File
MsgBox File.Name & " details:" & vbNewLine & vbNewLine & _
"Date Created: " & File.datecreated & vbNewLine & _
"Date Last Accessed: " & File.datelastaccessed & vbNewLine & _
"Date Last Modified: " & File.dateLastModified
Next
Set Library = Nothing
Set Folder = Nothing
Set File = Nothing
End Sub