Dir 函数Dir function

返回一个 String,它表示与指定模式或文件属性或驱动器的卷标匹配的文件、目录或文件夹的名称。Returns a String representing the name of a file, directory, or folder that matches a specified pattern or file attribute, or the volume label of a drive.

语法Syntax

Dir [ (pathname, [ attributes ] ) ]Dir [ (pathname, [ attributes ] ) ]


Dir 函数语法具有以下部分:The Dir function syntax has these parts:

PartPart 说明Description
pathnamepathname 可选。Optional. 指定文件名的字符串表达式,可包括目录或文件夹和驱动器。String expression that specifies a file name; may include directory or folder, and drive. 如果未找到 pathname,则返回零长度字符串 ("")。A zero-length string ("") is returned if pathname is not found.
attributesattributes 可选。Optional. 其和指定文件属性的常量数值表达式Constant or numeric expression, whose sum specifies file attributes. 如果省略它,则返回与 pathname 匹配但没有属性的文件。If omitted, returns files that match pathname but have no attributes.

设置Settings

attributes 参数设置为:The attributes argument settings are:

常量Constant Value 说明Description
vbNormalvbNormal 00 (默认)指定没有属性的文件。(Default) Specifies files with no attributes.
vbReadOnlyvbReadOnly 11 指定只读文件以及不带属性的文件。Specifies read-only files in addition to files with no attributes.
vbHiddenvbHidden 22 指定隐藏文件以及不带属性的文件。Specifies hidden files in addition to files with no attributes.
vbSystemvbSystem 44 指定系统文件以及不带属性的文件。Specifies system files in addition to files with no attributes. 在 Macintosh 上不可用。Not available on the Macintosh.
vbVolumevbVolume 88 指定卷标;如果指定任何其他属性,则忽略 vbVolumeSpecifies volume label; if any other attribute is specified, vbVolume is ignored. 在 Macintosh 上不可用。Not available on the Macintosh.
vbDirectoryvbDirectory 1616 指定目录或文件夹以及不带属性的文件。Specifies directories or folders in addition to files with no attributes.
vbAliasvbAlias 6464 指定文件名为别名。Specified file name is an alias. 仅在 Macintosh 上可用。Available only on the Macintosh.

备注

这些常量由 Visual Basic for Applications 指定并可在代码中的任何位置使用来代替实际值。These constants are specified by Visual Basic for Applications and can be used anywhere in your code in place of the actual values.

注解Remarks

在 Microsoft Windows 中,Dir 支持使用多字符 (*) 和单字符 (?) 通配符来指定多个文件。In Microsoft Windows, Dir supports the use of multiple character (*) and single character (?) wildcards to specify multiple files. 在 Macintosh 上,这些字符被视为有效文件名称字符,且不能用作指定多个文件的通配符。On the Macintosh, these characters are treated as valid file name characters and can't be used as wildcards to specify multiple files.

因为 Macintosh 不支持通配符,请使用文件类型来标识文件组。Because the Macintosh doesn't support the wildcards, use the file type to identify groups of files. 可以使用 MacID 函数指定文件类型而不是使用文件名。You can use the MacID function to specify file type instead of using the file names. 例如,以下语句返回当前文件夹中的第一个 TEXT 文件的名称:For example, the following statement returns the name of the first TEXT file in the current folder:

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

若要循环访问文件夹中的所有文件,请指定一个空字符串:To iterate over all files in a folder, specify an empty string:

Dir("")

如果在 Microsoft Windows 中将 MacID 函数与 Dir 一起使用,将发生错误。If you use the MacID function with Dir in Microsoft Windows, an error occurs.

任何大于 256 的 attribute 值都将被视为 MacID 值。Any attribute value greater than 256 is considered a MacID value.

必须在首次调用 Dir 函数时指定 pathname,否则将发生错误。You must specify pathname the first time you call the Dir function, or an error occurs. 如果还指定文件属性,则必须包括 pathnameIf you also specify file attributes, pathname must be included.

Dir 返回与 pathname 匹配的第一个文件名。Dir returns the first file name that matches pathname. 若要获取与 pathname 匹配的任何其他文件名,请重新调用 Dir 而不使用参数。To get any additional file names that match pathname, call Dir again with no arguments. 当不再有匹配的文件名时,Dir 将返回零长度字符串 ("")。When no more file names match, Dir returns a zero-length string (""). 在返回零长度字符串后,必须在后续调用中指定 pathname,否则将发生错误。After a zero-length string is returned, you must specify pathname in subsequent calls, or an error occurs.

可以更改为新的 pathname,而无需检索与当前 pathname 匹配的所有文件名。You can change to a new pathname without retrieving all of the file names that match the current pathname. 但是,不能以递归方式调用 Dir 函数。However, you can't call the Dir function recursively. 调用带 vbDirectory 属性的 Dir 不会连续返回子目录。Calling Dir with the vbDirectory attribute does not continually return subdirectories.

在 Excel for Mac 2016 中,初始 Dir 函数调用将成功。With Excel for Mac 2016, the initial Dir function call will succeed. 但是,对指定目录进行迭代的后续调用将导致错误。Subsequent calls to iterate through the specified directory will cause an error, however. 很遗憾,这是一个已知 bug。This is a known bug unfortunately.

提示

由于不按特定顺序检索文件名,因此,您可能需要按数组返回文件名,然后对数组进行排序。Because file names are retrieved in no particular order, you may want to store returned file names in an array, and then sort the array.

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.