我需要在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
Run Code Online (Sandbox Code Playgroud)