Why DOES this work??

This MUST be correct now???

[code]// DEFINE THE PATH TO THE TEXT FILE
Dim fi As FolderItem = SpecialFolder.Temporary.Child(“SupercalifragilisticexpialidociousFolder”)

If not fi.Exists then
fi.CreateAsFolder
End if

Dim fi2 As FolderItem = SpecialFolder.Temporary.Child(“SupercalifragilisticexpialidociousFolder”).Child(NoteName + “.txt”)

// IF SUB FOLDER AND FILE DOESNT EXIST - CREATE THEM
If Not fi2.Exists Then

// CREATE THE TEXT FILE
Dim tos As TextOutputStream = TextOutputStream.Create(fi2)
tos.Write Window1.noteView.Text
tos.close

// DO SOMETHING WITH THE NEWLY CREATED TEXT FILE HERE
blah blah blah

// NOW DELETE THE SUB FOLDER AND IT’S TEXT FILE, AS IT IS NO LONGER NEEDED
Dim sh as new shell
sh.Execute "rm -r " + fi.ShellPath
If sh.ErrorCode <> 0 then
MsgBox (“oh dear - an error has occurred - AGAIN!”)

Else
MsgBox(“All OK, for once”)
End if

End If[/code]

That’s fine, but why reference the subfolder twice? You’ve already stored it in fi, so just use it to create the child too. That way, if you change the name again, you only have to do it in one place.

See the code I posted above.

OK,
If this isn’t correct - I’m giving up and going to bed!!

[code]// DEFINE THE PATH TO THE TEXT FILE
Dim fi As FolderItem = SpecialFolder.Temporary.Child(“SupercalifragilisticexpialidociousFolder”)

If not fi.Exists then
fi.CreateAsFolder
End if

Dim fi2 As FolderItem = fi.Child(NoteName + “.txt”)

// IF SUB FOLDER AND FILE DOESNT EXIST - CREATE THEM
If Not fi2.Exists Then

// CREATE THE TEXT FILE
Dim tos As TextOutputStream = TextOutputStream.Create(fi2)
tos.Write Window1.noteView.Text
tos.close

// DO SOMETHING WITH THE NEWLY CREATED TEXT FILE HERE
blah blah blah

// NOW DELETE THE SUB FOLDER AND IT’S TEXT FILE, AS IT IS NO LONGER NEEDED
Dim sh as new shell
sh.Execute "rm -r " + fi.ShellPath
If sh.ErrorCode <> 0 then
MsgBox (“oh dear - an error has occurred - AGAIN!”)

Else
MsgBox(“All OK, for once”)
End if

End If[/code]

That looks good.

YEEEEEEEEEEHAAAAAAAAA!!!

Thank you so much Kem !

[quote=196922:@Richard Summers]OK,
If this isn’t correct - I’m giving up and going to bed!!

[code]// DEFINE THE PATH TO THE TEXT FILE
Dim fi As FolderItem = SpecialFolder.Temporary.Child(“SupercalifragilisticexpialidociousFolder”)

If not fi.Exists then
fi.CreateAsFolder
End if

Dim fi2 As FolderItem = fi.Child(NoteName + “.txt”)

// IF SUB FOLDER AND FILE DOESNT EXIST - CREATE THEM
If Not fi2.Exists Then

// CREATE THE TEXT FILE
Dim tos As TextOutputStream = TextOutputStream.Create(fi2)
tos.Write Window1.noteView.Text
tos.close

// DO SOMETHING WITH THE NEWLY CREATED TEXT FILE HERE
blah blah blah

// NOW DELETE THE SUB FOLDER AND IT’S TEXT FILE, AS IT IS NO LONGER NEEDED
Dim sh as new shell
sh.Execute "rm -r " + fi.ShellPath
If sh.ErrorCode <> 0 then
MsgBox (“oh dear - an error has occurred - AGAIN!”)

Else
MsgBox(“All OK, for once”)
End if

End If[/code][/quote]

Unfortunately, this code is not robust. SpecialFolder.Temporary and FolderItem.Child can return Nil for all sorts of reasons. TextOutputStream.Create can return nil. You should check the return value before using it.

A more subtle error is that this code

If not fi.Exists then fi.CreateAsFolder End if

leaves you exposed to a race condition, because it is possible that another process could create an item at fi after you check fi.Exists, but before you create the folder. The better practice is to just attempt to create the folder, then check to see whether it succeeded.

Arghhhhhhhhhhhhhhhhhhhh (crying uncontrollably) :frowning:

I’m now in a complete muddle and have absolutely no idea where to add the extra error checks as kindly advised by Charles?
Can anyone improve upon my code below, so that I can see how it should be done?

[code]// DEFINE THE PATH TO THE TEXT FILE
Dim fi As FolderItem = SpecialFolder.Temporary.Child(“SupercalifragilisticexpialidociousFolder”)

If not fi.Exists then
fi.CreateAsFolder
End if

Dim fi2 As FolderItem = fi.Child(NoteName + “.txt”)

// IF SUB FOLDER AND FILE DOESNT EXIST - CREATE THEM
If Not fi2.Exists Then

// CREATE THE TEXT FILE
Dim tos As TextOutputStream = TextOutputStream.Create(fi2)
tos.Write Window1.noteView.Text
tos.close

// DO SOMETHING WITH THE NEWLY CREATED TEXT FILE HERE
blah blah blah

// NOW DELETE THE SUB FOLDER AND IT’S TEXT FILE, AS IT IS NO LONGER NEEDED
Dim sh as new shell
sh.Execute "rm -r " + fi.ShellPath
If sh.ErrorCode <> 0 then
MsgBox (“oh dear - an error has occurred - AGAIN!”)

Else
MsgBox(“All OK, for once”)
End if

End If[/code]

Here’s my view of how it might be done.

  dim temporary as FolderItem = SpecialFolder.Temporary
  if temporary = nil then
    //handle error and bail out...
  end if
  
  //it's generally safer to use TrueChild in this situation in case a symlink was created at the path.
  dim SupercalifragilisticexpialidociousFolder as FolderItem = temporary.TrueChild("SupercalifragilisticexpialidociousFolder")
  if SupercalifragilisticexpialidociousFolder = nil then
    //handle error and bail out...
  end if
  
  SupercalifragilisticexpialidociousFolder.CreateAsFolder
  if not SupercalifragilisticexpialidociousFolder.Directory then
    //you can check LastErrorCode, but I've found it unreliable over the years.  I've also found this test to be not always
    //reliable as well.
    //handle error and bail out...
  end if
  
  //now the folder has been created.  To ensure that it is cleaned up even if an exception happens, we use a 
  //try-finally block.
  try
    dim fi2 As FolderItem = fi.Child(NoteName + ".txt")
    if fi2 <> nil then
      //since you want to call tos.Close...
      dim tos As TextOutputStream = TextOutputStream.Create(fi2)
      //TextOutputStream.Create docs say that it might raise an exception, but does not say anything about
      //the return value.  Thus you need to anticipate both an exception and a Nil return value.
      if tos <> nil then
        //we want to ensure that tos.Close is called even if an exception is raised.
        try
          tos.Write(Window1.noteView.Text)
        finally
          tos.Close
        end try
      end if
    else
      //handle error...
    end if
  exception e as IOException
    //TextOutputStream.Create did not succeed.  Handle error...
  finally
    Dim sh as new shell
    sh.Execute "rm -r " + SupercalifragilisticexpialidociousFolder.ShellPath
    if sh.ErrorCode <> 0 then
      //this probably isn't a big deal; it means that you've left some files in the temp directory.
    end if
  end try

Thanks Charles - I will compare mine to yours, and see what I was missing.

Much appreciated - Thank You!