Dir Function

Description

Returns 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 Dir function syntax has these parts:

Part Description
pathname String expression that specifies a filename — may include directory or folder, and drive. Null is returned if pathname is not found.
attributes Constant or numeric expression, the sum of which specifies file attributes. If omitted, all normal files are returned that match pathname.


Settings

The attributes argument settings are:


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.


Note These constants are specified by Visual Basic for applications. As a result, the names can be used anywhere in your code in place of the actual values.

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.

Dir returns the first filename that matches pathname. To get any additional filenames that match pathname, call Dir again with no arguments. When no more filenames 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 filenames that match the current pathname. However, you can’t recursively call the Dir function.

Tip Because filenames are retrieved in no particular order, you may want to store returned filenames in an array and then sort the array. Also, calling Dir with the vbDirectory attribute does not continually return subdirectories.

See Also

ChDir Statement, CurDir Function.

Example

This example uses the Dir function to check if certain files and directories exist.


' Returns "WIN.INI" if it exists.= Dir("C:\WINDOWS\WIN.INI")    
' Returns filename with specified extension. If more than one *.INI
' file exists, the first file found is returned.= Dir("C:\WINDOWS\*.INI")
' Call Dir again without arguments to return the next *.INI file in the 
' same directory.= Dir
' Return first *.TXT file with a set hidden attribute.= Dir("*.TXT", vbHidden)

' Display the names in C:\ that represent directories.= "c:\"    ' Set the path.= Dir(MyPath, vbDirectory)    ' Retrieve the first entry.While MyName <> ""    ' Start the loop.
    ' Ignore the current directory and the encompassing directory.
    If MyName <> "." And MyName <> ".." Then
        ' Use bitwise comparison to make sure MyName is a directory.
        If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
            Debug.Print MyName    ' Display entry only if it
        End If    ' it represents a directory.
    End If
    MyName = Dir    ' Get next entry.