Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Recursive Functions...maybe | Excel Worksheet Functions | |||
Recursive Functio help | Excel Worksheet Functions | |||
Why doesn't this recursive function return the right value? | Excel Programming | |||
recursive sums | Excel Worksheet Functions | |||
Recursive Subs? | Excel Programming |