我用FindFirstFile和FindNextFile遍历目录下指定扩展名怎吗不行?
'遍历主函数
' 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
'FindFirstFileA的句柄
Dim lHandle As Long
Dim tFindData As WIN32_FIND_DATA '
Dim strFileName As String '文件名
On Error Resume Next
'初始化变量
i = 1
iIndex = 0
tFindData.cFileName = "" '初始化定长字符串
lHandle = FindFirstFile(strPathName & "\*.txt", 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 Check4.Value = 1 Then
If iIndex > 0 Then
For i = 1 To iIndex
sDirTraversal sSubDir(i), objList
Next
End If
End If
End Sub
'去掉固定长度字符串右边的NULL字符(ASCII值为0)和SPACE字符(ASCII值为32)函数
Function fDelInvaildChr(str As String) As String
On Error Resume Next
Dim i As Integer
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
上边的
lHandle = FindFirstFile(strPathName & "\*.txt", tFindData)
必须改为
lHandle = FindFirstFile(strPathName & "\*.*", tFindData)
的时候才会显示出子文件夹的内容,为什吗那??谢谢