How To Use The DIR Function
How To Use The DIR Function
This Excel tutorial explains how to use the Excel DIR function with syntax and
examples.
Description
The Microsoft Excel DIR function returns the first filename that matches
the pathname and attributes specified. To retrieve additional filenames that
match pathname and attributes, call DIR again with no arguments.
The DIR function is a built-in function in Excel that is categorized as a File/Directory
Function. It can be used as a VBA function (VBA) in Excel. As a VBA function, you can
use this function in macro code that is entered through the Microsoft Visual Basic Editor.
Syntax
The syntax for the DIR function in Microsoft Excel is:
Parameters or Arguments
path
Optional. It the path to a file, folder, or directory. If the path is not found, the DIR
function will return a zero-length string.
attributes
Optional. It is the sum of the file attributes. File attributes can be one or a
combination of the following values:
vbReadOnly 1 Read-only
vbHidden 2 Hidden
Note
vbNormal - files with no specific attributes
vbReadOnly - read only files, in addition to files with no attributes
vbHidden - hidden files, in addition to files with no attributes
vbSystem - system files, in addition to files with no attributes (not available on the
Macintosh)
vbVolume - volume label
vbDirectory - directories or folders, in addition to files with no attributes
vbAlias - specified filename is an alias (only available on the Macintosh)
If omitted, the [Attributes] argument is set to vbNormal.
You can use wildcard characters to specify multiple files. The patterns that you can
choose from are:
Wildcar
Explanation
d
* Allows you to match any string of any length (including zero length)
Dir ("")
Returns
The DIR function returns a string value.
Dir("C:\instructions.doc")
Result: "instructions.doc"
Dir("C:\in*.doc")
Result: "instructions.doc"
Dir("C:\instruction?.doc")
Result: "instructions.doc"
For example:
LResult = Dir("C:\instructions.doc")
In this example, the variable called LResult would now contain the filename of
the instructions.doc file.
Question: How can I use the DIR function to test whether a file exists?
Answer: This can be done with a formula that utilizes a combination of the DIR
function, IF function, and LEN function.
For example:
If Len(Dir("c:\Instructions.doc")) = 0 Then
Msgbox "This file does NOT exist."
Else
Msgbox "This file does exist."
End If
Question: I'm not sure if a particular directory exists already. If it doesn't exist, I'd like to
create it using VBA code. How can I do this?
Answer: You can test to see if a directory exists using the VBA code below:
Dim fName As String
fName = Dir( "C:\Documents\data.txt" )
' The variable "fName" now contains the string "data.txt" (if the file
' is found) or an empty text string otherwise.
If fName = "" Then
Else
End If
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.
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:\".
' Store the current file or directory name in the string fList.
fList = fList & vbNewLine & fName
Loop
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.
Note: this example uses the wildcard *, which only works on computers running Microsoft
Windows.
Dim fList As String
Dim fName As String
Loop
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.