Using FolderItem.Child without errors

I’ve started to replace some bad code I wrote in RB many years ago eg.
Old code (still works fine):

myfolderItem = getfolderItem(f.absolutePath + "xFile.txt") if myfolderItem.exists then // do this else // goes here if file doesn't exist // (all good!)

New code (crashes program):

myfolderItem = f.child("xFile.txt") if myfolderItem.exists then // fine if file exists else // never goes here if file doesn't exist //exits with error


The above causes an error if myfolderItem is a file but not if it is a folder.
So should I be using -
folders: if myfolderItem.exists
files: if myFolderItem <> Nil

Or is it worth just using both tests in both situations

if myFolderItem <> Nil And myFolderItem.exists 

This is a shame because I have so many of these in this project that I have to manually change. I was hoping to just do find/replace.
BTW: If I replace AbsolutePath with Nativepath the program breaks on Mac so please don’t go there at this time.
This exercise is about having one set of code that avoids NativePath and AbsolutePath so the program can be compiled in old and new Xojo IDE for an old and new systems.

If FolderItem is NIL then the attempt to assign it a path (with or without a filename) was invalid
Otherwise the Folderitem will not be NIL… but all that means is that it defines a POSSIBLE valid filepath… You then need to check for EXISTS

So you should check BOTH regardless of if you are looking for a Folder or a File

If NOT NIL and does EXIST, then .Directory will tell you if it is a Folder vs a File

Mani years ago I created this function that I use continuosly:

Function NilExists(f as FolderItem) As Boolean If f = Nil Then Return True if f.NotExists Then Return True Return False End Function

You can use:

If NilsExists(myFolderitem) Then...

Unfortunately you can no extend a folderitem with something like

Function NilExists(extends f as FolderItem) As Boolean If f = Nil Then Return True if f.NotExists Then Return True Return False End Function
to use

If myFolderitem.Nilexists Then...

because you get an error if myFolderitem is Nil.

Ok will be doing that.

… but wouldn’t it be great if FolderItem.exists simply returned False for a file or folder that isn’t there, instead of throwing an error on one and not the other.

You can’t talk to a folderitem if there is no folderitem.

Sorry. In my example

 if f.NotExists Then Return True

is something particular. You should use

If Not f.Exists Then Return True

Unless you already know it’s not Nil, you should always test before accessing properties.

What I would be querying is why the old code works and the new code doesn’t as at first glance they should be producing the same result.

f.absolutePath
a. AbsolutePath is deprecated, you are running for troubles using it.

b. Since you do not check if the “AbsolutePath” really exists, you are running into troubles anyway (even with NativePath or others): you cannot know if your user (or the OS or…) rename/move/remove one folder in the Path…

When looking for trouble(s), most of the time, you are finding them…

Yes, and with all that security crap I’ve added an “if f.isreadable then” recently.

[quote=442182:@Emile Schwarz]f.absolutePath
a. AbsolutePath is deprecated, you are running for troubles using it.

b. Since you do not check if the “AbsolutePath” really exists, you are running into troubles anyway (even with NativePath or others): you cannot know if your user (or the OS or…) rename/move/remove one folder in the Path…

When looking for trouble(s), most of the time, you are finding them…[/quote]
Have you replied to the correct post because I’m sure my post says “Bad Code” right above f.absolutePath and then displays checks to see if the folderItem exists.

PS. We are all trying to move forward with our code, I’m looking at joining AA (AbsolutePath.anonymous).
Just be gentle on us who are trying to develop for legacy hardware and especially if it is just for Windows because for many of us NativePath doesn’t make a scrap of difference.
We know the worlds not flat and I wish the nativePath fratertiny would stop telling me I’m going to sail off the edge.

[quote=442157:@Craig Grech]I’ve started to replace some bad code I wrote in RB many years ago eg.
Old code (still works fine):

myfolderItem = getfolderItem(f.absolutePath + "xFile.txt") if myfolderItem.exists then // do this else // goes here if file doesn't exist // (all good!)

New code (crashes program):

myfolderItem = f.child("xFile.txt") if myfolderItem.exists then // fine if file exists else // never goes here if file doesn't exist //exits with error


The above causes an error if myfolderItem is a file but not if it is a folder.
So should I be using -
folders: if myfolderItem.exists
files: if myFolderItem <> Nil

Or is it worth just using both tests in both situations

if myFolderItem <> Nil And myFolderItem.exists 

This is a shame because I have so many of these in this project that I have to manually change. I was hoping to just do find/replace.
BTW: If I replace AbsolutePath with Nativepath the program breaks on Mac so please don’t go there at this time.
This exercise is about having one set of code that avoids NativePath and AbsolutePath so the program can be compiled in old and new Xojo IDE for an old and new systems.[/quote]
I stared at your code for some while thinking that the two should have been identical. And then it hit me. Let’s say the the file/folder pointed to by F is c:\some\path.

If F is a folder, f.AbsolutePath will be “c:\some\path”. Appending “xFile.txt” to it yields “c:\some\path\xFile.txt”. That is a file that may or may not exist, but lies in an existing path “c:\some\path”.

If F is a file, then f.AbsolutePath will be “c:\some\path”. Appending “xFile.txt” to it yields “c:\some\pathxFile.txt”. That is a file that most certainly doesn’t exist, but lies in an existing path “c:\some”.

In both cases, myFolderItem will be non-Nil and may or may not exist.

However, if when you use .Child, things go a little differently.

If F is a folder, then f.Child(“xFile.txt”) yields “c:\some\path\xfile.txt”). That is a file that may or may not exist, but lies in an existing path “c:\some\path”.

If F is a file, then f.Child(“xFile.txt”) yields “c:\some\path\xFile.txt”) - notice the difference. That is a file that lies in an impossible path “c:\some\path”. It will always be nil.

(https://xojo.com/issue/55731)>] .IsReadable and .IsWriteable are unreliable, see

You should just go ahead and try reading or writing to the folderitem, then trap any exception.

Ah, thanks. :slight_smile:

[quote=442158:@Dave S]If FolderItem is NIL then the attempt to assign it a path (with or without a filename) was invalid
Otherwise the Folderitem will not be NIL… but all that means is that it defines a POSSIBLE valid filepath… You then need to check for EXISTS

So you should check BOTH regardless of if you are looking for a Folder or a File

If NOT NIL and does EXIST, then .Directory will tell you if it is a Folder vs a File[/quote]

Agreed - and always check Nil first, then .Exists