Search code examples
vb6

Equivalent of Directory.CreateDirectory() in VB6


Trying to create several layers of folders at once C:\pie\applepie\recipies\ without using several different commands, is there an easy way similar to Directory.CreateDirectory()


Solution

  • Here's some code I used in one of my projects. It requires a reference be added to the project for the file system object.

    First, click Project -> References, scroll down to "Microsoft Scripting Runtime" and select it. Then you can use this function:

    Public Sub MakePath(ByVal Folder As String)
    
        Dim arTemp() As String
        Dim i As Long
        Dim FSO As Scripting.FileSystemObject
        Dim cFolder As String
    
        Set FSO = New Scripting.FileSystemObject
    
        arTemp = Split(Folder, "\")
        For i = LBound(arTemp) To UBound(arTemp)
            cFolder = cFolder & arTemp(i) & "\"
            If Not FSO.FolderExists(cFolder) Then
                Call FSO.CreateFolder(cFolder)
            End If
        Next
    
    End Sub