Private Sub Command1_Click()
Dim p As Object, fs As Object, fld As Object
Set fs = CreateObject("scripting.filesystemobject") '创建文件系统对象fs
Set fld = fs.getfolder("c:\test") '创建文件系统对象的文件夹对象fld
For Each p In fld.Files '遍历fld对象的所有files,并赋给变量p
Msgbox p.name
Next
end sub
' 一个函数,放哪都调用
' 获得指定目录下的所有文件的文件名列表
' Path 为目录的绝对路径, FileName() 为返回的文件名数组
Function GetDirectoryFileList(ByVal Path As String, ByRef FileName() As String) As Boolean
Dim fName As String, i As Long
If Right$(Path, 1) <> "\" Then Path = Path & "\"
fName = Dir$(Path & "*.*")
i = 0
Do While fName <> ""
ReDim Preserve FileName(i) As String
FileName(i) = fName
fName = Dir$
i = i + 1
Loop
If i <> 0 Then
ReDim Preserve FileName(i - 1) As String
GetDirectoryFileList = True
Else
GetDirectoryFileList = False
End If
End Function