Dir

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]])

The parts of the Dir function syntax are described in the following table.

Part Description
pathname Optional. String expression that specifies a file name—may include directory or folder, and drive. A zero-length string ("") is returned if pathname is not found.
attributes Optional. Numeric expression, whose sum specifies file attributes. If omitted, all files are returned that match pathname.

The attributes argument settings are described in the following table.

Constant Value Description
vbNormal 0 Normal
vbHidden 2 Hidden
vbSystem 4 System file
vbVolume 8 Volume label; if specified, all other attributes are ignored.
vbDirectory 16 Directory or folder

Remarks

Dir supports the use of multiple-character (*) and single-character (?) wildcards to specify multiple files.

You must specify pathname the first time you call the Dir function, or an error occurs. If you also specify file attributes, pathname must be included.

The Dir function returns the first file name that matches pathname. To get any additional file names that match pathname, call Dir again with no arguments. When no more file names match, Dir returns a zero-length string (""). Once a zero-length string is returned, you must specify pathname in subsequent calls or an error occurs. You can change to a new pathname without retrieving all of the file names that match the current pathname. However, you cannot call the Dir function recursively. Calling Dir with the vbDirectory attribute does not continually return subdirectories.