I’m sure this is entirely on me, but I’m loading a JSON file that is filled with relative path filenames. I have a FolderItem which I use as the “project root”, and was expecting to be able to do things like:
var f as FolderItem = root.Child(json.Value("imagePath").StringValue)
However, if - for example - the JSON imagePath is something like "data/images/logo.png", after calling root.Child the resulting FolderItem is now:
"/My/Project/root/data:images:logo.png"
Which is obviously very unusable and the file isn’t found. If I loop with a .Split("/") and individually append each child item then it works as expected.
Is there a way for me to do what I want here without having to manage OS-dependent path separators and splitting myself?
You can combine the filepath of the root folder with the one from “imagePath”
I used one path for the Desktop Folder, and then another for the text file inside on folder that’s inside another folder that’s on my desktop.
Var f As FolderItem = SpecialFolder.Desktop
If f.Exists And f <> Nil Then
MessageBox("Found Desktop Folder " + f.Name)//Found the folder
End If
Var p As String = f.NativePath
Var s As String = "Test Paths/Path 01/Path 1A/Test Doc.txt"
Var fp As String = p + "/" + s
f = New FolderItem(fp, FolderItem.PathTypeNative)
If f.Exists And f <> Nil Then
MessageBox("Found " + f.Name)//Found the file
End If
Is not working as expected because we guess that using .Child will convert the string that shows ‘/’ to .child(“data”).child(“images”).child(“logo.png”) but that is not how this works.
As Mark said, create your string then use the FolderItem creator to get what you need.
What @AlbertoD said. So yes, you will have to worry about the platform dependent separator. But you could make a platform dependent string constant for that, at least.
Not the answers I was hoping for, but thank you all.
Follow-up: is the documentation wrong regarding the Extension property? It states that it can be used to get and set the extension, but no matter what I do it never sets.
I can confirm that Xojo allows you to assign a new value to a FolderItem.Extension property, however, it does not change the underlying FolderItem. This is true even when the FolderItem does not represent an actual file on disk.
So it is not just a documentation issue, the property is Read / Write but assigning doesn’t do anything.