The VBA Dir function returns the first file or directory name that matches a specified pattern and attributes.
If the Dir function is then called a second time, with no arguments, it remembers the arguments from the previous call and returns the second matching file or directory.
If there are no matching files, the Dir function returns an empty string.
The syntax of the VBA Dir function is:
Where the function arguments are:
Path | - | The partial or full path and/or filename that you want to search for. | |||||||||||||||||||||
[Attributes] | - |
An optional vbFileAttribute enumeration value (or combination of values), specifying the attributes of any matching files. This can be one or more of the following values:
If omitted, the [Attributes] argument is set to vbNormal. Note that if more than one of the above values is used, these must be separated by the + symbol (e.g. vbHidden + vbSystem). |
If you are working on Microsoft Windows, you can use the following wildcards within the Path argument:
? - matches any single character
* - matches any sequence of characters
' Find the file "C:\Documents\data.txt"
Dim fName As String
fName = Dir( "C:\Documents\data.txt" )
' The variable "fName" now contains the string "data.txt" (if the file
If fName = "" Then
' is found) or an empty text string otherwise.
' File not found!
' File found. Open file to extract data.
|
The above call to the VBA Dir function returns the file name "data.txt" if this file is found in the directory "C:\Documents" or returns an empty string if the file is not found.
The following VBA code uses the Dir function to list all the files and directories within "C:\".
' List the files and directories within "C:\".
Dim fList As String
Dim fName As String
fName = Dir( "C:\", vbDirectory )
' The variable fName now contains the name of the first file or directory within "C:\".
Do While fName <> ""
' Display the list of directories in a message box.
' Store the current file or directory name in the string fList.
' Get the next file or directory within "C:\".fList = fList & vbNewLine & fName fName = Dir() ' The variable fName now contains the name of the next file or directory within "C:\". MsgBox( "File List:" & fList ) |
The above VBA code causes the following message box to be displayed, listing the names of all the files and directories in "C:\".
Note that the initial call to the Dir function provides the Path and [Attributes] arguments, and these values are remembered in the subsequent calls to the Dir function.
The following VBA code uses the Dir function to list all .csv files in the directory C:\DataFiles\.
Note: this example uses the wildcard *, which only works on computers running Microsoft Windows.
' Find all .csv files in directory "C:\DataFiles\"
Dim fList As String
Dim fName As String
fName = Dir( "C:\DataFiles\*.csv" )
' The variable fName now contains the name of the first .csv file within "C:\DataFiles\".
Do While fName <> ""
' Display the list of .csv files in a message box.
' Store the current file in the string fList.
' Get the next .csv file within "C:\DataFiles\".fList = fList & vbNewLine & fName fName = Dir() ' The variable fName now contains the name of the next .csv file within "C:\DataFiles\". MsgBox( "List of .csv Files:" & fList ) |
The above VBA code causes the following message box to be displayed, listing the names of all the .csv files in the directory "C:\DataFiles\".
Note that, as in the previous example, the Path argument is provided to the initial call to the Dir function, and this value is remembered in the subsequent calls to the function.