Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default Help with Recursive Call?

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default kept working, got it working, but...

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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Help with Recursive Call?


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.


--
tony h
------------------------------------------------------------------------
tony h's Profile: http://www.excelforum.com/member.php...o&userid=21074
View this thread: http://www.excelforum.com/showthread...hreadid=545329

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default Help with Recursive Call?

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.


  #5   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default 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



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default kept working, got it working, but...

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


"JMB" wrote:

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

  #7   Report Post  
Posted to microsoft.public.excel.programming
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
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default Thanks... references?

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
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Thanks... references?

There are lots of sites/books that describe these API functions.

http://vbnet.mvps.org/
(Randy Birch)

http://www.developer.com/net/vb/

Are a couple.



mark wrote:

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


--

Dave Peterson
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default Thanks... references?

thanks forf the links... will check them out.

"Dave Peterson" wrote:

There are lots of sites/books that describe these API functions.

http://vbnet.mvps.org/
(Randy Birch)

http://www.developer.com/net/vb/

Are a couple.



mark wrote:

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


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursive Functions...maybe busboy10 Excel Worksheet Functions 0 March 28th 11 09:46 PM
Recursive Functio help BigBobbo Excel Worksheet Functions 1 May 10th 06 07:23 PM
Why doesn't this recursive function return the right value? RB Smissaert Excel Programming 10 September 3rd 05 01:11 AM
recursive sums Joe Excel Worksheet Functions 6 July 17th 05 09:45 AM
Recursive Subs? ExcelMonkey[_74_] Excel Programming 5 February 5th 04 02:54 AM


All times are GMT +1. The time now is 09:54 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"