[Gambas-user] Try Catch fail when using mkdir....
Stephen Bungay
sbungay at ...981...
Fri Jul 1 05:58:40 CEST 2011
Hi Fabien & Tobias;
Thanks for taking the time to reply and putting those SUBs together.
Another way to do this is to simply execute a "mkdir -p " using the
command shell, but now that the problem exists I want to figure out why
the recursive routine is not behaving as expected.
Fabien, I thought the FINALLY did precede the CATCH within the
function... did it not? I reprint the SUB here with two additional
comments, a correction to a typo (thank you Tobias), and a conditional
surrounding the Mkdir in the Finally section just in case it tries to
make a directory that already exists and ends up going into an endless
loop.
Private Sub CreateNewOutputFolder(psFolderSpecification As String)
Dim sFolderSpec As String
sFolderSpec = psFolderSpecification
Mkdir sFolderSpec
Finally
If Not exists(sFolderSpec) Then
Mkdir sFolderSpec
End If
Catch
sFolderSpec = Mid$(psFolderSpecification, 1, RInStr(psFolderSpecification, "/") - 1)
CreateNewOutputFolder(sFolderSpec)
End
On 06/30/2011 10:09 AM, Fabien Bodard wrote:
> private sub CreateDirTree(sDir as string)
>
> dim s as string
> dim stmpDir as string = "/"
>
> if sdir begins "/" then sdir = right(sdir,-1)
>
> For each s in split(sDir, "/")
> stmpDir&= s
> if exist(stmpdir) then continue
> mkdir stmpdir
> next
>
> catch
> Print "The directory "& stmpdir& "can't be created"
>
> end
>
>
> 2011/6/30 Fabien Bodard<gambas.fr at ...626...>:
>> The FINALLY part is not mandatory. If there is a catch part in the
>> function, the FINALLY part must precede it.
>>
>> http://gambasdoc.org/help/lang/finally
>>
>> The second call will be in the catch part not in finally.
>>
>> 2011/6/30 Stephen Bungay<sbungay at ...981...>:
>>> Hi folks!
>>>
>>> Gambas 2.99
>>> Fedora 14
>>>
>>> Using mkdir with "catch" and "finally" to create a recursive SUB to
>>> build a directory structure.
>>> The harness consists of FormMain with one big-friendly button on it,
>>> pretty simple. Here is all of the code;
>>>
>>> ' Gambas class file
>>>
>>> Public Sub _new()
>>>
>>> End
>>>
>>> Public Sub Form_Open()
>>>
>>> End
>>>
>>> Private Sub CreateNewOutputFolder(psFolderSpecification As String)
>>> Dim sFolderSpec As String
>>>
>>> sFolderSpec = psFolderSpecification
>>>
>>> Mkdir sFolderSpec
>>>
>>> Finally
>>> Mkdir sFolderSpec
>>>
>>> Catch
>>> sFolderSpec = Mid$(psFolderSpecification, 1,
>>> RInStr(psFolderSpecification, ".") - 1)
>>> CreateNewOutputFolder(sFolderSpec)
>>> End
>>>
>>> Public Sub Button1_Click()
>>>
>>> CreateNewOutputFolder("/home/user/Rumple/Stilskin/Was/Here")
>>>
>>> End
>>>
>>>
>>> What I THINK should happen is the initial mkdir should fail, the code
>>> in "catch" should execute and copy the passed in parameter from position
>>> 1 to the charcter just prior to the last "/" and then call itself
>>> passing in the new result as the parameter. When/if that call fails (and
>>> it should as this folder specification doesn't exist in my home dir) it
>>> again recurses. This should go on until it reaches the left-most node in
>>> the directory structure (AFTER the "/home/user"), and THAT one
>>> ("/home/user/Rumple) should be the first to succeed in being created.
>>> The call stack should then unwind, and as it does, the previous SUBS on
>>> the stack should execute their "Finally" section. When the stack has
>>> completely unwound the directory structure should exist.... only that is
>>> not what is happening.
>>> The first Catch doesn't execute (although the directory does not get
>>> created.. meaning an error did indeed occur) and it skips directly to
>>> the "finally". When the mkdir in the "finally" is executed (same
>>> parameter string because we have not yet recursed) the error "File or
>>> Directory does not exist" pops up on the screen. Well there's the error
>>> that I expected from the initial mkdir, but the "catch" didn't execute,
>>> anybody got ideas?
>>>
>>> ------------------------------------------------------------------------------
>>> All of the data generated in your IT infrastructure is seriously valuable.
>>> Why? It contains a definitive record of application performance, security
>>> threats, fraudulent activity, and more. Splunk takes this data and makes
>>> sense of it. IT sense. And common sense.
>>> http://p.sf.net/sfu/splunk-d2d-c2
>>> _______________________________________________
>>> Gambas-user mailing list
>>> Gambas-user at lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/gambas-user
>>>
>>
>>
>> --
>> Fabien Bodard
>>
>
>
More information about the User
mailing list