N次修改了,此模块应该比较健壮吧,特点:
1、可遍历目录下所有文件
2、可筛选文件类型,可限定文件名关键词
3、遍历目录(文件夹)允许存在小数点.
4、一步到位,不用编写2次循环(即先遍历出目录,再遍历文件)
Sub searchFile() ' ---------------遍历文件夹内所有文件----------------------------- FileType = ".txt" '查找文件类型 FileKeyword = "svr" '进一步限定文件范围,当然也可以继续添加限定条件 '对话框方式选择路径 Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFolderPicker) If fd.Show = -1 Then sFolderPath = fd.SelectedItems(1) Set fd = Nothing Else Set fd = Nothing Exit Sub End If Dim file() As String, retFile() As String, fullPath$ Dim i%, k%, t%, f$ i = 1: k = 1: t = 1 ReDim file(1 To i) file(1) = sFolderPath & "\" '相对而言i父目录,k为对应子目录 Do Until i > k Debug.Print "file(" & i & ")=" & file(i) f = Dir(file(i), vbDirectory)