Tip 73: Creating Nested Directories

May 1, 1995

Abstract

You can use the Visual Basic® MkDir statement to create a new directory on any floppy or hard disk. This article explains how you can use this statement to create nested subdirectories, even if part of the path already exists.

The MkDir Statement

When developing an application in Visual Basic®, you may need to allow the user to store a data file in a specific directory on disk. The directory can be created with Visual Basic's MkDir statement. The syntax for this statement is:

MkDir "C:\<JUNK>"

where JUNK is the name of the directory you want to create and C:\ is the drive you want to create it on. If you want to create a nested subdirectory, for example C:\JUNK\TEST, you must first create the JUNK directory and then create the TEST directory. However, if the JUNK directory already exists, the MkDir statement will interrupt your application with a "Path/File Access Error" displayed in a message box.

To prevent this error message from appearing when your application is creating directories, you can use the On Error and Resume Next statements. These two statements allow you to trap this access error and force your Visual Basic program to continue executing as if the error had not actually occurred.

The example program below creates a directory that is three levels deep, called "\JUNK\TEST\TEST". The first time the MkDir statement is executed, it creates the JUNK directory. The second and third times MkDir is executed, however, it produces Error 76. The Resume Next statement tells our program to ignore this error each time it is encountered until we have successfully created the entire directory structure.

Example Program

This program shows how to create nested directories even if one of the directories in the path already exists.

  1. Create a new project in Visual Basic. Form1 is created by default.

  2. Add a Command Button control to Form1. Command1 is created by default. Set its Caption property to "Create Directory".

  3. Add the following code to the Click event for Command1:
    Sub Command1_Click()
        CreateNewDirectory "C:\junk3\test\test"
        MsgBox "Directory was created", 16, "Dir Demo"
    End Sub
    
  4. Create a new function called CreateNewDirectory. Add the following code to this function:
    Sub CreateNewDirectory(DirName As String)
        Dim NewLen As Integer
        Dim DirLen As Integer
        Dim MaxLen As Integer
        
        NewLen = 4
        MaxLen = Len(DirName)           
        
        If Right$(DirName, 1) <> "\" Then
            DirName = DirName + "\"
            MaxLen = MaxLen + 1
        End If
        On Error GoTo DirError
        
    MakeNext:
        DirLen = InStr(NewLen, DirName, "\")
        MkDir Left$(DirName, DirLen - 1)
        NewLen = DirLen + 1
        If NewLen >= MaxLen Then
            Exit Sub
        End If
        GoTo MakeNext
    DirError:
        Resume Next
    End Sub
    

Run the program by pressing the F5 function key. When you click the "Create Directory" command button, the program creates a directory called "JUNK\TEST\TEST" on drive C. Change the path in the Click event for Command1 to "C:\JUNK\TEST\TEST2" and run the program a second time. It will create a second subdirectory called TEST2 under C:\JUNK\TEST. Notice that Error 76 is ignored by the program. This error is generated by MkDir when it attempts to create a directory (in this case, C:\JUNK\TEST) that already exists.