2个回答
展开全部
'把下面源码复制到模块中即可
'文件查找模块
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const MAX_PATH = 260
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
Declare Function FindFirstFile Lib "kernel32" Alias _
"FindFirstFileA" (ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA) As Long
Declare Function FindNextFile Lib "kernel32" Alias _
"FindNextFileA" (ByVal hFindFile As Long, _
lpFindFileData As WIN32_FIND_DATA) As Long
Declare Function FindClose Lib "kernel32" (ByVal _
hFindFile As Long) As Long
Function AllSearch(sPath As String, sFile As String, searchsub As Boolean) As Long
'参数:sPath- 查找路径,sFile- 文件名(可含通配符),searchsub- 是否搜索下级目录
'返回:查找到的文件数量
Dim xf As WIN32_FIND_DATA
Dim ff As WIN32_FIND_DATA
Dim findhandle As Long
Dim lFindFile As Long
Dim astr As String
Dim bstr As String
Static Filecount As Long
sPath = IIf(Right(sPath, 1) = "\", Left(sPath, Len(sPath) - 1), sPath)
lFindFile = FindFirstFile(sPath + "\" + sFile, ff)
If lFindFile > 0 Then
Do
If Left(ff.cFileName, 2) <> ".." And Left(ff.cFileName, 1) <> "." And (GetAttr(sPath & "\" & ff.cFileName) And vbDirectory) <> vbDirectory Then
Filecount = Filecount + 1
End If
Loop Until (FindNextFile(lFindFile, ff) = 0)
FindClose lFindFile
End If
If searchsub Then
astr = sPath + "\" + "*.*"
findhandle = FindFirstFile(astr, xf)
DoEvents
Do
If xf.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY Then
If Asc(xf.cFileName) <> Asc(".") Then
bstr = sPath + "\" + Left$(xf.cFileName, InStr(xf.cFileName, Chr(0)) - 1)
AllSearch bstr, sFile, searchsub
End If
End If
xf.cFileName = ""
Loop Until (FindNextFile(findhandle, xf) = 0)
FindClose lFindFile
End If
AllSearch = Filecount
End Function
'文件查找模块
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const MAX_PATH = 260
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
Declare Function FindFirstFile Lib "kernel32" Alias _
"FindFirstFileA" (ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA) As Long
Declare Function FindNextFile Lib "kernel32" Alias _
"FindNextFileA" (ByVal hFindFile As Long, _
lpFindFileData As WIN32_FIND_DATA) As Long
Declare Function FindClose Lib "kernel32" (ByVal _
hFindFile As Long) As Long
Function AllSearch(sPath As String, sFile As String, searchsub As Boolean) As Long
'参数:sPath- 查找路径,sFile- 文件名(可含通配符),searchsub- 是否搜索下级目录
'返回:查找到的文件数量
Dim xf As WIN32_FIND_DATA
Dim ff As WIN32_FIND_DATA
Dim findhandle As Long
Dim lFindFile As Long
Dim astr As String
Dim bstr As String
Static Filecount As Long
sPath = IIf(Right(sPath, 1) = "\", Left(sPath, Len(sPath) - 1), sPath)
lFindFile = FindFirstFile(sPath + "\" + sFile, ff)
If lFindFile > 0 Then
Do
If Left(ff.cFileName, 2) <> ".." And Left(ff.cFileName, 1) <> "." And (GetAttr(sPath & "\" & ff.cFileName) And vbDirectory) <> vbDirectory Then
Filecount = Filecount + 1
End If
Loop Until (FindNextFile(lFindFile, ff) = 0)
FindClose lFindFile
End If
If searchsub Then
astr = sPath + "\" + "*.*"
findhandle = FindFirstFile(astr, xf)
DoEvents
Do
If xf.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY Then
If Asc(xf.cFileName) <> Asc(".") Then
bstr = sPath + "\" + Left$(xf.cFileName, InStr(xf.cFileName, Chr(0)) - 1)
AllSearch bstr, sFile, searchsub
End If
End If
xf.cFileName = ""
Loop Until (FindNextFile(findhandle, xf) = 0)
FindClose lFindFile
End If
AllSearch = Filecount
End Function
展开全部
动态生成一个filelistbox 实现这个功能:
把.Path = "c:\"中的C:\换成你需要的路径就行了..
Private WithEvents a As FileListBox
Private Sub Command1_Click()
Set a = Controls.Add("VB.FileListBox", "File1")
With a
.Visible = False
.Path = "c:\"
.ReadOnly = True
.Hidden = True
.System = True
End With
Print a.ListCount
End Sub
把.Path = "c:\"中的C:\换成你需要的路径就行了..
Private WithEvents a As FileListBox
Private Sub Command1_Click()
Set a = Controls.Add("VB.FileListBox", "File1")
With a
.Visible = False
.Path = "c:\"
.ReadOnly = True
.Hidden = True
.System = True
End With
Print a.ListCount
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询