Help and Support

文章編號: 185601 - 上次校閱: 2007年5月18日 - 版次: 2.0

HOW TO:使用 FileSystemObject 遞迴搜尋目錄

在此頁中

全部展開 | 全部摺疊

結論

本文將逐步告訴您,如何使用 FileSystemObject 類別遞迴搜尋目錄及尋找特定檔案。

關於 FileSystemObject 類別的資訊

FileSystemObject 類別位於 Microsoft Scripting Runtime (Scrrun.dll) 中。如果要取得 Scrrun.dll 檔案,請安裝下列任何一個套件:
  • Microsoft Windows Script Host
  • Microsoft Windows NT Option Pack
  • Microsoft Internet Information Server 3.0
  • Scripting 3.1 升級
  • Microsoft Visual Studio 98
  • Microsoft Visual Basic 6.0
FileSystemObject 類別能夠提供比使用 DirGetAttr 等 Visual Basic 內建函式更佳的效能。此外,FileSystemObject 比 Visual Basic 內建函式更容易實作。

建立範例

  1. 在 Visual Basic 中建立新的標準 EXE 專案。根據預設會建立 Form1。
  2. [專案] 功能表上,按一下 [參考],然後新增 Microsoft Scripting Runtime 的參考。如果未列出這個選項,請在您的系統上找出 Scrrun.dll 檔案。如有必要,請安裝<關於 FileSystemObject 類別的資訊>一節中列出的其中一個工具。
  3. 在 Form1 中加入 CommandButtonLabelListBox 控制項。請將 Label 控制項的寬度調整為與表單的寬度相同。
  4. 將下列程式碼加入至 Form1 的 [一般宣告] 區段:
    Option Explicit
    
    Dim fso As New FileSystemObject
    Dim fld As Folder
    
    Private Sub Command1_Click()
       Dim nDirs As Long, nFiles As Long, lSize As Currency
       Dim sDir As String, sSrchString As String
       sDir = InputBox("Type the directory that you want to search for", _
                       "FileSystemObjects example", "C:\")
       sSrchString = InputBox("Type the file name that you want to search for", _
                       "FileSystemObjects example", "vb.ini")
       MousePointer = vbHourglass
       Label1.Caption = "Searching " & vbCrLf & UCase(sDir) & "..."
       lSize = FindFile(sDir, sSrchString, nDirs, nFiles)
       MousePointer = vbDefault
       MsgBox Str(nFiles) & " files found in" & Str(nDirs) & _
              " directories", vbInformation
       MsgBox "Total Size = " & lSize & " bytes"
    End Sub
    
    Private Function FindFile(ByVal sFol As String, sFile As String, _
       nDirs As Long, nFiles As Long) As Currency
       Dim tFld As Folder, tFil As File, FileName As String
       
       On Error GoTo Catch
       Set fld = fso.GetFolder(sFol)
       FileName = Dir(fso.BuildPath(fld.Path, sFile), vbNormal Or _
                      vbHidden Or vbSystem Or vbReadOnly)
       While Len(FileName) <> 0
          FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, _
          FileName))
          nFiles = nFiles + 1
          List1.AddItem fso.BuildPath(fld.Path, FileName)  ' Load ListBox
          FileName = Dir()  ' Get next file
          DoEvents
       Wend
       Label1 = "Searching " & vbCrLf & fld.Path & "..."
       nDirs = nDirs + 1
       If fld.SubFolders.Count > 0 Then
          For Each tFld In fld.SubFolders
             DoEvents
             FindFile = FindFile + FindFile(tFld.Path, sFile, nDirs, nFiles)
          Next
       End If
       Exit Function
    Catch:  FileName = ""
           Resume Next
    End Function
    					
  5. 執行專案,然後按一下 [Command1]
  6. 輸入要搜尋的目錄和檔案名稱。請注意,每找到一個檔案,該檔案名稱就會加入清單方塊中。程序完成時,訊息方塊會顯示找到的檔案數目,以及檔案的全部大小。

疑難排解

  • 如果應用程式嘗試存取某些檔案和目錄 (例如 Microsoft Windows XP 上的「系統磁碟區資訊」),就會造成存取違規。當發生問題時,錯誤處理程式碼會停止查看目錄。如果您有更穩固的解決方案,請務必使用不同的方法。
  • 如果您使用檔案篩選條件 (例如 *.*),可能會傳回大量的檔案。ListBox 控制項只能包含有限數目的字元。達到該限制時,將不會有任何項目加入至清單方塊中。
  • 這個範例程式碼是使用 Microsoft Scripting Runtime (Scrrun.dll) 5.6.0.6626 版進行測試。如果發生問題,您可能必須下載這個檔案的更新版本。

?考

如需有關可用於尋找特定檔案的其他方法的詳細資訊,請按一下下面的文件編號,檢視「Microsoft 知識庫」中的文件:
185476? (http://support.microsoft.com/kb/185476/ ) How To Search Directories to Find or List Files

這篇文章中的資訊適用於:
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
關鍵字:?
kbhowto kbhowtomaster KB185601
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。

文章翻譯