Testing for the existence of a file

I’ve got a Xojo Web app working very well right now. It loads in a browser, accepts data entry, exports the entered data to a CSV file and emails that CSV file. I now want to attach a SQLite database to it for data storage. In the Session’s Open event, I have this simple code to see if it sees the new database file:

Dim dbFile As FolderItem
dbFile = GetFolderItem("mydatabase.sqlite")
If dbFile.Exists = False Then
    MsgBox  "No database file."
End If

Though mydatabase.sqlite does indeed exist in the same folder as the project, when I Run the project, it always reports (via Msg Box you see there) the database doesn’t exist. What might I not understand here? It must be something very fundamental.

According to the docs, http://documentation.xojo.com/index.php/GetFolderItem is not available for Web.

Well, that certainly explains my frustration at trying to get this to work. Thanks, Parnell. So it sounds like my best bet is to just load the database and test to see if it is connected.

I think the LR is in error. Maybe there was confusion with GetOpenFolderItem which does not exist in Web.

I just tested, GetFolderItem works just fine.

However, in the code posted, the code assumes the file is next to the executable, which may not be the case for a Windows debug, and also the type of path is not specified. That probably explains.

Instead of placing the file manually, use a CopyFile build step to copy the file in the executable directory. That way it should work in debug run as well.

I would also do :

dbFile = GetFolderItem("mydatabase.sqlite", FolderItem.PathTypeShell)

Ok. I did a test like this:

Dim currentFolder As FolderItem currentFolder = GetFolderItem("") MsgBox(currentFolder.Name)

and indeed found the current folder was the window.debug folder, so that was the problem. I ended up placing my database file in my Documents folder for now, and use this:

Dim dbFile As FolderItem dbFile = SpecialFolder.Documents.Child("mydatabase.sqlite") If dbFile.Exists = False Then MsgBox "No database file." End If

and that works fine. When I finally deploy this, I can change the code to look in the executable’s folder again.

You can also use an

If TargetBuild

to use the proper path in each case.

Can I tell via code if this is a Debug session?

#if DebugBuild

Add a copy files step to your build to copy the database when you debug.

The first thing I do when “somethings not working” is I check the docs to see how it works.
Silly me for assuming the documentation would be correct :stuck_out_tongue:

Of course. Sorry.

[quote=247182:@Tim Parnell]The first thing I do when “somethings not working” is I check the docs to see how it works.
Silly me for assuming the documentation would be correct :P[/quote]

It’s alright. We all do. Except without reading the docs, I had used GetFolderItem before in Xojo Web, so I knew it worked :wink:

42550 - (Documentation) GetFolderItem works perfectly in Web
Status: Needs Review Rank: Not Ranked Product: Xojo Category: N/A

Michel Bujardet Today at 3:08 AM
OS: OS X 10.11.4

Xojo: Xojo 2015r4.1

Steps: On the page http://documentation.xojo.com/index.php/Getfolderitem

It is mentioned as project type Desktop only.

It works perfectly too in :

  • Web
  • Console

Maybe there was some confusion with GetOpenFolderItem() which exists only in Desktop ?

It would be nice to correct the page.

<https://xojo.com/issue/42550>

Ok. This seems to work. I just give a full path to the database file, which lives in the folder housing the executable, like this

Dim dbFile As FolderItem
dbFile = GetFolderItem("/Users/ralph/Dropbox/Xojo Projects/mydatabase.sqlite", FolderItem.PathTypeShell)
If dbFile.Exists = False Then
    MsgBox  "No database file found."
    Session.Quit
End If

It fails if I don’t have FolderItem.PathTypeShell in there, as Michel suggested.

Actually, since I program on a Mac but the project might end up on a nonMac server (very likely), I changed it to this, assuming the database file is placed in the same folder as the executable:

If DebugBuild Then dbFile = GetFolderItem("/Users/ralph/Dropbox/Xojo Projects/mydatabase.sqlite", FolderItem.PathTypeShell) Else dbFile = GetFolderItem("mydatabase.sqlite") End If If dbFile.Exists = False Then MsgBox "No database file found." Session.Quit End If

If it ends up on a Windows server the database will be read only if the app is placed in the correct location i.e. program files.