Skip to content

DIR


在文件系统中搜索并返回条目信息;执行目录搜索

语法

vb
# include "dir.bi"

declare function Dir ( byref item_spec as const string, byval attrib_mask as integer = fbNormal, byref out_attrib as integer ) as string
declare function Dir ( byref item_spec as const string, byval attrib_mask as integer = fbNormal, byval p_out_attrib as integer ptr = 0 ) as string
declare function Dir ( byref out_attrib as integer ) as string
declare function Dir ( byval p_out_attrib as integer ptr = 0 ) as string

用法

result = Dir( item_spec, [ attrib_mask ], out_attrib )
result = Dir( item_spec [, [ attrib_mask ] [, p_out_attrib ] ] )
result = Dir( out_attrib )
result = Dir( [ p_out_attrib ] )

参数

item_spec

用于匹配条目名称的模式。

attrib_mask

用于匹配条目属性的位掩码。

out_attrib

对位掩码的引用,用于接收找到的条目属性(若有)。

p_out_attrib

指向位掩码的指针,用于接收找到的条目属性(若有)。

返回值

若未找到匹配 item_spec 名称或 attrib_mask 属性的条目,则 out_attrib(或 *p_out_attrib)被赋值为零,并返回空字符串。否则,out_attrib(或 *p_out_attrib)被赋值为该条目的属性掩码,并返回不含路径的条目名称。

说明

Dir 返回第一个匹配所传入 item_spec 的文件系统条目。要获取匹配同一 item_spec 模式的后续条目,可不带该参数(或传入空字符串)再次调用 Dir

因此,要获取目录中的条目列表,需多次调用 Dir,每次返回一个条目。

item_spec 含绝对路径,则第一次调用在文件系统中搜索名称匹配且属性包含在 attrib_mask(默认为 fbNormal)中的条目;否则从当前目录(参见 CurDir)开始搜索。若未找到匹配条目,out_attrib 被赋值为零并返回空字符串;否则 out_attrib 被赋值为该条目的属性标志,并返回不含路径的条目名称。

item_spec 可包含星号(*,匹配任意相邻字符)或一个或多个问号(?,匹配任意单个字符)。若包含这些通配符,函数搜索第一个匹配的条目。

若找到条目,后续不带 item_specitem_spec 为空字符串的调用将依次返回下一个匹配 item_spec 的条目,直到没有更多匹配项。若后续调用省略 attrib_mask,则使用上次调用中相同的属性进行搜索。

第二种语法与 Dir( item_spec, attrib_mask, *p_out_attrib ) 等价。

第三种语法与 Dir( "", , out_attrib ) 等价。

第四种语法与 Dir( "", , *p_out_attrib ) 等价。

文件属性: 文件、目录和其他条目都具有所谓的文件属性——描述该条目的元数据。这些元数据的含义可能因操作系统和文件系统而异。

以下定义的常量在 attrib_maskout_attrib(或 *p_out_attrib)中用作位标志。它们的值可以使用 Operator Or 组合成掩码。这些值是允许返回的文件所具有的元数据。要访问这些定义的标志,必须 #Include "dir.bi"

以下说明中,"文件"泛指任何文件系统条目:普通文件、目录等。

attrib_mask 指定允许返回的文件属性集合(若文件具有 attrib_mask 中未指定的属性,则该文件将被排除)。

例如:

fbDirectory 只允许未设置目录属性,即只匹配没有设置任何其他属性的文件或目录。

(fbReadOnly Or fbDirectory) 将匹配只读目录和文件,以及可写目录和文件。

fbArchive Or fbDirectory 将匹配已归档文件、已归档目录、未归档文件和未归档目录(不匹配只读文件等)。

逻辑条件:

DIR 返回文件名的条件:((attrib_mask) OR (file_attrib)) = (attrib_mask) 为真

DIR 返回文件名的条件:((file_attrib) AND (Not attrib_mask)) = 0

通过检查返回的 out_attrib 中的特定标志(使用 Operator And),可以实现更精细的过滤。

# define fbReadOnly &h01

该条目不可写入或删除。

DOS 和 Windows:该条目设置了"只读"属性。

Linux:该条目对当前用户或组没有写入权限,也没有全局写入权限。(是否具有 root 权限忽略不计。)

# define fbHidden &h02

该条目在普通目录列表中被隐藏。

DOS 和 Windows:该条目设置了"隐藏"属性。

Linux:该条目名称以句点(.)开头。

# define fbSystem &h04

该条目几乎仅由系统使用。

DOS 和 Windows:该条目设置了"系统"属性。

Linux:该条目为字符设备、块设备、命名管道(FIFO)或 Unix 套接字。

# define fbDirectory &h10

该条目为目录,包括当前目录(.)和父目录(..)。

DOS、Windows 和 Linux:该条目为目录。

# define fbArchive &h20

某些自动化操作后该条目可能被备份。

DOS 和 Windows:该条目设置了"归档"属性(文件每次被写入后自动设置)。

Linux:该条目不是目录;典型文件系统不支持此元数据。

# define fbNormal (fbReadOnly or fbArchive)

该条目为只读或"已归档"。

(若 attrib_mask 不包含 fbArchiveDir 可能扩展检查以包含 fbDirectory,但建议显式添加 fbDirectory,如果需要此行为。)

没有属性的条目始终匹配,无论 attrib_mask 的值为何。

若条目具有一个或多个 attrib_mask 中未指定的属性,则该条目不匹配。

通常无法使用 attrib_mask 在包含某一属性集的文件/文件夹的同时排除具有不同属性集的文件/文件夹。例如,无法只扫描只读目录而排除只读文件(除非这些文件还具有其他属性)。

通过检查 out_attrib 值中所需的属性集,可以获得更精细的控制。

示例

vb
' Number of files in the current directory.

Dim As Integer FileCount

If Dir("*") <> "" Then  ' Start a file search with no specified filespec/attrib *AND* get the first filename.
    Filecount = 1
    While Dir() <> ""  ' If dir() is "", exit the loop: no more filenames are left to be read.
        FileCount += 1  ' Increment the counter of number of files
    Wend
End If

Print FileCount & " files in the current directory."

Sleep
vb
#include "dir.bi" 'provides constants to use for the attrib_mask parameter

Sub list_files (ByRef filespec As String, ByVal attrib As Integer)
    Dim As String filename = Dir(filespec, attrib) ' Start a file search with the specified filespec/attrib *AND* get the first filename.
    Do While Len(filename) > 0 ' If len(filename) is 0, exit the loop: no more filenames are left to be read.
        Print filename
        filename = Dir() ' Search for (and get) the next item matching the initially specified filespec/attrib.
    Loop
End Sub

Print "directories:"
list_files "*", fbDirectory

Print
Print "archive files:"
list_files "*", fbArchive
vb
'' show any files that have directory attribute and don't care if it is system, hidden, read-only, or archive

#include once "dir.bi"

'' allow everything
Var mask = fbDirectory Or fbHidden Or fbSystem Or fbArchive Or fbReadOnly

Var attrib = 0
Var f = Dir( "*.*", mask, attrib )
While( f > "" )
    '' show any files that have at least a directory attribute
    If( attrib And fbDirectory ) Then
        Print f
    End If
    f = Dir( attrib )
Wend
vb
'' Example of using DIR function and retrieving attributes

#include "dir.bi" '' provides constants to match the attributes against

'' set input attribute mask to allow files that are normal, hidden, system or directory
Const attrib_mask = fbNormal Or fbHidden Or fbSystem Or fbDirectory ' = &h37

Dim As UInteger out_attr '' unsigned integer to hold retrieved attributes

Dim As String fname '' file/directory name returned with
Dim As Integer filecount, dircount

fname = Dir("*.*", attrib_mask, out_attr) '' Get first file name/attributes, according to supplied file spec and attribute mask

Print "File listing in " & CurDir & ":"

Do Until Len(fname) = 0 '' loop until Dir returns empty string

    If (fname <> ".") And (fname <> "..") Then '' ignore current and parent directory entries

        Print fname,

        If (out_attr And fbDirectory) <> 0 Then
            Print "- directory";
            dircount += 1
        Else
            Print "- file";
            filecount += 1
        End If
        If (out_attr And fbReadOnly) <> 0 Then Print ", read-only";
        If (out_attr And fbHidden  ) <> 0 Then Print ", hidden";
        If (out_attr And fbSystem  ) <> 0 Then Print ", system";
        If (out_attr And fbArchive ) <> 0 Then Print ", archived";
        Print

    End If

    fname = Dir(out_attr) '' find next name/attributes

Loop

Print
Print "Found " & filecount & " files and " & dircount & " subdirs"

平台差异

  • Linux 要求 filename 大小写与文件实际名称匹配;Windows 和 DOS 不区分大小写。
  • Linux 中路径分隔符为正斜杠 /;Windows 使用反斜杠 \,但也允许正斜杠;DOS 使用反斜杠。
  • 在 DOS 中,属性掩码值 &h37&h3F 通常也有效,但 &h37 更安全)可返回所有文件和目录,包括 "." 和 "..",但不含卷标;值 8 返回卷标,即使当前目录不是根目录。

方言差异

  • -lang qb 方言中不可用,除非使用别名 __Dir 引用。

与 QB 的差异

  • QBasic 中不存在,但 Visual Basic 中存在。out_attrib 参数是 FreeBASIC 新增的。

另请参阅

  • Open
  • Curdir
  • Chdir
  • Mkdir
  • Rmdir

返回 目录

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系