Dir, Dir$ Functions

Named Arguments

No

Syntax

Dir[(pathname[, attributes])]

pathname

Use: Optional

Data Type: String

A string expression that defines a path that may contain a drive name, a folder name, and a filename.

attributes

Use: Optional

Data Type: Numeric or Constant

A constant or numeric expression specifying the file attributes to be matched.

Return Value

Dir returns a variant of subtype string; Dir$ returns a string data type.

Description

Returns the name of a single file or folder matching the pattern or attribute passed to the function.

Rules at a Glance

  • A zero-length string ("") is returned if a matching file isn't found.

  • Possible values for attributes are:

    Constant Value Description
    vbNormal 0 Normal (not hidden and not a system file)
    vbHidden 2 Hidden
    vbSystem 4 System file
    vbVolume 8 Volume label; if specified, all other attributes are ignored
    vbDirectory 16 Directory or folder
  • The object browser and published documentation list several other constants that can be supplied as arguments to the attributes parameter. However, these either don't work on the Win9x/NT platforms (i.e., vbAlias, which is available only on the Macintosh) or have no effect on the operation of the function (vbReadOnly, vbArchive).

  • The attributes constants can be added together to create combinations of attributes to match; e.g., vbHidden + vbDirectory matches hidden directories.

  • If attributes isn't specified, files matching pathname are returned regardless of attributes.

  • You can use the wildcard ...

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.