What is the use of FolderItem.Exists?


I’m wondering what the use of FolderItem.Exists is? Is there a way to get an exists=false without a NilObjectException?

You’ll get a NilObjectException if the folderitem can’t exist for example a missing folder in your tree. A folder item can exist for a file yet to be created.

Sure, just create a FolderItem to a file that you have not yet created on disk.

For example:

Dim f As FolderItem = SpecialFolder.Desktop.Child("MyFile.txt") // f.Exists is False

dim f as folderitem = SpecialFolder.Desktop.Child("asdkljas98da9isdnlk")

unless you have a file on your desktop called “asdkljas98da9isdnlk”
f.exists will be false
and you know it should (in theory) be safe to create one etc

If F=NIL then the path specified in F “cannot exist, and cannot be created, as the path is invalid in some manner”
If F.Exists=False then the path IS valid, and simply has yet to be created

I like to think of a FolderItem as a POTENTIAL or ACTUAL file on a disk. As everyone has said, and maybe said a little more precisely, a FolderItem can only exist (that is, not be NIL) if it references an actual file or one that could exist at the specified Parent.

Just out of curiosity, though, what if you did this?

f = SpecialFolder.Desktop.Child(“Folder 1”)
f2 = SpecialFolder.Desktop.Child(“Folder 1”).Child(“ThisFile1.txt”)
f3 = SpecialFolder.Desktop.Child(“Folder 1”).Child(“ThisFile2.txt”)
f4 = SpecialFolder.Desktop.Child(“Folder 1”).Child(“ThisFile3.txt”)
// now remember, the TEXT files aren’t created, they are just potentials with Exists=false
f = Nil

Would f2, f3, and f4 all of a sudden become NIL after f gets deleted and destroyed? Or would they be “orphaned” in some way?

good point :slight_smile:
no they wouldn’t become nil, because a reference had already been created, and they are static objects…
I would bet, they would NOT be NIL, nor would they Exist (since they were never created in the first place)
but any attempt to use them would result in an exception

Thanks for the explanation. One way or another I didn’t get the grasp of simple OOP concepts with the FolderItem class. While I use those concepts all the time.