Filepath question

I’m using someone else’s code to solve a problem and here is part of it that I have a question about:

Dim f As FolderItem f = GetFolderItem("") f = GetFolderItem(f.NativePath + Lowercase(App.ExecutableFile.Name) + ".cgi")

Why do I need NativePath here? Can’t I just do this:

Dim f As FolderItem f = GetFolderItem(Lowercase(App.ExecutableFile.Name) + ".cgi")

Wouldn’t that work on all platforms because it doesn’t reference any platform-specific things like forward or backward slashes? Perhaps I don’t truly understand what I read about NativePath in the docs.

It looks as if you are trying to use the code to upgrade cgi apps posted by Phillip Zedalis at http://www.dev.1701software.com/blog/2013/09/08/upgrading-xojo-cgi-apps

I have been using it as posted there for years without any glitch, so I am inclined not to fix what is working.

However, indeed, you can probably simply do

f = Getfolderitem(App.Executablefile.Shellpath+".cgi", FolderItem.PathTypeShell)

Or the same with NativePath.

Yes. That’s the code I’m using, and it works great. I was just posting this as a learning experience, to see if I understood Xojo filepath conventions. I have no intention of changing what’s solving a problem beautifully for me.

So, alternatively to using NativePath or ShellPath, when I read the docs, it looks like I can also do this:

Dim f As FolderItem f = GetFolderItem("") f = f.Child(Lowercase(App.ExecutableFile.Name) + ".cgi")

you should not use GetFolderItem with paths math.

f = Getfolderitem(App.Executablefile.name+".cgi")

with name, it is better.

Yes, this would work :

Dim f As FolderItem f = GetFolderItem("") f = f.Child(Lowercase(App.ExecutableFile.Name) + ".cgi")

But it is somewhat unnecessary, since App.ExecutableFile is a folderItem that replaces everything but the .cgi.

If your intention is to explorer the building of a folderItem with Child and Parent, you could also do :

f = App.ExecutableFile.Parent.Child(App.ExecutableFile.Name)+ ".cgi")

I usually prefer the folderItem construction over path, except to save and load on file. I do not believe it is necessary to use Lowercase, as Xojo creates automatically the files in lowercase.

So, no longer talking about App.ExecutableFile.Name, let’s say I want to access or create a file in a subfolder of the executable folder. At first, I was doing this:

Dim f As FolderItem f = GetFolderItem("MySubfolder/MyFile")

But I worried about the forward slash not being recognized on all platforms (e.g., Windows). So I changed it to

f = New FolderItem f = GetFolderItem("") f = f.Child("MySubfolder").Child("MyFile")

Could I have done that with NativePath or ShellPath?

of course you can do NativePath/ShellPath, but you may always run into problems.
So Child is highly preferred.

and with your last code, please remove who lines:

dim MySubfolder as folderitem GetFolderItem("MySubfolder") f = MySubfolder .Child("MyFile")

The “new folderitem” is not needed. And getfolderitem can take the name of the folder directly, so you don’t need one Child call.

Isn’t there a missing equal sign in the first line and an extra space in the second line? So it should like this? …

dim MySubfolder as folderitem = GetFolderItem("MySubfolder") f = MySubfolder.Child("MyFile")

or like this, if I don’t like the variable name matching the folder name? …

Dim f As FolderItem = GetFolderItem("MySubfolder") f = f.Child("MyFile")

I think Xojo allows some variables to be instantiated and set in the same line, and some not. Isn’t that right? Since I don’t remember which allow it, for now I’m always instantiating on one line and setting on another. Didn’t know I could do both on the same line with FolderItem.

That is just fine if you are sure MySubfolder exists.

Ah, yes. I’ll put in a check for the existence of the subfolder and create if not. It’s a subfolder I manually created on the server, but who knows what can happen later, unbeknownst to me. Thanks.

dim MySubfolder as folderitem = GetFolderItem("MySubfolder") f = MySubfolder.Child("MyFile")

sorry, I typed too quickly.
I prefer the temp variable here, so you can later put an if there to check if file exists or is nil.

I went back to re-reading the online Xojo documentation after reviewing the replies in this thread from Michel and Christian. I think I understand the New operator better and why I didn’t need it in certain places I inserted it. Please correct me if needed here…

It appears that I do not need the line with the New operator here

f = New FolderItem f = GetFolderItem("MyFolder")

because GetFolderItem does that for me. So I can eliminate the first line above.

Likewise, if I use SpecialFolder, that also creates a FolderItem, so I won’t need the New operator line below:

f = New FolderItem f = SpecialFolder.Documents.Child("MyFolder")

So the New operator is used for instantiating objects, but is not needed if some other process implicitly does the instantiating. I’ve been using the New operator far more often that needed, and compilation isn’t going to tell me about that because it’s not an error.

Indeed the object is created for you by GetFolderItem() or SpecialFolder.