Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake the directories in a complete path
Keywordsdirectory, path
CategoriesFiles and Directories
 
Use Split to break the path apart at the \ symbols. Loop through the pieces of the path, creating the subdirectories that don't already exist from the directory root down.
 
' Create the complete directory path.
Private Sub MakePath(ByVal path As String)
Dim directories As Variant
Dim i As Integer
Dim new_dir As String
Dim dir_path As String

    ' Break the path into directories.
    directories = Split(path, "\")

    ' Build the subdirectories.
    For i = LBound(directories) To UBound(directories)
        ' Get the next directory in the path.
        new_dir = directories(i)

        ' Make sure the entry is non-empty.
        If Len(new_dir) > 0 Then
            ' Add the new directory to the path.
            dir_path = dir_path & new_dir & "\"

            ' Make sure we don't just have a drive
            ' specification.
            If Right$(new_dir, 1) <> ":" Then
                ' See if the directory already exists.
                If Dir$(dir_path, vbDirectory) = "" Then
                    ' The directory doesn't exist.
                    ' Make it.
                    MkDir dir_path
                End If
            End If
        End If
    Next i
End Sub
 
Bill Hileman found a discussion group posting by ObiWan with this advice:

Add this declaration to a module:
 
Declare Function MakeTree Lib "imagehlp.dll" Alias _
    "MakeSureDirectoryPathExists" (ByVal lpszPath As _
    String) As Long
 
This routine ensures that the entire directory tree exists in one function call.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated