VB 中遍历目录,遍历目录查找文件的2个实现方法
方法1:API实现
将下列代码保存到一个模块中,就可以直接调用了
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
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
'最大路径长度和文件属性常量的定义
Public Const MAX_PATH = 260
Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
Public Const FILE_ATTRIBUTE_COMPRESSED = &H800
Public Const FILE_ATTRIBUTE_DIRECTORY = &H10
Public Const FILE_ATTRIBUTE_HIDDEN = &H2
Public Const FILE_ATTRIBUTE_NORMAL = &H80
Public Const FILE_ATTRIBUTE_READONLY = &H1
Public Const FILE_ATTRIBUTE_SYSTEM = &H4
Public Const FILE_ATTRIBUTE_TEMPORARY = &H100
'自定义数据类型FILETIME和WIN32_FIND_DATA的定义
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public 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 Function fDelInvaildChr(str As String) As String
On Error Resume Next
For i = Len(str) To 1 Step -1
If Asc(Mid(str, i, 1)) <> 0 And Asc(Mid(str, i, 1)) <> 32 Then
fDelInvaildChr = Left(str, i)
Exit For
End If
Next
End Function
'遍历主函数
'参数说明:
' strPathName 要遍历的目录
' objList 使用VB的内部控件ListBox来存放遍历得到的路径,之所以
' 不使用字符串数组是因为数组大小不好定义
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub sDirTraversal(ByVal strPathName As String, ByRef objList As ListBox)
Dim sSubDir(200) As String '存放当前目录下的子目录,下标可根据需要调整
Dim iIndex As Integer '子目录数组下标
Dim i As Integer '用于循环子目录的查找
Dim lHandle As Long 'FindFirstFileA 的句柄
Dim tFindData As WIN32_FIND_DATA '
Dim strFileName As String '文件名
On Error Resume Next
'初始化变量
i = 1
iIndex = 0
tFindData.cFileName = "" '初始化定长字符串
lHandle = FindFirstFile(strPathName & "\*.*", tFindData)
If lHandle = 0 Then '查询结束或发生错误
Exit Sub
End If
strFileName = fDelInvaildChr(tFindData.cFileName)
If tFindData.dwFileAttributes = &H10 Then '目录
If strFileName <> "." And strFileName <> ".." Then
iIndex = iIndex + 1
sSubDir(iIndex) = strPathName & "\" & strFileName '添加到目录数组
End If
Else
objList.AddItem strPathName & "\" & strFileName
End If
'循环查找下一个文件,直到结束
Do While True
tFindData.cFileName = ""
If FindNextFile(lHandle, tFindData) = 0 Then '查询结束或发生错误
FindClose (lHandle)
Exit Do
Else
strFileName = fDelInvaildChr(tFindData.cFileName)
If tFindData.dwFileAttributes = &H10 Then
If strFileName <> "." And strFileName <> ".." Then
iIndex = iIndex + 1
sSubDir(iIndex) = strPathName & "\" & strFileName '添加到目录数组
End If
Else
objList.AddItem strPathName & "\" & strFileName
End If
End If
Loop
'如果该目录下有目录,则根据目录数组递归遍历
If iIndex > 0 Then
For i = 1 To iIndex
sDirTraversal sSubDir(i), objList
Next
End If
End Sub
方法2:不使用API,直接用VB的函数Dir 来实现
'------------------------------------
'遍历列出所有目录
Public Function FindTxt(ByVal sTmp As String, list As Collection)
Dim myPath, myName As String
Dim i, j, k As Integer
Dim sPath() As String
Dim sStr() As String
Dim sTemp As String
'Dim sTxt() As String
myPath = sTmp
myName = Dir(myPath, vbDirectory) ' 找寻第一项。
j = 0
k = 0
While Len(myName) > 0
If myName <> "." And myName <> ".." Then
' 使用位比较来确定 MyName 代表一目录。
If (GetAttr(myPath & myName) And vbDirectory) = vbDirectory Then
ReDim Preserve sPath(j)
'Debug.Print "找到目录 " & myName ' 如果它是一个目录,将其名称显示出来。
sPath(j) = myName
'i = i + 1
j = j + 1
Else
sStr = Split(myName, ".")
If UBound(sStr) = 1 Then
If LCase(sStr(1)) = "txt" Then
'ReDim Preserve sTxt(k)
'Debug.Print "找到文件 " & myName ' 如果它是一个目录,将其名称显示出来。
list.Add myName
'sTxt(k) = myName
'k = k + 1
End If
End If
End If
End If
myName = Dir
Wend
If j > 0 Then
For i = 0 To UBound(sPath)
Call FindTxt(myPath & sPath(i) & "\", list)
Next
End If
Erase sPath
'Erase sTxt
End Function
'------------------------------------
'遍历列出所有目录
Public Function FindDir(ByVal sTmp As String, list As Collection)
Dim myPath, myName As String
Dim i, j As Integer
Dim sPath() As String
myPath = sTmp
myName = Dir(myPath, vbDirectory) ' 找寻第一项。
j = 0
While Len(myName) > 0
'i = 0
If myName <> "." And myName <> ".." Then
' 使用位比较来确定 MyName 代表一目录。
If (GetAttr(myPath & myName) And vbDirectory) = vbDirectory Then
ReDim Preserve sPath(j)
list.Add myName
sPath(j) = myName
'i = i + 1
j = j + 1
End If
End If
myName = Dir
Wend
If j > 0 Then
For i = 0 To UBound(sPath)
FindDir myPath & sPath(i) & "\", list
Next
End If
Erase sPath
End Function
总结,方法2实现起来比较简单,速度也不错,调用方法:
dim oCols as new collection
FindDir "C:\", oCols
for i =1 to oCols.Count
debug.print oCols.Item(i)
next
总结,方法2实现起来比较简单,速度也不错,调用方法:
dim oCols as new collection
FindDir "C:\", oCols
for i =1 to oCols.Count
debug.print oCols.Item(i)
next