_finddata_t 列出清单是這個嗎?
不要選我當版主
//-------------------------------------- // 程序功能:調用Win32功能用通配符查找一批文件的文件名 // 說明:1.採用DOS通配符,即包含*和?號的文件,比如*.c是所有以.c為擴展名的文件,可帶路徑 // 2.用命令行指定文件樣式,若省略,默認為*.* // 3.默認將結果輸出到控制臺,若用重定向,可輸出到文本文件,語法類如為test *.c > list.txt,即寫到list.txt上 // 4.獲得文件清單,可用數組循環處理(本例是寫到標準容器vector中,相當於文件名數組) // 5.要獲取文件的其他屬性,可查閱MSDN中WIN32_FIND_DATA結構的內容 // 6.本程序使用寬字符以及安全版本的函數,然而用重定向輸出得到的文本文件是可以用Ansi模式讀入的 //-------------------------------------- #include <Windows.h> #include <stdio.h> #include <string.h> #include <vector> BOOL GetFileList(const wchar_t FileName[], std::vector<WIN32_FIND_DATAW>& FileList); int wmain(int argc, wchar_t* argv[]) { wchar_t FileName[FILENAME_MAX]; wcscpy_s(FileName, FILENAME_MAX - 1, (argc < 2) ? L"*.*" : argv[1]); std::vector<WIN32_FIND_DATAW> FileList; FileList.clear(); // 清空集合,如果需要追加模式,重覆調用GetFileList()函數而不要再清空 if (GetFileList(FileName, FileList)) { for (std::vector<std::wstring>::size_type index = 0; index != FileList.size(); ++index) { _putws(FileList[index].cFileName); } } return 0; } // 獲取文件清單 // 備註:結果通過集合FileList返回,傳遞的是引用 BOOL GetFileList(const wchar_t FileName[], std::vector<WIN32_FIND_DATAW>& FileList) { WIN32_FIND_DATAW FindFileData; // 文件數據結構,類型聲明末尾為W表示是寬字符版本,若為A則是Ansi版本的 HANDLE Handle; // 用於搜索文件的句柄,將此句柄供給FindNextFile()函數,文件信息儲存在上面結構中 Handle = FindFirstFileW(FileName, &FindFileData); if (Handle != INVALID_HANDLE_VALUE) { do { FileList.push_back(FindFileData); } while (FindNextFileW(Handle, &FindFileData)); return true; } else return false; }
Option Explicit Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long Const MaxLFNPath = 260 Const INVALID_HANDLE_VALUE = -1 Private Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type Private 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 * MaxLFNPath cShortFileName As String * 14 End Type Dim WFD As WIN32_FIND_DATA Dim bgndir$, curpath$, schpattern$, aa$, fname$, progdisk$ Dim hItem&, hFile&, rtn&, i%, j%, k%, tfiles&, tfsize#, stopyn As Boolean 'Boolean 数据类型 (Visual Basic)存放只可能为 True 或 False 的值 Dim X1&, buff$ 'Dim x1& 是Dim x1 As Long“长整型”& 是 As Long的缩写,! 是 as single 的缩写,例如:dim x0!,x1!,t!(或:dim x0 as single,x1 as single,t as single) Private Sub cmdBower_Click() Dim Path As String Path = BrowseForFolder(Me.hwnd, "Select Project's Location :", , NEWFOLDER) If (Trim(Path) <> "") Then txtTargetPath.Text = IIf(Right(Trim(Path), 1) <> "\", Path & "\", Path) txtTargetPath.ToolTipText = txtTargetPath.Text cmdSearch.Enabled = True SelectTargetPath = Path End If End Sub Private Sub CmdExit_Click() Call WriteKTPList Unload A_frmSearchKTP A_MainForm.Show End Sub Private Sub WriteKTPList() Dim FileName As String, TempString As String Dim i As Integer, j As Integer, FileNum As Integer FileName = App.Path & "\" & "CSV" If IsFolderExist(FileName) = False Then MkDir FileName FileName = FileName & "\KTPList.ini" FileNum = FreeFile If List1.List(0) <> "" Then j = 1 Open FileName For Output As #FileNum For i = 0 To List1.ListCount If List1.List(i) <> "" Then Print #FileNum, j & "=" & List1.List(i) j = j + 1 End If Next i Close #FileNum End If End Sub Private Sub cmdSearch_Click() Dim s As String On Error Resume Next List1.Clear '清空list1里面的内容 tfiles = 0: tfsize = 0 '初始化统计文件数为0,文件大小为0,其中冒号是将两个语句分隔开 stopyn = False 'stopyn估计是按钮的停止属性跟cancel差不多吧 CmdExit.Enabled = Not CmdExit.Enabled cmdBower.Enabled = Not cmdBower.Enabled cmdSearch.Enabled = Not cmdSearch.Enabled cmdSTOP.Enabled = Not cmdSTOP.Enabled Text1.Locked = Not Text1.Locked Text2.Locked = Not Text2.Locked If InStr(Text1.Text, ".") = 0 Then Text1.Text = Trim(Text1.Text) & "*.*" '在text1中查找"."如果"."是第一个则.... s = Trim(txtTargetPath.Text) bgndir = s '开始搜的文件夹 If InStr(bgndir, ":") = 0 And Len(bgndir) = 1 Then bgndir = bgndir & ":" If Right(bgndir, 1) <> "\" Then bgndir = bgndir & "\" schpattern = Trim(Text1.Text) '模糊搜索条件,例如 *.* 或 *.mp3 或 sc*.* Call SearchDirs(bgndir) If tfiles > 0 Then MsgBox "搜索完成,共查找到" & str(tfiles) & " 个文件" & vbCrLf & Chr(10) & "总占空间: " & Format(str(tfsize), "#,###") & " Bytes" Else MsgBox "搜索完成,未找到符合的文件" End If cmdBower.Enabled = Not cmdBower.Enabled cmdSearch.Enabled = Not cmdSearch.Enabled cmdSTOP.Enabled = Not cmdSTOP.Enabled CmdExit.Enabled = Not CmdExit.Enabled Text1.Locked = Not Text1.Locked Text2.Locked = Not Text2.Locked Me.Caption = "快速搜索文件" End Sub Private Sub cmdSTOP_Click() stopyn = True End Sub Private Sub Form_Load() Call init '窗体加载时,首先调用init配置过程 End Sub Private Sub SearchDirs(curpath) Dim dirs%, dircount%, dirbuf$() Dim FilterName As String On Error Resume Next Me.Caption = "正在查找 " & curpath DoEvents hItem = FindFirstFile(curpath & "*", WFD) If hItem <> INVALID_HANDLE_VALUE Then Do DoEvents If stopyn Then Exit Do FilterName = IIf(Trim(Text2.Text) <> "", Trim(Text2.Text), "") If FilterName <> "" Then If InStr(WFD.cFileName, FilterName) = 0 Then If (WFD.dwFileAttributes And vbDirectory) And Asc(WFD.cFileName) <> 46 Then If (dirs Mod 10) = 0 Then ReDim Preserve dirbuf(dirs + 10) dirs = dirs + 1 dirbuf(dirs) = Left(WFD.cFileName, InStr(WFD.cFileName, vbNullChar) - 1) End If End If Else If (WFD.dwFileAttributes And vbDirectory) And Asc(WFD.cFileName) <> 46 Then If (dirs Mod 10) = 0 Then ReDim Preserve dirbuf(dirs + 10) dirs = dirs + 1 dirbuf(dirs) = Left(WFD.cFileName, InStr(WFD.cFileName, vbNullChar) - 1) End If End If Loop While FindNextFile(hItem, WFD) Call FindClose(hItem) Call mohusearch(curpath) End If For dircount = 1 To dirs DoEvents If stopyn Then Exit For SearchDirs curpath & dirbuf$(dircount) & "\" Next dircount End Sub Private Sub mohusearch(curpath) On Error Resume Next hFile = FindFirstFile(curpath & schpattern, WFD) If hFile <> INVALID_HANDLE_VALUE Then Do DoEvents If stopyn Then Exit Do aa = Trim(Trim(curpath) & Trim(WFD.cFileName)) If (WFD.dwFileAttributes And vbDirectory) Or Asc(WFD.cFileName) = 46 Then Else k = InStr(aa, Chr(0)) If k > 0 Then fname = Mid(aa, 1, k - 1) aa = fname '& " ----- " & Format(str(FileLen(fname)), "#,###") & " Bytes" tfiles = tfiles + 1 tfsize = tfsize + FileLen(fname) List1.AddItem aa List1.Selected(List1.ListCount - 1) = True End If End If Loop While FindNextFile(hFile, WFD) Call FindClose(hFile) End If End Sub Private Sub List1_dblClick() If List1.ListCount > 0 Then j = List1.ListIndex fname = Trim(List1.List(j)) j = InStr(fname, "-----") If j > 0 Then fname = Trim(Mid(fname, 1, j - 1)) Shell "explorer " & fname, vbNormalNoFocus End If End If End Sub Private Sub init() '配置窗体加载 Text1.Text = "*.KTP" 'cmdSearch.Enabled = False cmdSTOP.Enabled = False End Sub