Function
getAllFiles(fpath
As
String
)
filename
=
Dir
(fpath)
'
获取路径下的第一个文件名(仅有文件名,不含完整路径)
Do
While
filename <>
""
Debug.Print (filename)
filename
=
Dir
'
获取下一个文件
End Function
'
利用FSO遍历目录下的所有文件名及其路径,不包含子目录的
Function
getAllFiles2(fpath
As
String
)
Dim
OFso
As
Object
, baseFolder
As
Object
, ofile
As
Object
Set
OFso =
CreateObject
(
"
Scripting.FileSystemObject
"
)
Set
baseFolder =
OFso.GetFolder(fpath)
For
Each
ofile
In
baseFolder.Files
Debug.Print ofile.Path
End Function
'
利用FSO遍历目录下的所有文件名及其路径,包含子目录的
Function
getAllFiles3(fpath
As
String
)
Dim
fso
As
New
FileSystemObject
'
此处与前面getAllFiles2中使用了不同的思路
Dim
src_folder
As
Folder
'
使用Floder对象需要在菜单栏——工具——引用中勾选Microsoft Scripting Runtime
Set
src_folder = fso.GetFolder(fpath)
'
将String类型的路径转换成Folder对象
Dim
f
As
File, fld
As
Folder
For
Each
f
In
src_folder.Files
'
遍历目录下所有文件
Debug.Print (f.Path)
For
Each
fld
In
src_folder.SubFolders
'
遍历子文件夹
'
Debug.Print (fld.Path)
getAllFiles3 (fld.Path)
'
嵌套调用自身
End Function
法2则可带可不带。
法2参考
https://blog.csdn.net/weixin_45385543/article/details/125487567