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

Elements

The Dir function syntax has these parts:

Part

Description

pathname

String expression that specifies a file name — 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.


The attributes argument has these constants and values:

Constant

Value

File Attribute

vbNormal

0

Normal.

vbHidden

2

Hidden.

vbSystem

4

System — not available on the Macintosh.

vbVolume

8

Volume label; if specified, all attributes are ignored — not available on the Macintosh.

vbDirectory

16

Directory or folder.


Note

These constants are specified by Visual Basic. 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. However, 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 does not support 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:


Dir("", MacID("TEXT"))

If you use the MacID function with Dir in Microsoft Windows, an error occurs.

Any attribute 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 recursively call the Dir function.

Tip

Because file names are retrieved in no particular order, you may want to store returned file names in an array and then sort the array.

See Also

ChDir Statement, CurDir Function, MacID Function.