Help with Recursive Call?

G

Guest

Hi.

I am familiar with what a recursive call is, but am still working on getting
good at them.

I'm trying to use the filesystemobject to create a directory, which may be
several levels down from the existing directories.

for instance, if I want to create the directory c:\Joe2\Level1\Level2\Level3
, but the current directory structure only has c:\Joe2 , I would like it to
receive c:\Joe2\Level1\Level2\Level3 as the directory to create, and check
for / create directories recursively, until it finds that the first directory
in that string that exists is c:\Joe2

Then, create the three subdirectories recursively. The code I have works
its way down and created the c:\Joe2\Level1 directory, but does not work it's
way back up creating the other directories. And besides, I suspect my code
is more complicated than it needs to be.

Could someone help me with the code below? Thanks.

******************

Sub testdir()

sbCreateFolder ("c:\Joe2\Level1\Level2\Level3")


End Sub

Sub sbCreateFolder(ByVal stFolder As String)

'dimension variables
On Error Resume Next
Dim fs As Object


'assign variables

Set fs = CreateObject("Scripting.FileSystemObject")


'check if dest directory exists

fs.createfolder (stFolder)

If Not fs.folderexists(stFolder) Then

While Right(stFolder, 1) <> "\"

stFolder = Left(stFolder, Len(stFolder) - 1)

Wend

stFolder = Left(stFolder, Len(stFolder) - 1)

sbCreateFolder (stFolder)

Else
Exit Sub

End If

End Sub
 
G

Guest

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
 
T

tony h

Looking at your code I would say stay away from recursive calls until
you have more experience. They are a fantastic way to cripple a
machine.

Just a few points.
1. you have an On Error resume Next but nowhere do you actually check
for error values and handle them.
2. You have no end to the recursiveness it should end when you run out
of directories to create.
3. Just glancing at the manipulation of stFolder it seems like a mess
(I reserve the right to review this statement in the cold light of
day). Do you use debugging : stepping through, testing values of
variables etc

4. if you are determined to write it recursively then get it working
non-recursively first.
 
G

Guest

I agree, I need a lot of practice in learning to write efficient, eloquent
recursive calls.
1. you have an On Error resume Next but nowhere do you actually check
for error values and handle them.

Not really true. The entire poitn of that On Error was so that the code
wouldn't bomb when it tried to create a directory that it wasn't yet ready
for... too far down the tree.
2. You have no end to the recursiveness it should end when you run out
of directories to create.

Again, not correct. It does run out when the directories are created. If
it didn't, I'd now be getting infinite calls, whereas it is now creating the
proper number of directories, and exiting... (albeit not in the best manner,
were I more experienced with recursive calls, which I was the first to admit
that I'm not).
3. Just glancing at the manipulation of stFolder it seems like a mess
(I reserve the right to review this statement in the cold light of
day). Do you use debugging : stepping through, testing values of
variables etc


Thank you for your criticism.

However, 'constructive' criticism, which usually includes specific
suggestions, not only statement proven to be incorrect, is usually more
useful.

4. if you are determined to write it recursively then get it working
non-recursively first.

I may also do that.
 
G

Guest

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
 
G

Guest

Thank you!

I was looking for that text compare, but didn't find it today. Thank you
for pointing it out. It definitely cleans up the part of the code that finds
the current directory's parent directory, A LOT.

Thank you for your constructive comments! I do appreciate it.
Mark
 
D

Dave Peterson

Jim Rech posted this:

Try this code:

Declare Function MakePath Lib "imagehlp.dll" Alias _
"MakeSureDirectoryPathExists" (ByVal lpPath As String) As Long


Sub Test()
MakeDir "c:\aaa\bbb"
End Sub


Sub MakeDir(DirPath As String)
If Right(DirPath, 1) <> "\" Then DirPath = DirPath & "\"
MakePath DirPath
End Sub
 
G

Guest

Thanks, Dave.

Do you know of a reference that would give information about functions
contained in .dll files?

I see what you're doing there, and appreciate the guidance.

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top