【发布时间】:2014-12-03 11:18:34
【问题描述】:
我需要在 excel 2010 中使用 VBA 从远程服务器上的文件夹中获取文件名集合。我有一个可以工作的功能,在大多数情况下它可以完成这项工作,但是远程服务器经常很糟糕,可怕的网络性能问题。这意味着循环遍历 300 个文件以将其名称放入一个集合可能需要 10 分钟,文件夹中的文件数量可能会增长到数千个,因此这是行不通的,我需要一种获取所有文件名的方法在单个网络请求中而不是循环。我相信它连接到需要时间的远程服务器,因此单个请求应该能够相当快地一次性获取所有文件。
这是我目前拥有的功能:
Private Function GetFileNames(sPath As String) As Collection
'takes a path and returns a collection of the file names in the folder
Dim oFolder As Object
Dim oFile As Object
Dim oFSO As Object
Dim colList As New Collection
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(folderpath:=sPath)
For Each oFile In oFolder.Files
colList.Add oFile.Name
Next oFile
Set GetFileNames = colList
Set oFolder = Nothing
Set oFSO = Nothing
End Function
【问题讨论】:
-
+ 1 好问题 :) 你差点让我思考!
标签: vba excel optimization filesystemobject