Note The function, method, object, or property described in this topic is disabled if the Microsoft Jet Expression Service is running in sandbox mode, which prevents the evaluation of potentially unsafe expressions. For more information on sandbox mode, search for "sandbox mode" in Help.
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 arguments:
||Optional. String expression (string expression: An expression that evaluates to a sequence of contiguous characters. Elements of the expression can be: functions that return a string or a string Variant (VarType 8); a string literal, constant, variable, or Variant.) that specifies a file name — may include directory or folder, and drive. A zero-length string ("") is returned if pathname is not found.
||Optional. Constant (constant: A value that is not calculated and, therefore, does not change. For example, the number 210, and the text "Quarterly Earnings" are constants. An expression, or a value resulting from an expression, is not a constant.) or numeric expression (numeric expression: Any expression that evaluates to a number. The expression can be any combination of variables, constants, functions, and operators.), whose sum specifies file attributes. If omitted, returns files that match pathname but have no attributes.
The attributes argument (argument: A value that provides information to an action, an event, a method, a property, a function, or a procedure.) settings are:
||(Default) Specifies files with no attributes.
||Specifies read-only files in addition to files with no attributes.
||Specifies hidden files in addition to files with no attributes.
||Specifies system files in addition to files with no attributes. Not available on the Macintosh.
||Specifies volume label; if any other attributed is specified, vbVolume is ignored. Not available on the Macintosh.
||Specifies directories or folders in addition to files with no attributes.
||Specified file name is an alias. Available only on the Macintosh.
Note These constants are specified by Visual Basic for Applications (VBA) and can be used anywhere in your code in place of the actual values.
In Microsoft Windows, Dir supports the use of multiple character (*) and single character (?) wildcards to specify multiple files. On the Macintosh, these characters are treated as valid file name characters and can't be used as wildcards to specify multiple files.
Since the Macintosh doesn't support the wildcards, use the file type to identify groups of files. You can use the MacID function to specify file type instead of using the file names. For example, the following statement returns the name of the first TEXT file in the current folder:
To iterate over all files in a folder, specify an empty string:
If you use the MacID function with Dir in Windows, an error occurs.
Any attributes value greater than 256 is considered a MacID value.
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 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 can't call the Dir function recursively. Calling Dir with the vbDirectory attribute does not continually return subdirectories.
Tip Because file names are retrieved in no particular order, you may want to store returned file names in an array (array: A variable that contains a finite number of elements that have a common name and data type. Each element of an array is identified by a unique index number. Changes made to one element of an array don't affect the other elements.), and then sort the array.
Note Examples that follow demonstrate the use of this function in a Visual Basic for Applications (VBA) module. For more information about working with VBA, select Developer Reference in the drop-down list next to Search and enter one or more terms in the search box.
This example uses the Dir function to check if certain files and directories exist. On the Macintosh, “HD:” is the default drive name and portions of the pathname are separated by colons instead of backslashes. Also, the Windows wildcard characters are treated as valid file-name characters on the Macintosh. However, you can use the MacID function to specify file groups.
Dim MyFile, MyPath, MyName
' Returns "WIN.INI" (on Microsoft Windows) if it exists.
MyFile = "C:\WINDOWS\WIN.INI"
' Returns filename with specified extension. If more than one *.ini
' file exists, the first file found is returned.
MyFile = "C:\WINDOWS\*.INI"
' Call Dir again without arguments to return the next *.INI file in the
' same directory.
MyFile = Dir
' Return first *.TXT file with a set hidden attribute.
MyFile = "*.TXT" vbHidden
' Display the names in C:\ that represent directories.
MyPath = "c:\" ' Set the path.
MyName = MyPath vbDirectory ' Retrieve the first entry.
Do 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.
MyName = ' Get next entry.