Dir Function

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 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, that specifies file attributes. If omitted, returns all files that match pathname.

Значения

The attributes argument settings are:

Константа Значение Описание
cdbNormal 0 Normal
cdbHidden 2 Hidden
cdbSystem 4 System (Microsoft Windows only)
cdbVolume 8

Volume label; if specified, any other attributes are ignored (Microsoft Windows only)

cdbDirectory

16 Directory or folder
cdbAlias 64

Specified file name is an Alias (Macintosh only)

Note. These constants are specified by the application, that is they can be used anywhere in your code in place of the actual values.

Remarks

When Dir is called first time, a path should be specified - otherwise an error will occur. If file attributes are specified, the pathname argument is required.
The Dir function returns the first file name that matches pathname. To get other file names, matching pathname, call Dir again without arguments. When there are no more matching file names, an empty string ("") is returned. When calling the function after an empty string has been returned, pathname must be specified - otherwise an error occurs. You can modify pathname at any time. Dir can't be called recursively. Calling Dir with the cdbDirectory attribute doesn't return subfolders subsequently.

Note. As file names are returned in random order, you may store them in an array and then sort.

Example

This example uses the Dir function to look for certain files and directories.

Dim MyFile, MyPath, MyName
' In Microsoft Windows:
' Returns"WIN.INI" (if exists).
MyFile = Dir("C:\WINDOWS\WIN.INI")

' Returns a file name with specified extension. If more than one *.INI file exist 
' returns the first file found. 
MyFile = Dir("C:\WINDOWS\*.INI")

' Call Dir again with no arguments to get the next *.INI file 
' located in the same directory.
MyFile = Dir

' Returns the first found *.TXT file with hidden attribute.
MyFile = Dir("*.TXT", cdbHidden)

' Returns the list of directories on drive C:.
MyPath = "c:\"                     ' Specify path.
MyName = Dir(MyPath, cdbDirectory) ' 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 cdbDirectory) = cdbDirectory Then
            Trace MyName    ' Display entry only if it represents a directory
End If End If MyName = Dir ' Get next entry. Loop

 

See Also

Инструкция ChDir, Функция CurDir