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

tobias tobiasboe1 at ...20...
Thu Jun 30 15:57:53 CEST 2011


hi,

> 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?
> 

first of all, i wouldn't use CATCH and FINALLY. for me, these are in my 
code only to handle errors. also calling the function recursively pushes 
unneccessary stackframes onto the stack because you can do this in a 
loop, too. last, i can't figure out the meaning of this line:
sFolderSpec = Mid$(psFolderSpecification, 1, 
RInStr(psFolderSpecification, ".") - 1)
why do you search for "." as your given string doesn't even contain one 
and you say that this code will copy the path until the prior char of 
the last "/"?
well, this is what i would have used:

PUBLIC SUB Button1_Click()

   BuildDirTree("////home/user/This/../This//Is/.//../Not/../A/Tree")

END

PUBLIC SUB BuildDirTree(sPath AS String)

   DIM sCreate AS String = "/" 'Will contain the next path to create

   'make the path end with a "/"
   sPath &/= "/"

   'finish when we built the tree
   WHILE NOT (sCreate = sPath) OR NOT Exist(sPath)
     IF NOT Exist(sCreate) THEN
       MKDIR sCreate
       PRINT "[*] Creating "; sCreate
     ENDIF
     sCreate = Mid$(sPath, 1, InStr(sPath, "/", Len(sCreate) + 1))
     IF sCreate = "" THEN BREAK 'this will be the last part
   WEND
   PRINT "[*] Built successfully"

   CATCH
   Message.Info("Error: " & Error.Text)

END

which works just fine for me and i was suprised that it also handles 
this weird path i give...

regards,
tobi




More information about the User mailing list