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.
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")
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?
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.
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.
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.