Library code snippets

Creating Folders using recursion

Assume that you're trying to create the following folder:
C:\Folder1\Folder2\Folder3\Folder4
You could easily create it if the folder C:\Folder1\Folder2\Folder3 existed by simply calling the MkDir method:
MkDir "C:\folder1\folder2\folder3\folder4"

However, VB flags a run-time error if the C:\folder1\folder2\folder3 does not exists. The following function makes creating subfolders more easier by checking all the parent folders and creating any of them if they did not exist using recursion.

To use this function, simply pass the FULL PATH of the folder you wish to create. Make sure that destDir always end with "\" (without quotes). The function returns TRUE if operation was successful. Otherwise, it returns FALSE.


Public Function CreateFolder(destDir As String) As Boolean
   
   Dim i As Long
   Dim prevDir As String
   
   On Error Resume Next
   
   For i = Len(destDir) To 1 Step -1
       If Mid(destDir, i, 1) = "\" Then
           prevDir = Left(destDir, i - 1)
           Exit For
       End If
   Next i
   
   If prevDir = "" Then CreateFolder = False: Exit Function
   If Not Len(Dir(prevDir & "\", vbDirectory)) > 0 Then
       If Not CreateFolder(prevDir) Then CreateFolder = False: Exit Function
   End If
   
   On Error GoTo errDirMake
   MkDir destDir
   CreateFolder = True
   Exit Function
   
errDirMake:
   CreateFolder = False

End Function

Example:
If CreateFolder("C:\Folder1\Folder2\Folder3\") Then
  MsgBox "Folder Creation successful!"
Else
  MsgBox "Folder Creation failed!"
End If

Comments

  1. 04 Jan 2010 at 14:22

    You probably already figured this out but you would could use Microsoft.VisualBasic.Left

    Imports System.IO Imports Microsoft.VisualBasic Imports VB = Microsoft.VisualBasic

    Public Function CreateFolder(ByVal destDir As String) As Boolean

        Dim i As Long
        Dim prevDir As String
    
        On Error Resume Next
    
        For i = Len(destDir) To 1 Step -1
            If Mid(destDir, i, 1) = "\" Then
                prevDir = Microsoft.VisualBasic.Left(destDir, i - 1)
                Exit For
            End If
        Next i
    
        If prevDir = "" Then CreateFolder = False : Exit Function
        If Not Len(Dir(prevDir & "\", vbDirectory)) > 0 Then
            If Not CreateFolder(prevDir) Then CreateFolder = False : Exit Function
        End If
    
        On Error GoTo errDirMake
        MkDir(destDir)
        CreateFolder = True
        Exit Function
    

    errDirMake: CreateFolder = False

    End Function
    
  2. 16 Nov 2005 at 15:49


    i'm using vb.net 2005 and get an error as follows


    Code:
    Public Property Left() As Integer' has no parameters and its return type cannot be indexed.

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Creating Folders using recursion.

Leave a comment

Sign in or Join us (it's free).

 Lio_889

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!