'最近想编写一个应用程序需要遍历所有文件功能,以下是代码。结果是隐藏文件夹下的文件不能搜索得到文件名。如何解决,各位请赐教,'以下是调用代码:Private Sub Command1_Click()
Dim SearchedPath As String
Dim SearchedFile As String
SearchedPath = "F:"
SearchedFile = "*.*"
AllSearch SearchedPath, SearchedFile
End Sub'以下是模块
Public Const MAX_PATH = 260
Public Const FILE_ATTRIBUTE_DIRECTORY = &H10Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End TypePublic 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 TypePublic 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 Declare Function SetCurrentDirectory Lib "kernel32" Alias _
"SetCurrentDirectoryA" (ByVal lpPathName As String) As LongSub AllSearch(sPath As String, sFile As String)
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
lFindFile = FindFirstFile(sPath + "\" + sFile, ff)
Debug.Print sPath + "\" + sFile
If lFindFile > 0 Then
Do
Form1.List1.AddItem ff.cFileName
Loop Until (FindNextFile(lFindFile, ff) = 0)
FindClose lFindFile
End If
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
End If
End If
xf.cFileName = ""
Loop Until (FindNextFile(findhandle, xf) = 0)
FindClose findfile
End Sub
Dim SearchedPath As String
Dim SearchedFile As String
SearchedPath = "F:"
SearchedFile = "*.*"
AllSearch SearchedPath, SearchedFile
End Sub'以下是模块
Public Const MAX_PATH = 260
Public Const FILE_ATTRIBUTE_DIRECTORY = &H10Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End TypePublic 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 TypePublic 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 Declare Function SetCurrentDirectory Lib "kernel32" Alias _
"SetCurrentDirectoryA" (ByVal lpPathName As String) As LongSub AllSearch(sPath As String, sFile As String)
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
lFindFile = FindFirstFile(sPath + "\" + sFile, ff)
Debug.Print sPath + "\" + sFile
If lFindFile > 0 Then
Do
Form1.List1.AddItem ff.cFileName
Loop Until (FindNextFile(lFindFile, ff) = 0)
FindClose lFindFile
End If
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
End If
End If
xf.cFileName = ""
Loop Until (FindNextFile(findhandle, xf) = 0)
FindClose findfile
End Sub
如果不全是,如何设置FILE_ATTRIBUTE_DIRECTORY才能到所有文件夹下遍历?
程序是参考书上的不大懂