其他分享
首页 > 其他分享> > VBA 获得文件夹下的所有文件(包括子文件夹)列表,输出为getfilelist数组

VBA 获得文件夹下的所有文件(包括子文件夹)列表,输出为getfilelist数组

作者:互联网

Function getfilelist(path As String) As Variant
'返回文件夹的所有文件,输出位getfilelist数组
Dim ifolder As Folder
Dim ifile As file
Dim filelist As New ArrayList

Set root = os.GetFolder(path)

For Each ifile In root.Files
    filelist.Add ifile.Name
Next

For Each ifolder In root.SubFolders
    For Each ifile In ifolder.Files
        filelist.Add ifile.Name
    Next
Next

getfilelist = filelist.ToArray

End Function 

  

其他Sub程序可以直接调用此函数;例如

Sub fds()
Dim c As Variant
c = getfilelist("C:\Users\xyz\software")
Debug.Print "文件夹下共有" & UBound(c) & "个文件"
Debug.Print "第一个文件名是:" & c(0)

 

 

 

 

 

标签:Dim,VBA,getfilelist,ifile,filelist,Next,文件夹
来源: https://www.cnblogs.com/wingxyz/p/16175526.html