Dir, Dir$ Functions

See Also48EIPK              ExampleGU21FM>Low

Returns the name of a file or directory that matches a specified pattern and file attribute.  Also can return the volume label of a drive.

Syntax

Dir[$] [(filespec [,attrmask])]

Remarks

Dir returns a Variant8PHEAW3; Dir$ returns a String.

The Dir[$] function has these parts:

Part               Description

 

filespec          String expression1330R89 that specifies a path or file name.  The path and file name can include a drive specification and any valid wildcard characters.

attrmask        Specifies the attributes (Hidden, System, and so on) of the file names you want returned.  If attrmask is not specified, Dir[$] returns all normal files (those without hidden, system, directory, or volume label attributes) that match filespec.  You can add to the list of file names returned by Dir[$] by specifying a value for attrmask that includes directories, and hidden or system files.  In addition, you can specify that Dir[$] return the label of the volume whose drive is specified as part of filespec, or the currently selected drive if no drive is explicitly specified.  If attrmask specifies the volume label, all other attributes are ignored.  The attrmask is created by summing the values, shown in the table below, to identify the particular file attribute(s) you want included.

Symbolic Constant

Value

Meaning

 

ATTR_NORMAL

0

Normal files

ATTR_HIDDEN

2

Hidden files

ATTR_SYSTEM

4

System files

ATTR_VOLUME

8

Volume label

ATTR_DIRECTORY

16

Directory

 

Note   Symbolic constants for attribute definitions can be found in the Visual Basic file CONSTANT.TXT.  When placed in any module in a project, the symbolic names can be used in all your form and code modules.

 

You must specify filespec the first time you call the function, or an error occurs.  If you also specify file attributes, attrmask can only be included in the same call to Dir[$] where filespec is included.  Dir[$] returns the first file name that matches filespec.  To get any additional file names that match filespec, call Dir[$] again with no arguments.  When no more file names match, Dir[$] returns a zero-length String or Variant of VarType7A68ZTZ 8.  Once a zero-length String or Variant has been returned, you must again use a filespec argument in subsequent calls or an error occurs.

 

Note   You can change to a new filespec without retrieving all of the file names that match the current filespec.  However, you cannot recursively call the Dir[$] function.

 

Tip     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

ChDir StatementPMFGU6

CurDir, CurDir$ Functions3DE0TX


Dir, Dir$ Functions Example

The example uses Dir to determine all the subdirectories on drive C.  To try this example, paste the code into the Declarations section of a form containing a list box named List1.  Then press F5 and click the form.

 

Const ATTR_DIRECTORY = 16                ' Declare form constant.

Sub Form_Click ()

   ListSubDirs "C:\"                     ' Call ListSubDirs

End Sub

Sub ListSubDirs (Path)

   Dim Count, D(), I, DirName            ' Declare variables.

   DirName = Dir(Path, ATTR_DIRECTORY)   ' Get first directory name.

   'Iterate through PATH, caching all subdirectories in D()

   Do While DirName <> ""

      If DirName <> "." And DirName <> ".." Then

         If GetAttr(Path + DirName) And ATTR_DIRECTORY = ATTR_DIRECTORY Then

            If (Count Mod 10) = 0 Then

               ReDim Preserve D(Count + 10)    ' Resize the array.

            End If

            Count = Count + 1            ' Increment counter.

            D(Count) = DirName

         End If

      End If

      DirName = Dir                      ' Get another directory name.

   Loop

   ' Now recursively iterate through each cached subdirectory.

   For I = 1 To Count

      List1.AddItem Path & D(I)          ' Put name in list box.

      ListSubDirs Path & D(I) & "\"

   Next I

End Sub