《用api函数遍历指定驱动器、目录的文》由会员分享,可在线阅读,更多相关《用api函数遍历指定驱动器、目录的文(6页珍藏版)》请在金锄头文库上搜索。
1、用用 APIAPI 函数遍历指定驱动器、目录的文函数遍历指定驱动器、目录的文用 API 函数遍历指定驱动器、目录的文件以下代码演示了如何用 Windows API 函数遍历指定驱动器、目录的所有文件。其思路是:调出浏览文件夹窗口让用户指定所要搜索的起始路径,然后用查找文件的 API 函数遍历该目录下及其包含的子目录下的所有文件。本例需要:一个按钮,一个 TextBox 和一个ListBox,其中,TextBox 应设置为多行。核心代码参照 API-Guide 的两个例子程序,特此声明。Option Explicit查找第一个文件的 APIPrivate Declare Function Fin
2、dFirstFile Lib “kernel32“ Alias “FindFirstFileA“ (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long查找下一个文件的 APIPrivate Declare Function FindNextFile Lib “kernel32“ Alias “FindNextFileA“ (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long获取文件属性的 APIPrivate Declar
3、e Function GetFileAttributes Lib “kernel32“ Alias “GetFileAttributesA“ (ByVal lpFileName As String) As Long关闭查找文件的 APIPrivate Declare Function FindClose Lib “kernel32“ (ByVal hFindFile As Long) As Long以下为调用浏览文件夹窗口的 APIPrivate Declare Sub CoTaskMemFree Lib “ole32.dll“ (ByVal hMem As Long)Private Decl
4、are Function lstrcat Lib “kernel32“ Alias “lstrcatA“ (ByVal lpString1 As String, ByVal lpString2 As String) As LongPrivate Declare Function SHBrowseForFolder Lib “shell32“ (lpbi As BrowseInfo) As LongPrivate Declare Function SHGetPathFromIDList Lib “shell32“ (ByVal pidList As Long, ByVal lpBuffer As
5、 String) As Long常量Const MAX_PATH = 260Const MAXDWORD = &HFFFFConst INVALID_HANDLE_VALUE = -1Const FILE_ATTRIBUTE_ARCHIVE = &H20Const FILE_ATTRIBUTE_DIRECTORY = &H10Const FILE_ATTRIBUTE_HIDDEN = &H2Const FILE_ATTRIBUTE_NORMAL = &H80Const FILE_ATTRIBUTE_READONLY = &H1Const FILE_ATTRIBUTE_SYSTEM = &H4C
6、onst FILE_ATTRIBUTE_TEMPORARY = &H100Const BIF_RETURNONLYFSDIRS = 1Private Type FILETIMEdwLowDateTime As LongdwHighDateTime As LongEnd Type定义类(用于查找文件)Private Type WIN32_FIND_DATAdwFileAttributes As LongftCreationTime As FILETIMEftLastAccessTime As FILETIMEftLastWriteTime As FILETIMEnFileSizeHigh As
7、LongnFileSizeLow As LongdwReserved0 As LongdwReserved1 As LongcFileName As String * MAX_PATHcAlternate As String * 14End Type定义类(用于浏览文件夹窗口)Private Type BrowseInfohWndOwner As LongpIDLRoot As LongpszDisplayName As LonglpszTitle As LongulFlags As LonglpfnCallback As LonglParam As LongiImage As LongEnd
8、 Type自定义函数Function StripNulls(OriginalStr As String) As StringIf (InStr(OriginalStr, Chr(0) 0) ThenOriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0) - 1)End IfStripNulls = OriginalStrEnd Function自定义函数Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, _DirCount A
9、s Integer)Dim FileName As String 文件名Dim DirName As String 子目录名Dim dirNames() As String 目录数组Dim nDir As Integer 当前路径的目录数Dim i As Integer 循环计数器变量Dim hSearch As Long 搜索句柄变量Dim WFD As WIN32_FIND_DATADim Cont As IntegerIf Right(path, 1) “ Then path = path & “搜索子目录nDir = 0ReDim dirNames(nDir)Cont = TruehS
10、earch = FindFirstFile(path & “*“, WFD)If hSearch INVALID_HANDLE_VALUE ThenDo While ContDirName = StripNulls(WFD.cFileName)If (DirName “.“) And (DirName “.“) ThenIf GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY ThendirNames(nDir) = DirNameDirCount = DirCount + 1nDir = nDir + 1ReDim P
11、reserve dirNames(nDir)End IfEnd IfCont = FindNextFile(hSearch, WFD) 获取下一个子目录LoopCont = FindClose(hSearch)End If 遍历目录并累计文件总数hSearch = FindFirstFile(path & SearchStr, WFD)Cont = TrueIf hSearch INVALID_HANDLE_VALUE ThenWhile ContFileName = StripNulls(WFD.cFileName)If (FileName “.“) And (FileName “.“) T
12、henFindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLowFileCount = FileCount + 1List1.AddItem path & FileNameEnd IfCont = FindNextFile(hSearch, WFD) 获取下一个文件WendCont = FindClose(hSearch)End If如果子目录存在则遍历之If nDir 0 ThenFor i = 0 To nDir - 1FindFilesAPI = FindFilesAPI + FindFi
13、lesAPI(path & dirNames(i) & “, _SearchStr, FileCount, DirCount)Next iEnd IfEnd Function查找按钮代码Sub Command1_Click()Dim SearchPath As String, FindStr As StringDim FileSize As LongDim NumFiles As Integer, NumDirs As IntegerDim iNull As Integer, lpIDList As Long, lResult As LongDim sPath As String, udtBI
14、 As BrowseInfoWith udtBI设置浏览窗口.hWndOwner = Me.hWnd返回选中的目录.ulFlags = BIF_RETURNONLYFSDIRSEnd With调出浏览窗口lpIDList = SHBrowseForFolder(udtBI)If lpIDList ThensPath = String$(MAX_PATH, 0)获取路径SHGetPathFromIDList lpIDList, sPath释放内存CoTaskMemFree lpIDListiNull = InStr(sPath, vbNullChar)If iNull ThensPath = L
15、eft$(sPath, iNull - 1)End IfEnd IfScreen.MousePointer = vbHourglassList1.ClearSearchPath = sPath 选中的目录为搜索的起始路径FindStr = “*.*“ 搜索所有类型的文件(此处可另作定义)FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)Text1.Text = “查找到的文件数:“ & NumFiles & vbCrLf & “查找的目录数:“ & _NumDirs + 1 & vbCrLf & “文件大小总共为:“ & vbCrLf & _Format(FileSize, “#,#,#,#0“) & “字节“Screen.MousePointer = vbDefaultEnd Sub