不好意思,看错题了,呵呵。重给你答复:
在FORM1上创建FILELISTBOX 名为:fleList,创建DIRLISTBOX 名为:dirList
然后...
Sub SearchAllFile(ByVal Path As String)
Dim i As Integer, OldPath As String, Lc As Integer
Dim OldPath2 As String
OldPath = dirList.Path
dirList.Path = Path
OldPath2 = dirList.Path
Lc = dirList.ListCount - 1
'Search Dirs
For i = 0 To Lc
SearchAllFile dirList.List(i)
Next
fleList.Path = OldPath2
'Search Files
For i = 0 To fleList.ListCount - 1
List1.AddItem dirList.Path & "\" & fleList.List(i)
Next
dirList.Path = OldPath
End Sub
下面是一个列出指定目录下所有符合要求的文件名的函数:
Public Function AutoListFiles(ByVal sDirName As String,ByVal FileFilter As String ) As Boolean
On Error GoTo RF_ERROR
Dim sName As String, sFile As String, sExt As String
Dim sDirList() As String, iDirNum As Integer, i As Integer
′首先枚举所有文件
sFile = Dir(sDirName + FileFilter, vbNormal + vbArchive + vbHidden)
Do While Len(sFile) >0
sFile = UCase(Trim(sFile))
′在此处可以将 sFile 加入到一个 Text 控件...
sFile = Dir ′下一个文件
Loop
iDirNum = 0
sName = Dir(sDirName + ″*.*″, vbDirectory + vbNormal)
Do While Len(sName) >0
If sName <> ″.″ And sName <> ″..″ Then
iDirNum = iDirNum + 1
ReDim Preserve sDirList(1 To iDirNum)
sDirList(iDirNum) = sDirName + sName + ″\″
End If
sName = Dir ′下一个目录
Loop
For i = 1 To iDirNum
AutoListFiles sDirList(i) ′递归调用
Next
End If
RF_EXIT:
AutoListFiles = True
Exit Function
RF_ERROR:
MsgBox Err.Description, vbCritical, ″″
Resume RF_EXIT
End Function
调用上述函数的示例:
AutoListFiles(″C:\″,″*.txt")
dim szFileName as String
szFileName = Dir("*.txt") '查找第一个txt文件。
Do While szFileName <> "" ' 开始循环。
form1.Print szFileName
szFileName = Dir ' 查找下一个txt文件。
Loop
Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Public Declare Function FindFirstFile _
Lib "kernel32" Alias "FindFirstFileA" _
(ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindNextFile _
Lib "kernel32" Alias "FindNextFileA" _
(ByVal hFindFile As Long, _
lpFindFileData As WIN32_FIND_DATA) As Long
在你自己的函数中:
Dim WFD As WIN32_FIND_DATA
Dim hFile As Long
Dim sFile As String
hFile = FindFirstFile(sPath & "*.*" & Chr$(0), WFD)
If hFile <> -1 Then
sFile = TrimNull(WFD.cFileName)
While FindNextFile(hFile, WFD)