Private Sub Command1_Click() Debug.Print SeachFile("D:\外语学习\lhut3227\Source", "doc")
End SubPublic Function SeachFile(PathStr As String, ExtensionName As String) As LongDim Fso As FileSystemObject Dim Fols As Folders Dim Fol As Folder Dim Fils As Files Dim Fil As File If Right(PathStr, 1) <> "\" Then PathStr = PathStr & "\" Set Fso = CreateObject("Scripting.FileSystemObject") Set Fol = Fso.GetFolder(PathStr) Set Fils = Fol.Files
For Each Fil In Fol.Files If Fso.GetExtensionName(Fil.Name) = ExtensionName Then Debug.Print Fil.Path & "\" & Fil.Name '//你的要求可以写在这里..........如果包含特定字符就怎么样!! SeachFile = SeachFile + 1 End If Next DoEvents Set Fols = Fol.SubFolders For Each Fol In Fols Call SeachFile(Fol.Path, ExtensionName) NextEnd Function
列举出c:\test文件夹中文件名包含wsui的所有文件到列表框list1中,代码如下:private sub command1_click() dim sfile as string sfile=dir("c:\test\*.*") do while sfile<>"" if instr(1,sfile,"wsui") then list1.additem sfile end if sfile=dir loop end sub
最方便简单的方法,已经设计成了函数。 ----------------------Option Explicit'// 查找函数 Private Function GetFileName(ByVal Path As String, ByVal Search As String) As Collection Dim MyName As String Set GetFileName = New Collection Path = Path & IIf(Right$(Path, 1) = "\", "", "\") MyName = Dir(Path, vbArchive) Do While MyName <> "" If MyName <> "." And MyName <> ".." Then If (GetAttr(Path & MyName) And vbArchive) = vbArchive Then If InStr(LCase$(MyName), LCase$(Search)) Then GetFileName.Add MyName End If End If MyName = Dir Loop End FunctionPrivate Sub Command1_Click() Dim lNext As Long Dim ColData As Collection Set ColData = GetFileName("E:\Actx\", "zh") Debug.Print "一共找到: " & ColData.Count & " 个文件" For lNext = 1 To ColData.Count Debug.Print "第 " & lNext & " 个文件是: " & ColData(lNext) Next lNext End Sub
Private Function GetFileName(ByVal Path As String, ByVal Search As String) As Collection Dim MyName As String Set GetFileName = New Collection Path = Path & IIf(Right$(Path, 1) = "\", "", "\") MyName = Dir(Path, vbArchive) Do While MyName <> "" If MyName <> "." And MyName <> ".." Then If (GetAttr(Path & MyName) And vbArchive) = vbArchive Then If InStr(LCase$(MyName), LCase$(Search)) Then GetFileName.Add MyName End If End If MyName = Dir Loop End Function
Debug.Print SeachFile("D:\外语学习\lhut3227\Source", "doc")
End SubPublic Function SeachFile(PathStr As String, ExtensionName As String) As LongDim Fso As FileSystemObject
Dim Fols As Folders
Dim Fol As Folder
Dim Fils As Files
Dim Fil As File
If Right(PathStr, 1) <> "\" Then PathStr = PathStr & "\"
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Fol = Fso.GetFolder(PathStr)
Set Fils = Fol.Files
For Each Fil In Fol.Files
If Fso.GetExtensionName(Fil.Name) = ExtensionName Then
Debug.Print Fil.Path & "\" & Fil.Name
'//你的要求可以写在这里..........如果包含特定字符就怎么样!!
SeachFile = SeachFile + 1
End If
Next
DoEvents
Set Fols = Fol.SubFolders
For Each Fol In Fols
Call SeachFile(Fol.Path, ExtensionName)
NextEnd Function
dim sfile as string
sfile=dir("c:\test\*.*")
do while sfile<>""
if instr(1,sfile,"wsui") then
list1.additem sfile
end if
sfile=dir
loop
end sub
最方便简单的方法,已经设计成了函数。
----------------------Option Explicit'// 查找函数
Private Function GetFileName(ByVal Path As String, ByVal Search As String) As Collection
Dim MyName As String
Set GetFileName = New Collection
Path = Path & IIf(Right$(Path, 1) = "\", "", "\")
MyName = Dir(Path, vbArchive)
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
If (GetAttr(Path & MyName) And vbArchive) = vbArchive Then
If InStr(LCase$(MyName), LCase$(Search)) Then GetFileName.Add MyName
End If
End If
MyName = Dir
Loop
End FunctionPrivate Sub Command1_Click()
Dim lNext As Long
Dim ColData As Collection
Set ColData = GetFileName("E:\Actx\", "zh")
Debug.Print "一共找到: " & ColData.Count & " 个文件"
For lNext = 1 To ColData.Count
Debug.Print "第 " & lNext & " 个文件是: " & ColData(lNext)
Next lNext
End Sub
Dim MyName As String
Set GetFileName = New Collection
Path = Path & IIf(Right$(Path, 1) = "\", "", "\")
MyName = Dir(Path, vbArchive)
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
If (GetAttr(Path & MyName) And vbArchive) = vbArchive Then
If InStr(LCase$(MyName), LCase$(Search)) Then GetFileName.Add MyName
End If
End If
MyName = Dir
Loop
End Function