kept working, got it working, but...
Recursion is usually less efficient than an iterative loop due the overhead
of extra procedure calls. Also, if it gets out of hand, it can consume all
of the computers memory or cause stack overflow errors.
The first function uses as few lines of code as I could (just as an
example). The second tests to see if the folder exists before making another
procedure call (which would be more efficient)
Function jCreateFolder(ByVal stFolder As String) As Boolean
On Error Resume Next
jCreateFolder (Left(stFolder, _
InStrRev(stFolder, "\", -1, vbTextCompare) - 1))
MkDir (stFolder)
End Function
Function jCreateFolder(ByVal stFolder As String) As Boolean
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
If Not FSO.folderexists(stFolder) Then _
jCreateFolder (Left(stFolder, _
InStrRev(stFolder, "\", -1, vbTextCompare) - 1))
MkDir (stFolder)
End Function
"mark" wrote:
I kept working, and got it working, but I still suspect it's more complicated
than it needs to be.
Here's what I have:
******************
Sub testdir()
Dim blFolder As Boolean
blFolder = fnCreateFolder("c:\Joe2\Level1\Level2\Level3")
End Sub
Function fnCreateFolder(ByVal stFolder As String) As Boolean
'dimension variables
On Error Resume Next
Dim fs As Object
Dim stFolderTest As String
'assign variables
Set fs = CreateObject("Scripting.FileSystemObject")
stFolderTest = stFolder
'check if dest directory exists
While Not fs.folderexists(stFolder)
fs.createfolder (stFolder)
If fs.folderexists(stFolder) Then
fnCreateFolder = True
Else
While Right(stFolderTest, 1) < "\"
stFolderTest = Left(stFolderTest, Len(stFolderTest) - 1)
Wend
stFolderTest = Left(stFolderTest, Len(stFolderTest) - 1)
fnCreateFolder (stFolderTest)
End If
Wend
End Function
|