高手请进!!非常急的问题!

HONGDUN 2003-01-15 07:38:41
如何用VB遍历指定路径下的文件。
注:后面要获取每一个文件的详细信息!!!
...全文
8 点赞 收藏 5
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
along 2003-01-15
给你一段代码,它是用API实现的速度比DIR快(用 DIR函数也可实现)
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private FiTime As FILETIME
Private 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 * 255
cAlternate As String * 14
End Type
Privat Const vbKeyDot = 46
'遍历指定目录下所有文件
Public Sub SearchFileSpec(ByVal CurPath As String)
Dim FindF1 As WIN32_FIND_DATA
Dim hDndle1 As Long, Lp0 As Long
Dim FileNa As String
curpath=iif(right(curpath,1),"\",cutpath,curpath &"\")
hDndle1 = FindFirstFile(CurPath & "*", FindF1)
If hDndle1 <> -1 Then

Do
DoEvents
'添加列表项
If Asc(FindF1.cFileName) <> vbKeyDot Then

Lp0 = InStr(FindF1.cFileName, vbNullChar)
If Lp0 <> 0 Then
FileNa = Mid$(FindF1.cFileName, 1, Lp0 - 1)
Else
FileNa = FindF1.cFileName
End If
‘文件已得到添加你想处理的代码

endif
Loop While FindNextFile(hDndle1, FindF1)
Call FindClose(hDndle1)
End If
End Sub
回复
dsclub 2003-01-15
to:boybluesky(boybluesky)

我可没说“树状目录中所有的文件”。是本级目录里所有属性的文件,搞清了大侠!

要是想遍历树状目录中所有的文件,还要在加上对文件夹的遍历判断,我当初做的时候是用了递归函数。
回复
龙华 2003-01-15
dsclub的方法能够遍历树状目录中所有的文件???怀疑中。。明天再试。应该是不可以。
回复
freelazy 2003-01-15
楼上所说的是最简单的 vb 方法,
还有一种可能就是用 api ,好像比较复杂。
回复
dsclub 2003-01-15
'以下代码遍历指定路径下的文件(是所有属性的文件)
Private Sub Command1_Click()
Dim strPath As String
Dim strFile As String

strPath = "C:\"

strFile = Dir(strPath + "*.*", vbNormal + vbHidden + vbReadOnly + vbSystem)
Do While strFile <> ""
List1.AddItem strFile '在窗体上放一个List1
strFile = Dir
Loop

End Sub
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7453

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告