我的宏将需要读取一个很大的文件目录,并从文件中解析数据。该目录会定期更新,因此我正在尝试优化程序,使其仅读取自上次运行程序以来添加的文件。
到目前为止,如果我没记错的话,似乎我只能按字母顺序读取文件。
到目前为止,我最好的解决方案是每次读取所有文件,创建一个包含文件信息的数组,按DateModified排序,然后仅打开我需要的文件。我很好奇我是否可以通过按DateModified顺序读取文件来跳过此步骤。
谢谢。
答案 0 :(得分:0)
Shell
在这里似乎是一个不错的选择-尽管我没有将性能与FSO
进行比较。例如,您可以考虑使用forfiles
命令,该命令允许您检索在指定日期之后修改的文件?
一些示例代码如下:
Public Sub RunMe()
Dim fileNames As Collection
Dim path As String
Dim dat As Date
Dim file As Variant
'Set the path and 'find after' date.
path = "c:\user\documents"
dat = #1/1/2018#
'Fetch the files, setting mask as required.
'This example is fetching all .txt files.
Set fileNames = GetFilesModifiedAfter(path, dat, "*.txt")
'Process the list of files.
If Not fileNames Is Nothing Then
For Each file In fileNames
' ... do stuff here.
Debug.Print path & "\" & file
Next
End If
End Sub
Private Function GetFilesModifiedAfter( _
path As String, _
after As Date, _
Optional mask As String) As Collection
Dim cmd As String
Dim piped() As String
Dim result As Collection
Dim i As Long
'Build the command string.
'Date must be formatted as MM/DD/YYYY.
cmd = "cmd.exe /s /c forfiles /p " & _
"""" & path & """" & _
" /d +" & Format(after, "mm/dd/yyyy")
'Add the mask if passed-in.
If mask <> vbNullString Then cmd = cmd & " /m " & mask
'Execute the command and split by /r/n.
piped = Split(CreateObject("WScript.Shell").Exec(cmd).StdOut.ReadAll, vbCrLf)
'Leave if nothing is returned.
If UBound(piped) = -1 Then Exit Function
'Poplate the result collection,
'and remove the leading and trailing inverted commas.
Set result = New Collection
For i = 0 To UBound(piped)
If Len(piped(i)) > 2 Then
result.Add Mid(piped(i), 2, Len(piped(i)) - 2)
End If
Next
'Return the result collection.
Set GetFilesModifiedAfter = result
End Function
更新
我刚刚进行了一些测试,看来FSO
更快,当然对于包含少于100个文件的文件夹而言。在非常大的文件夹(例如一千个文件)上运行此命令很有意思,因为我本能地认为Shell
可能具有性能优势。但是,现在,这里是FSO
版本:
Private Function GetFilesModifiedAfter2( _
path As String, _
after As Date, _
mask As String) As Collection
Dim fso As Object, file As Object
Dim result As Collection
'Instance of objects.
Set fso = CreateObject("Scripting.FileSystemObject")
Set result = New Collection
'Iterate the files and test date last modified property.
For Each file In fso.GetFolder(path & "\").Files
If file.Name Like mask And file.DateLastModified > after Then
result.Add file.Name
End If
Next
'Return the result collection.
Set GetFilesModifiedAfter2 = result
End Function