Sign in with Microsoft
Sign in or create an account.
Hello,
Select a different account.
You have multiple accounts
Choose the account you want to sign in with.

附註: 如果 Microsoft Jet Expression Service 在沙箱模式中執行,會停用本主題中所述的函數、方法、物件或屬性,以免評估可能不安全的表達式。 如需沙盒模式的詳細資訊,請在 [說明] 中搜尋「沙盒模式」。

傳回一個 字串 ,代表符合指定模式或檔屬性的檔案、目錄或資料夾名稱,或是磁碟驅動器的磁碟區標籤。

語法

Dir[ (pathname [, attributes] ) ]

Dir 函數語法具有下列自變數:

引數

描述

路徑

選擇性。 指定檔名的字串表示式可能包括目錄或資料夾,以及磁碟驅動器。 如果找不到 pathname ,就會傳回零長度字串 (“”) 。

屬性

選擇性。 Constant 或 數值運算式,其加總會指定檔案屬性。 如果省略,會傳回符合 pathname 但沒有屬性的檔案。


設定

屬性引數 設定如下:

常數

描述

vbNormal

0

(預設) 指定沒有屬性的檔案。

vbReadOnly

1

除了沒有屬性的檔案外,還指定唯讀檔案。

vbHidden

2

除了沒有屬性的檔案外,還指定隱藏的檔案。

VbSystem

4

除了沒有屬性的檔案外,還指定系統檔案。 無法在 Macintosh 上使用。

vbVolume

8

指定音量標籤;如果已指定任何其他屬性, 則會忽略 vbVolume 。 無法在 Macintosh 上使用。

vbDirectory

16

除了沒有屬性的檔案外,還指定目錄或資料夾。

vbAlias

64

指定的檔名是別名。 僅適用於Macintosh。


附註:  這些常數是由 Visual Basic for Applications (VBA) 所指定,可用於程式代碼中的任何位置來代替實際值。

註解

在 Microsoft Windows 中, Dir 支援使用多個字元 (*) 和單一字元 () 通配符來指定多個檔案。 在 Macintosh 上,這些字元會被視為有效的檔案名字符,無法做為通配符來指定多個檔案。

由於 Macintosh 不支援通配符,請使用該文件類型來識別檔案群組。 您可以使用 MacID 函數來指定檔案類型,而不是使用檔案名。 例如,下列語句會傳回目前資料夾中第一個 TEXT 檔案的名稱:

Dir("SomePath", MacID("TEXT"))

若要反覆運算資料夾中的所有檔案,請指定空白字串:

Dir("")

如果您在 Windows 中搭配 Dir 使用 MacID 函數,則會發生錯誤。

任何大於 256 的 屬性 值都視為 MacID 值。

您必須在第一次呼叫 Dir 函數時指定路徑名稱,否則會發生錯誤。 如果您也指定了檔案屬性,則必須包含 pathname

Dir 會 傳回第一個符合 pathname 的檔名。 若要取得任何其他符合 路徑名稱的檔名,請再次撥打 Dir ,但不使用自變數。 當不再有檔名相符時, Dir 會 傳回零長度字串 (“) 。 傳回零長度字串后,您必須在後續通話中指定 路徑名稱 或發生錯誤。 您可以變更為新的 路徑名稱 ,而不需要擷取符合目前 路徑名稱的所有檔名。 不過,您無法週期性地呼叫 Dir 函數。 使用 vbDirectory 屬性呼叫 Dir 並不會持續傳回子導向。

祕訣     因為不會以特定順序擷取檔名,您可能會想要將傳回的檔名儲存在 陣列,然後排序數位。

範例

附註: 下列範例示範如何在 Visual Basic for Applications (VBA) 模組中使用此函數。 如需使用 VBA 的詳細資訊,請在 [搜尋] 旁的下拉式清單中選取 [開發人員參考],並在 [搜尋] 方塊中輸入一個或多個字詞。

此範例使用 Dir 函數來檢查某些檔案和目錄是否存在。 在 Macintosh 上,“HD:” 是預設的磁碟驅動器名稱,路徑名稱的一部分會以冒號而非反斜杠分隔。 此外,在 Macintosh 上,Windows 通配符會視為有效的檔名字符。 不過,您可以使用 MacID 函數來指定檔案群組。

Dim MyFile, MyPath, MyName
' Returns "WIN.INI" (on Microsoft Windows) if it exists.
MyFile = Dir("C:\WINDOWS\WIN.INI")
' Returns filename with specified extension. If more than one *.ini
' file exists, the first file found is returned.
MyFile = Dir("C:\WINDOWS\*.INI")
' Call Dir again without arguments to return the next *.INI file in the
' same directory.
MyFile = Dir
' Return first *.TXT file with a set hidden attribute.
MyFile = Dir("*.TXT", vbHidden)
' Display the names in C:\ that represent directories.
MyPath = "c:\" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
' Ignore the current directory and the encompassing directory.
If MyName <> "." And MyName <> ".." Then
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' Display entry only if it
End If ' it represents a directory.
End If
MyName = Dir ' Get next entry.
Loop

Need more help?

Want more options?

探索訂閱權益、瀏覽訓練課程、瞭解如何保護您的裝置等等。

社群可協助您詢問並回答問題、提供意見反應,以及聆聽來自具有豐富知識的專家意見。

Was this information helpful?

How satisfied are you with the translation quality?
What affected your experience?
By pressing submit, your feedback will be used to improve Microsoft products and services. Your IT admin will be able to collect this data. Privacy Statement.

Thank you for your feedback!

×