[Gambas-user] Try Catch fail when using mkdir....

nando nando_f at ...951...
Fri Jul 1 16:36:00 CEST 2011


In a true multi-user multi-tasking environment the following is good
except it is missing one thing:
The TRY must be added because during the IF test and the MKDIR,
it might have been created.  This makes it the list error prone.



Private Sub CreateNewOutputFolder(psFolderSpecification As String)
Dim sFolderSpec As String

    sFolderSpec = File.Path(psFolderSpecification)
    
    If Exist(sFolderSpec) Then
         'ok, do nothing
    Else
         TRY Mkdir sFolderSpec    <------TRY ADDED HERE
    End if

End


And, the whole above can simply be just one line:

Private Sub CreateNewOutputFolder(psFolderSpecification As String)

    TRY MKDIR File.Path(psFolderSpecification)

End

-Fernando





---------- Original Message -----------
From: Rolf-Werner Eilert <eilert-sprachen at ...221...>
To: gambas-user at lists.sourceforge.net
Sent: Fri, 01 Jul 2011 10:21:31 +0200
Subject: Re: [Gambas-user] Try Catch fail when using mkdir....

> Hi Stephen,
> 
> my first thought was that it might be If Not Exist... I had such a case 
> some time ago, and it did not react as expected. Just check if it really 
> reports TRUE if the directory isn't there. (I remember Benoit explained 
> why it didn't run correctly in my case, maybe you find the thread in the 
> archives.)
> 
> Then I thought why do you use an error condition when you could simply 
> look for the directory and decide whatever when it exists or not? Why 
> going recursively at all? And you could use File.Path to ensure the  the 
> psFolderSpecification is cut to its correct part.
> 
> Private Sub CreateNewOutputFolder(psFolderSpecification As String)
> Dim sFolderSpec As String
> 
> 	sFolderSpec = File.Path(psFolderSpecification)
> 	
> 	If Exist(sFolderSpec) Then
> 		'ok, do nothing
> 	Else
> 		Mkdir sFolderSpec
> 	End if
> 
> End
> 
> Regards
> 
> Rolf
> 
> Am 01.07.2011 05:58, schrieb Stephen Bungay:
> >     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
> >>>
> >>
> >>
> >
> >
> > ------------------------------------------------------------------------------
> > 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
> >
> 
> ------------------------------------------------------------------------------
> 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
------- End of Original Message -------





More information about the User mailing list