Skip to content

Dir

返回一个String,表示与指定模式或文件属性匹配的文件、目录或文件夹的名称,或驱动器的卷标。

语法:Dir [ ( pathname [ , attributes ] ) ]

pathname
可选 字符串表达式,指定文件名;可以包含目录或文件夹以及驱动器。如果未找到pathname,则返回零长度字符串("")。
attributes
可选 常量或数值表达式,其和指定文件属性。如果省略,返回与pathname匹配但无属性的文件。

attributes参数设置如下:

常量描述
vbNormal0(默认)指定无属性的文件。
vbReadOnly1指定只读文件以及无属性的文件。
vbHidden2指定隐藏文件以及无属性的文件。
vbSystem4指定系统文件以及无属性的文件。
vbVolume8指定卷标;如果指定了任何其他属性,vbVolume将被忽略。
vbDirectory16指定目录或文件夹以及无属性的文件。

Dir支持使用多字符(*)和单字符(?)通配符指定多个文件。

第一次调用Dir必须指定pathname,否则会产生错误。当同时指定文件属性时,必须包含pathname

Dir返回与pathname匹配的第一个文件名。要获取与pathname匹配的其他文件名,请不带参数再次调用Dir。当没有更多文件名匹配时,Dir返回零长度字符串("")。返回零长度字符串后,后续调用必须指定pathname,否则会产生错误。

可以在不先检索当前pathname的所有匹配文件名的情况下指定新的pathname。但是,Dir不能递归调用。使用vbDirectory属性调用Dir不会持续返回子目录。

TIP

由于Windows上文件名按不区分大小写的顺序检索,建议将返回的文件名存储在数组中并对数组排序。

另请参阅

示例

本示例使用Dir函数检查某些文件和目录是否存在,以及枚举文件夹中的文件。

vb
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, including files 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                       ' represents a directory.
    End If
    MyName = Dir                     ' Get next entry.
Loop

twinBASIC及其LOGO版权为作者"韦恩"所有