The Daily Newbie - Using the Dir() Function

Submitted on: 1/17/2015 8:52:00 AM
By: Matthew Roberts (from psc cd)  
Level: Beginner
User Rating: By 5 Users
Compatibility: VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, VBA MS Access
Views: 489
     Explains the basics of using the Dir() command to get file and folder information.

The Daily Newbie

To Start Things Off Right
Today's Topic: Using the Dir Command
Name Derived From "Directory"
Used For: Getting Information about a particular folder or file.
VB Help File Description 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
Plain English Description

Depending on the paramters set, returns:

  • The Name of a file in a folder that matches a patten (*.txt)
  • The name of a sub folder within a folder.
  • The name of a hard drive.

To get a file name from a directory:   strFile = Dir ("c:\MyFolder\*.txt")

To get a read-only file name from a directory:   strFile = Dir ("c:\MyFolder\*.txt", vbReadOnly)

To get a sub-directory from a directory:   strFile = Dir ("c:\MyFolder\*", vbDirectory)

To get the label of a drive:   strFile = Dir ("d:", vbVolume)

  • Path - The root directory to search from.
  • Attribute - One of the following VB attribute values: vbNormal (default), vbReadOnly, vbHidden, VbSystem, vbVolume, vbDirectory


Copy & Paste Code:

Today's copy and paste code lists all of the files in a directory to the debug window. For details on usage of the Dir ()command in this example, see the Notes below.

Dim strPathAndPattern As String
    Dim strFileName As String
    strPathAndPattern = InputBox("Enter a path and search pattern (ex: c:\windows\*.exe):")
    strFileName = Dir(strPathAndPattern)
    Debug.Print strFileName
    While strFileName > ""
     strFileName = Dir
         Debug.Print strFileName Wend
  • IMPORTANT: To find multiple files, you must do a "two step" proccess. In the example above, notice that the first time Dir() is called (before the While...Wend loop), the parameter strPathAndPatten is used. After that, the call is simply Dir. (strFileName = Dir). This is sort of confusing if you don't know what is going on. When I first used the Dir command, I kept getting the same file name over and over. This is because using Dir() with a path parameter will always return the FIRST match. To get subsequent matches, you simply call Dir(). It remembers the last pattern and passes the NEXT match. Really screwy.
  • Dir can be used to see if a file already exists:

             If Dir ("c:\MyFolder\log.txt") > "" Then
                    MsgBox "File Already Exists!"
             End If

    This is because Dir() will return an empty string ("") if a match is not found, but will return the file name if a match is found.
  • Dir can also be used to see if a folder exists, but this is a little different. Because of the way Windows handles folder names, there is always a folder named "." and another named ".." . As odd as that seems, these names represent the current folder and the parent folder. So to find out if a certain folder exists, you can do this:

    If Dir ("c:\windows\system", vbDirectory) > ".." Then
         MsgBox "Folder Already Exists!"
    End If

  • For a downloadable project using the Dir command, Click Here


Other 25 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

10/22/2015 10:34:27 AMDavid

404 error on download link in text

(If this comment was disrespectful, please report it.)


Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.