View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Help with Recursive Call?

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




mark wrote:

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


--

Dave Peterson