Option Explicit
Public Const MAX_PATH = 260
Public Const INVALID_HANDLE_VALUE = -1
Public Const ERROR_NO_MORE_FILES = 18&
Public Const FILE_ATTRIBUTE_DIRECTORY = &H10
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 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" (B
yVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) A
s Long
Public Declare Function GetLastError Lib "kernel32" () As Long
'所有的文件列表,全局变量
Public thefilelist As New Collection
Public Sub findfile(ByVal thepathname As String) 'thepathname 的形式为"C:\pa
th\thesubpath\",
'例如,调用 findfile "c:\windows\"
Dim hfile As Long
Dim result As Long
Dim errorno As Long
Dim thefileinfo As WIN32_FIND_DATA
Dim findfinish As Boolean
Dim fullname As String
Dim realname As String
'debug
Dim pos As Long
Dim r As String
'debug
findfinish = False
hfile = FindFirstFile(thepathname & "*", thefileinfo)
If (hfile = INVALID_HANDLE_VALUE) Then
'错误处理......
errorlog
Exit Sub
End If
realname = trimstr(thefileinfo.cFileName)
fullname = thepathname & realname
If (thefileinfo.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY) Then
If (realname <> "." And realname <> "..") Then
findfile fullname & "\"
End If
Else
thefilelist.Add fullname
End If
Do Until (findfinish)
result = FindNextFile(hfile, thefileinfo)
If (result = 0) Then
errorno = GetLastError()
If (errorno = ERROR_NO_MORE_FILES) Then
'此目录查询完毕
findfinish = True
Else
'出错
errorlog
End If
'关闭查找句柄
result = FindClose(hfile)
If (result = 0) Then
errorlog
End If
Exit Sub
Else
realname = trimstr(thefileinfo.cFileName)
fullname = thepathname & realname
If (thefileinfo.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY) Then
If (realname <> "." And realname <> "..") Then
findfile fullname & "\"
End If
Else
thefilelist.Add fullname
End If
End If
Loop
End Sub
Public Sub errorlog()
Dim errorno As Long
Dim hfile As Integer
Dim thedate
Dim thetime
Dim msg As String
Dim errordir As String
errorno = GetLastError()
thedate = Date
thetime = time
errordir = App.Path
errordir = errordir & "\errorlog.txt"
hfile = FreeFile
msg = CStr(errorno) & " the systemerro " & CStr(thedate) & " " & CStr(thetime)
Open errordir For Append As hfile
Write #hfile, msg
Close hfile
thefilelist.Remove 1
End Sub
Function trimstr(thelongfilename As String) As String '?????? 这样写好么?因
为win32_file_data下面的文件名是定长的字符串,
Dim pos As Integer
Dim theend As String
theend = Right(thelongfilename, 1)
pos = InStr(thelongfilename, theend)
trimstr = Left(thelongfilename, pos - 1)
End Function
' 將「C:\」根目錄下的子目錄顯示出。
MyPath = "c:\" ' 指定路徑。
MyName = Dir(MyPath, vbDirectory) ' 找尋第一個子目錄。
Do While MyName <> "" ' 執行迴圈。
' 跳過目前的目錄及上層目錄。
If MyName <> "." And MyName <> ".." Then
' 使用位元比對來確定 MyName 代表一目錄。
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' 將目錄名稱顯示出來。
End If
End If
MyName = Dir ' 尋找下一個目錄。
Loop