Share via

Visual Basic for Applications Reference

Dir Function

See Also    Example    Specifics

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.


Dir[(pathname[, attributes])]

The Dir function syntax has these parts:

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. Constant or numeric expression, whose sum specifies file attributes. If omitted, returns files that match pathname but have no attributes.


The attributesargument settings are:

Constant Value Description
vbNormal 0 (Default) Specifies files with no attributes.
vbReadOnly 1 Specifies read-only files in addition to files with no attributes.
vbHidden 2 Specifies hidden files in addition to files with no attributes.
VbSystem 4 Specifies system files in addition to files with no attributes.
vbVolume 8 Specifies volume label; if any other attributed is specified, vbVolume is ignored.
vbDirectory 16 Specifies directories or folders in addition to files with no attributes.

Note   These constants are specified by Visual Basic for Applications and can be used anywhere in your code in place of the actual values.


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

Security Note   Do not make decisions about the contents of a file based on the file name extension. For example, a file named Form1.vb may not be a Visual Basic source file.