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