Why DOES this work??

Can someone please tell me why this code works:

Dim tempSourceFolder as FolderItem = SpecialFolder.Temporary.Child("tempFile") If tempSourceFolder.Exists Then tempSourceFolder.delete

(The temp folder already exists, and contains a text file)

I was under the impression that you could not delete a folder that had files inside it?
I guess I was wrong?

Thanks.

It works because tempSourceFolder does not Exist (unless you created tempFile elsewhere that you’re not showing us).

Yes - the temp folder is created earlier, and contains a text file.

And it’s going away after this code runs?

Yes.
Well, I did a spotlight search for the folder name, and no results were found, so I’m guessing it has been deleted?

How do I find out exactly where this folder (inside the temporary folder) is actually located?

Thanks.

Set a breaking point in your code and check your tempSourceFolder.AbsolutePath in Debugger…

Right, it won’t show up in Spotlight.

OK - I now have the path displayed in the debugger.

This is what it says:
Macintosh HD:private:var:folders:lj:nnc1m_812l1b7d79mglnw9bw0000gn:T:TemporaryItems:Business Details.txt

The questions I have are:

  1. When you create a folder like this:
SpecialFolder.Temporary.Child("myFolder")

Is a completely different folder created each time this is called?
Explanation: The path displayed in the debugger seems to have created a folder with random characters in the path - which leads me to believe that the actual path to the created folder will differ each time it is called?

  1. Do sub-folders created inside the Temporary folder automatically get deleted after the app closes?

Thanks.

  1. yes
  2. yes

hence the word “temporary” :slight_smile:

@Dave,
So basically - when using the code below - the second line is unnecessary, as firstly the tempFile folder will never be exactly the same folder (as it gets random characters prefixed to the path anyway) - and secondly - the folder (or folders) will get auto deleted as soon as the app closes:

Dim tempSourceFolder as FolderItem = SpecialFolder.Temporary.Child("tempFile") If tempSourceFolder.Exists Then tempSourceFolder.delete

Have I now understood this correctly?
Thanks Dave.

First, that code isn’t creating anything other than a reference to a file/folder that may or may not exist.

Second, on a Mac/Linux, the temporary files are cleaned up at restart. On Windows, it’s when the app quits (or should be). In either case, it’s good practice to clean up after yourself if you can.

[quote=196852:@Richard Summers]1) When you create a folder like this:

SpecialFolder.Temporary.Child(“myFolder”)
Is a completely different folder created each time this is called?
Explanation: The path displayed in the debugger seems to have created a folder with random characters in the path - which leads me to believe that the actual path to the created folder will differ each time it is called?

[/quote]

No it won’t. I just tried. The temporary folder is the same every time you refer to it for the session. So either use a different folder name, or use GetTemporaryFolderItem which indeed is different every time you call it, and still resides in the temp folder item.

@Kem,
Thanks.

So OS X temp folders are deleted after a system restart. Cool.

However, what I am still little unsure of is:
If I use the code below to create a text file inside the Temporary folder, like this:

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

// IF FOLDER DOESNT EXIST - CREATE IT
If Not fi.Exists Then

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

End If[/code]

How do I then delete this folder, if a text file has previously been created there?
And finally, if I do not delete the file - what happens if I try to write the text file again? Will it overwrite the existing text file?

Thanks for your patience (I’m trying to get my head around the concept) :slight_smile:

You either have to write a method that will delete the contents of the folder recursively, i.e., will drill down through the contents of the folder and delete each item it finds, or you can issue a shell command to do it.

rm -r /path/to/folder

On the Mac/Linux, the code would look something like this:

dim sh as new shell
sh.Execute "rm -r " + fi.ShellPath
if sh.ErrorCode <> 0 then
  // Deal with the error
end if

This assumes that fi holds a folder within TemporaryItems. If it holds a reference to a file or, worse, the system’s Temporary Items folder, it will either not work or cause problems.

So really, it is best to create a folder inside the Temporary folder, and then create the file inside that.
Then I can use the code you kindly provided, to remove the sub folder and it’s text file.

Thanks Kem - that made it a lot easier to understand.

One final question:
If I tried to create a sub folder, containing the text file, but it already existed - would this throw an error; overwrite the existing file; or do absolutely nothing?

Thank you once again for all the help - it’s much appreciated.

In other words…

dim f as new FolderItem( "/path/to/existing/folder" )
f.CreateAsFolder

That would either do nothing or throw an exception. It wouldn’t overwrite anything. However, I would check to make sure that f.Exists is false before attempting to create it.

Hang on…
Where does a folder come into this?

Dim tempSourceFolder as FolderItem = SpecialFolder.Temporary.Child("tempFile") If tempSourceFolder.Exists Then tempSourceFolder.delete

After the first line, tempSourceFolder is a FILE isnt it?
(which may not even exist)

It would only be a folder if someone had used CreateAsFolder earlier…
All the commands I can see here are creating text files?

Edit: apart from Kems post 4 seconds ago… :wink:

:stuck_out_tongue:

Ok Gentleman,
I think I finally have this nailed (holding my breath):

How does this look?

[code]// DEFINE THE PATH TO THE TEXT FILE
Dim fi As FolderItem = SpecialFolder.Temporary.Child(“TextFolder”)
Dim fi2 As FolderItem = SpecialFolder.Temporary.Child(“TextFolder”).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]

// DEFINE THE PATH TO THE TEXT FILE
Dim fi As FolderItem = SpecialFolder.Temporary.Child("TextFolder")
if not fi.Exists then
  fi.CreateAsFolder
end if
Dim fi2 As FolderItem = fi.Child(NoteName + ".txt")

Also, since Temporary is shared, you might want to give the subfolder a more unique name than “TextFolder”.