用vb如何 统计文件夹下文件的数量!!!!

如题!!求救!!... 如题 !!求救!! 展开
 我来答
ljl88900
推荐于2016-04-26 · TA获得超过2661个赞
知道大有可为答主
回答量:2197
采纳率:100%
帮助的人:2632万
展开全部
'把下面源码复制到模块中即可
'文件查找模块
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
VB妮可
2007-12-03 · TA获得超过3333个赞
知道大有可为答主
回答量:2574
采纳率:0%
帮助的人:0
展开全部
动态生成一个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
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式