Directory Creation Behavior

I was seeing some strange behavior with existing code when run under Xojo 2017r1. I’ve condensed the behavior into a single action item.

Create a simple window with a single push button control and put the following code into the “Action” event handler:

Dim test_dir As FolderItem
Dim app_path As String
Dim app_dir As FolderItem

app_dir = SpecialFolder.Desktop
app_path = app_dir.NativePath
test_dir = GetFolderItem( app_path + "TestDir" )
MsgBox( "test_dir LastErrorCode: " + Str( test_dir.LastErrorCode ) )
If ( ( test_dir <> Nil ) AND ( test_dir.LastErrorCode > 0 ) ) Then
  MsgBox( "Failed to Obtain Path to Test Directory - Error Code: " + Str( test_dir.LastErrorCode ) )
  
ElseIf ( ( test_dir <> Nil ) AND ( NOT test_dir.Exists ) ) Then
  test_dir.CreateAsFolder()
  If ( test_dir.LastErrorCode > 0 ) Then
    MsgBox( "Failed to Create Test Directory - Error Code: " + Str( test_dir.LastErrorCode ) )
  End If
  
End If

Self.Close()

If you execute the code without any breakpoints - everything will execute correctly and a folder named “TestDir” will be created on your desktop.

Now delete any existing “TestDir” folder on your desktop, set a Breakpoint at the “if” statement. After the message box tells you that the test_dir.LastErrorCode is 0, then examine the TestDir FolderItem and look at the LastErrorCode - it will show to be 101.

If you run the code with or without the breakpoint, all works well. However, if you examine the FolderItem LastErrorCode and then step into the If, you will get the error message.

The basic purpose of the code snippet is to create the directory if it does not already exist, so make sure you delete any existing folder prior to running the code each time.

I am confused how the MessageBox can tell me the error code is 0, but if I examine it, it shows as 101.

I see different behavior running Xojo 2015r2.4

Ron Bower

.

I’m new to xojo; But it seems that GetFolderItem() is has to do a stat() to determine existence, and if the folder isnt there, the LastErrorCode should be 101.

I’m confused as to why the code displays it as zero, but inspecting it I see its 101 (same as what Ron sees)…
This appears to only be an issue on Windows…

Running on win10 with 2017r1 I see the odd behavior Ron sees.
Running on macos 10.12.5beta x86 32 bit & x86 64 bit: the msgbox shows the value as 101, and inspecting shows the value to be 101
running on ubuntu 16.04 an x86-64 bit image the msg box shows error 2 (which would be errno 2, ‘no such file or directory’)

Is there a problem with LastErrorCode on windows, or am I misunderstanding when to use LastErrorCode?
tks
-jim

I think you’re misunderstanding when to use LastErrorCode. It’s not useful following a GetFolderItem, as you haven’t done anything to the file yet. There are 3 possible outcomes after GetFolderItem

  1. Nil - you passed in a bad path
  2. Exists = False
  3. Exists = True

You should test the folderitem for Nil and then test Exists. LastErrorCode doesn’t enter into the picture.

That said, there are a few things wrong with that code:

  1. You should pass the path type to GetFolderItem
  2. You shouldn’t build a path like that in the first place. GetFolderItem is the wrong thing to use. User FolderItem.Child:
app_dir = SpecialFolder.Desktop
if app_dir <> Nil then
   test_dir = app_dir.Child("TestDir")
end

Tim - Thank you!
-jim

Very helpful, Tim !!!

Thank you for the time explaining the correct procedure.

Ron

Sorry to be a P.I.T.A., but trying to create code to eventually cross-compile to Linux and OS X…

So I have created a “FetchFolder” function that looks like this:

[code]Dim fi As FolderItem

fi = Dir.Child( Folder )
If ( fi = Nil ) Then
MsgBox( "Failed to Obtain Path to Directory " + Folder )
Else
If ( fi.Exists ) Then
If ( NOT fi.Directory ) Then
MsgBox( "Unable to create folder " + Folder + EndOfLine + “You may already have a file by that name.” )
fi = Nil
End If
Else
fi.CreateAsFolder()
End If
End If
Return( fi )
[/code]

It is called from a PushButton Action event and uses the info from a text box:

[code]Dim test_dir As FolderItem
Dim app_dir As FolderItem
Dim fi As Folderitem

app_dir = SpecialFolder.Desktop
test_dir = FetchFolder( app_dir, TextField1.Text )

If ( test_dir <> Nil ) Then
MsgBox( “Success” )
Else
MsgBox( “Failure” )
End If
[/code]

Now, IF the folder is created okay (Non-Nil), I’d like to create a file in the folder named “Test.txt”.

How would you recommend I create that file ?

Ron Bower

if test_dir <> Nil then
   // MsgBox( "Success" )
   fi = test_dir.Child("Test.txt")
   if fi.Exists then
       MsgBox( "The file Test.txt already exists" )
   else
      dim to as TextOutputStream = TextOutputStream.Create(fi)
    end if
else
    MsgBox( "Failure" )
end if

Thanks, Tim, very helpful, as usual.