VB爱好者的求助:如何实现遍历目录查找特定类型的文件?
我想编写一个程序实现查找特定类型的文件的功能,然后把所有找到的文件用列表框列出来.要求包含文件的完整路径.不用API可不可以....
我想编写一个程序实现查找特定类型的文件的功能,然后把所有找到的文件用列表框列出来.要求包含文件的完整路径.不用API可不可以.
展开
1个回答
展开全部
请参考下面代码,其中假设要查找("D:\MyFolder\")下的*.XXX类型的文件
Sub FindFile()
Dim strFileName As String
Dim objFso As FileSystemObject
Dim objFiles As Files
Dim objFile As File
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFiles = objFso.GetFolder("D:\MyFolder\").Files
If objFiles.Count = 0 Then
MsgBox "没有任何文件!", vbInformation, "提示"
GoTo Err
End If
For Each objFile In objFiles
strFileName = objFile.Name
If Right(strFileName) = ".XXX" Then
List1.AddItem strFileName
End If
Next
Err:
Set objFile = Nothing
Set objFiles = Nothing
Set objFso = Nothing
Exit Sub
End Sub
Sub FindFile()
Dim strFileName As String
Dim objFso As FileSystemObject
Dim objFiles As Files
Dim objFile As File
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFiles = objFso.GetFolder("D:\MyFolder\").Files
If objFiles.Count = 0 Then
MsgBox "没有任何文件!", vbInformation, "提示"
GoTo Err
End If
For Each objFile In objFiles
strFileName = objFile.Name
If Right(strFileName) = ".XXX" Then
List1.AddItem strFileName
End If
Next
Err:
Set objFile = Nothing
Set objFiles = Nothing
Set objFso = Nothing
Exit Sub
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询