Referencing a file in Xojo Cloud

Simple story, not so obvious answer.

I have a Console app uploaded to my Xojo Cloud. The app is run from a Shell, which a Web App provides. Thank you Greg for helping that functionality come to pass.

The console App is a Linux build and lives in the Shared_Documents folder, in a folder named TMW-Alpha. It references local files, in particular a log file (ProjectLog.txt) in the same folder as the console app. This folder also contains the console app Libs folder.

Now if you are a hardworking, decent person, you might think that you reference this TMW-Alpha folder as a subfolder within SpecialFolder.SharedDocuments … and you would be wrong. That does not work (i.e. nil object exception). A Linux build, even though it runs within the Xojo Cloud environment, cannot find the Shared_Documents folder of your Xojo Cloud using any SpecialFolder notation that I have found. If others know of secret sauce which will divulge this folder easily, please share !

My solution is sufficiently ugly that I have thought to share it here, so that others may read, and weep.

[code]dim fA, fA2, fA3, fA4, fA5, fB as folderItem

if debugBuild then
return SpecialFolder.Desktop.child(“Text-Me-Web-Folder”).child(“ProjectAlpha”)

else
fA = SpecialFolder.CurrentWorkingDirectory

if fA.exists = false then return nil

fA2 = fA.child("home")
if fA2.exists = false then return nil

fA3 = fA2.child("sites")
if fA3.exists = false then return nil

fA4 = fA3.child("www.example.com")
if fA4.exists = false then return nil

fA5 = fA4.child("Shared_Documents")
if fA5.exists = false then return nil

fB = fA5.child("TMW-Alpha")
if fB.exists = false then return nil

return fB[/code]

This ugliness will return a folderItem that is either nil, or points to the desired location ( a subfolder named TMW-Alpha within the Shared_Documents folder).

Hope this will save others in their search for their folders.

Regards,
Tony Barry
Sydney

Code like this should work to find TMW-Alpha:

  dim f as FolderItem = SpecialFolder.SharedDocuments.child("TMW-Alpha")
  
  if f.Exists then
    MsgBox "It's there."
  else
    MsgBox "Couldn't find it."
  end if

My mistake (thanks Paul!) - the code that I posted works for a web app but not for a console app. This code should work for a console app:

Dim f As FolderItem = App.ExecutableFile.Parent.Child("TMW-Alpha")

Thank you Jason for this short cut. Much appreciated.

Now a perplexing observation.

I use a Shell within my web app to launch my Console app.
If I add the path to the Console app to the PATH environment variable, then the Console app launches and does its work.

If I launch it via the following Shell command, it does not work as expected.

cd /home/sites/www.example.com/Shared_Documents/TMW-Alpha; pwd; ./PretendParse

When I run this, the Shell correctly indicates (from the pwd command) that it is pointing at the TMW-Alpha folder. And PretendParse runs; however it is unable to “find” its log file, which is in the same folder, and exits with an error which alerts me to the fact.

Note I can remove the pwd command and get the same result.

Any thoughts on why the PATH addition works, but the cd command set does not ?

Here is the function that fails:-

[code] dim log as TextOutputStream
dim fBase, fLog as folderItem
dim d as date

d = new date

fBase = getBaseFolder()
if fBase = nil then
return false
end if

fLog = fBase.child(“ProjectLog.txt”)
if fLog.exists = false then
return false
end if

if fLog.exists = false then
log = TextOutputStream.Create(fLog)
else
log = TextOutputStream.Append(fLog)
end if

if log = nil then

return false

end if

log.WriteLine d.SQLDateTime + " | " + datum

log.Close

return true[/code]

The function getBaseFolder is described above, but reproduced here for ease of understanding:-

Dim f As FolderItem = App.ExecutableFile.Parent.Child("TMW-Alpha")

Regards,
Tony Barry
Sydney, Australia

Are you sure that it’s not finding it? You’ve got three conditions which return False from baseFolder being nil, to the file not existing, to log being Nil.

Observations:

if fLog.exists = false then Return False End If

If the log file doesn’t exist, you return False without creating it.

if fLog.exists = false then log = TextOutputStream.Create(fLog) else log = TextOutputStream.Append(fLog) end if

This section of code will throw an ioexception if there’s an error and not simply return Nil.

Hi Greg,

Yes indeed. A poor showing on my part there.

That was definitely not one of my better efforts.

Thank you for your observations. I shall read over my code again and try to observe where I have been similarly stupid.

Regards,
Tony Barry
Sydney, Australia