Folders

Hello,
I have some doubts regarding folders. For exampleI understand the use of …SpecialFolder.Documents.Child…
How do you code the path to save/open a folder that does not belong to SpecialFolder (say, a folder within somewhere else, for example in DropBox or other places)?

Thank you

SpecialFolder is a container for each of the Standard platform specific folders. So Documents is the usual place for a user to store documents, etc. Things that aren’t standard would have to be approached in a different way. If the folder you are after is within a standard location, such as Documents/DropBox then you can use .Parent and .Child methods to navigate up and down the folder hierarchy.

// Assuming it would be "Documents/DropBox"
Var oDocuments as FolderItem = SpecialFolder.Documents
Var oDropBox as FolderItem = oDocuments.Child( "DropBox" )

// If it was DropBox as a folder next to Documents you could use:
Var oDocuments as FolderItem = SpecialFolder.Documents
Var oDropBox as FolderItem = oDocuments.Parent.Child( "DropBox" )

This allows you to deal with a Child inside the Documents Folder. That Child can be a File or a Folder.

Care to expand what you meant t with “doubts” ?
Or explain the real trouble…

Use the FolderItem constructor for arbitrary paths. Eg.,

var f as New FolderItem(“c:\somefolder\myfile.txt”)

or

var f as FolderItem
f = New FolderItem(“c:\somefolder\myfile.txt”)

and get a crash if you have a typo in the path…

NB: the passed path is for Windows. (it starts with "c:"…)

Sure, we could unwind that to a series of checks and Child calls, but a simple question calls for a simple answer. The question was, what do I do if the file isn’t in any of the SpecialFolder locations? The answer is, use New FolderItem.

Only if you don’t use exception handlers :wink:
Try…Catch is your friend here.

1 Like

Doubts in how to deal with folders that do not belong inside Documents or any SpecialFolder.
Say my target folder has a path like: Users\DropBox\BigFolder\MyFolder, how do I address it to save and/open a file within MyFolder. I cannot use SpecialFolder.Documents.…
By the way, I use an iMac

Do it as described in the Constructor Section of the Docs: FolderItem — Xojo documentation

Put File Operations in Try…Catch Statements, to be able to catch IOExceptions and alike.
And always Check for NIL and maybe Exist. :slight_smile:

Try

// FolderItem Stuff goes here

Catch err As IOException

// Exception Handling goes here

End Try

Its simple: ask the user where the folder is and store its path. Before using the path, check if it exists…
And keep in mind, that hardened runtimes and sandboxed apps cannot save to any folder outside the sandbox without a dialog…

f = new FolderItem ("/Users/DropBox/BigFolder/MyFolder", FolderItem.PathModes.Native)

I made it. Thanks everybody for your siggestions and hel

Good day Tim,
I’m sorry Ineed your help again.
I followed your suggestion as far as addressing a folder not belonging to SpecialFolder family and it works if you use it to save/retrieve data via BinaryStream.
What if I put an SQLite DB inside said folder and want to reach it? How do I address it?
Thanks again in advance

f = new folderitem ("/path/to/the/folder/mysqlite.db", FolderItem.PathModes.Native)

Hello Tim,
I added this:
AutoFldr = new FolderItem (“/Users/shep201/MEGAsync/CASA iMac/ANNO IN CORSO/CASA/AUTO/DATI AUTO/Auto.db”, FolderItem.PathModes.Native),
I tried with …/Auto.sqlite but in both cases I get a NilObjectException error

Please show that section of code, and which line gets the Nil exception.

Also, if posting code:

  1. Add the code to your post
  2. Select it with the mouse
  3. Click the </> button.

Would’nt it be safer to use something like:
SpecialFolder.UserHome.Child("MEGAsync").Child("CASA iMac").Child...?

Yes it would, but Iwas trying to break free from SpecialFolder and see if it could be done putting the DB somewhere else. It’s just an exercise

Ok, I think I fixed it (although I have other problems but I’working on them.)
For my knowledge: how do you get the line which causes the error?

Thank you

Var f As FolderItem
Try
  // replace the path with your reall path
  f = new folderitem("/mypath/to/the/folder/myfilename", FolderItem.PathModes.Native)
  If Not(f = Nil) Then
    If f.Exists then
      // Your code to open the file, move the file or some other file operation goes here.
      // What follows is opening a a text file example:
      Var t As TextInputStream
      t.Encoding = Encodings.UTF8 //specify encoding of input stream
      TextArea1.Text = t.ReadAll
      t.Close
    Else
      MessageBox("The FolderItem des not exist in the path specified, the File Open operation Failed")
    End If
  Else
    MessageBox("The FolderItem was Nil. The File Open operation Failed")
  End If
Catch e As IOException
  MessageBox("Error Number: " + e.ErrorNumber.ToString + " Error Description: " + e.Message)
End Try