Creating Folders Using VB and Recursion

  • Using recursion is an un-necessary overhead. You could achieve the same result without putting this much strain on your stack by iterating through an array:

    Function CreateFolder(Path As String) As Scripting.Folder

    Dim arrTemp() As String

    Dim i As Long

    Dim strPath As String

    Dim oFS As Scripting.FileSystemObject

    Dim oFld As Scripting.Folder

    On Error Goto ErrHandler

    Set oFS = New Scripting.FileSystemObject

    strPath = oFS.GetAbsolutePathName(Path)

    arrTemp = Split(strPath,"\")

    If oFS.DriveExists(arrTemp(0)) Then

    strPath = arrTemp(0) & "\"

    For i=1 To UBound(arrTemp)

    strPath = strPath & arrTemp(i) & "\"

    If Not oFS.FolderExists(strPath) Then

    oFS.CreateFolder(strPath)

    End If

    Next

    Set CreateFolder = oFS.GetFolder(strPath)

    Else

    'Error - Drive doesn't exist

    Set CreateFolder = Nothing

    End If

    Exit Function

    ErrHandler:

    Set CreateFolder = Nothing

    End Function

    This version also handles relative paths, such as "..\folder1", etc.

  • Hey Richard,

    Thanks for reading the article and taking time to comment. I can't really argue that using recursion probably takes more resources, but I think you'd have to a REALLY long path name before you would come close to exhausing stack space.

    I like recursion because it's clean. And for this article, it was a chance to solve a problem and get readers thinking about uses for it - possibly I could have found a better one!

    One thing I like about your code is being able to use the "..\folder" relative addressing. That can really come in handy.

    Andy

  • This function really should be iterative. There is no benefit in it being recursive. First of all, there is the overhead of having to maintain a stack. Even though you don't have to do it, it's still being done. If you had to write the stack your self, or make explicit calls to it, you wouldn't use it for this algorithim, because you don't need it. Visual Baisc isn't scheme, it won't optimize tail recursive functions for you. There's also the fact that you have to create a file system object instance for every directory in the path. Why instantiate objects you don't need. VB's memory management is really horrible, mainly because you can't control it. From a theoretical computer science standpoint, as well as from a pratical, real world point of view, it just doesn't make since to implement that function recursively

  • Hello Wizzy,

    As I posted earlier in reply to Richard, I cant argue that you can solve this other ways, probably more efficient as well. It was posted as one way to solve a problem, with the goal being to encourage readers to think about recursion.

    Overall I agree with you about not instantiating objects you dont need. I didnt try to optimize it (one way would be to make the fso public I think) since given what it does, the overall execution time, resources, etc, are minimal. That doesn't make it right, just good enough!

    Thanks for reading the article and taking time to offer your comments.

    Andy

  • Hello, I've maked this code for creating folders in VB;

    Public Sub CrearPath(strPath As String)

        Dim Carpetas() As String

       

        Carpetas() = Split(strPath, "\")

       

        strP$ = Carpetas(0)       '<---------- drive:  ex:   "N:"

        For a& = 1 To UBound(Carpetas())

           

            strP$ = strP$ & "\" & Carpetas(a&amp

           

            If FSO.FolderExists(strP$) = False Then

                On Error Resume Next

                FSO.CreateFolder strP$

            End If

        Next a&

    End Sub

    This could be called from any form (if the Sub is declared in a Module), and works...

    Ex:

    CrearPath "C:\Folder1\Folder2\Folder3\Folder4"

    NOTE:

    An "\" in the end of the string, it doesn't matter... don't worry..

    (Sorry for my English)

    Brian Sternari - HeAD.Zip

     

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply