Relative Paths...

Post

Posted
Rating:
#1 (In Topic #1363)
Regular
chrisRoald is in the usergroup ‘Regular’
Hi, do relative paths only work with the executable, or in the development interface as well?
I'm trying to save a new file to the relative path:  ../.public/myfilename.json  but get the 'Access forbidden' error when debugging!

I've checked the Permissions for the .public folder and set them to: create and delete; read/write across owner,group and user,
then reloaded the project, but all to no avail.  The Encode works fine, btw.  Any ideas?

my code is as straight-forward as this

Code (gambas)

  1. fileSpec = "../.public/myfilename.json"
  2. File.Save(fileSpec, JSON.Encode(saveCollection))

Thanks,
C.
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
NEVER use relative paths for write.

use Application.Path &/ ".public/myfilename.json"

/cat/path - Gambas Documentation
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
Also "../" is not the same thing as the bash "../".
Where did you expect that relative path to point to? It's not the directory above the one we are running in, it means "the component above this one" as in "the component where this was called from".
If you truly want the directory above the current run directory then modify BruceS's code to:

Code (gambas)

  1. File.Dir(Application.Path)&/ ".public/myfilename.json"
Then again, I don't think that's what you are after.
b

Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
chrisRoald is in the usergroup ‘Regular’
…Thanks for these solutions.
This line - "While running your project in the IDE, project files can be modified by using absolute paths. But do not do that! As soon as your project is run as an executable, these absolute paths do not exist anymore." - under Relative Paths in the documentation /cat/path - Gambas Documentation is a bit misleading I feel. It suggested to me that 'relative paths' should therefore be used instead, and that "../" - specified in a previous statement - is the syntax to use.
The whole page could be re-written to be less ambiguous/confusing, with the 'application.path' solutions included as examples perhaps!
Regards,
C.
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’

chrisRoald said

The whole page could be re-written to be less ambiguous/confusing, with the 'application.path' solutions included as examples perhaps!

Go for it Chris , it's a community created wiki.

If you want changes you are free to change it.

Everyone else is busy ;) lol
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
I didn't fancy re-writing the whole page like you suggested but i changed that paragraph. (it will show by tomorrow)

You were right it was a bit misleading,
I believe all that part is trying to say is that you can modify files in the project with their absolute path but then when running the program as an executable, unless you have hit "Make executable" and re-made the archive, they (the modified files) do not yet exist in the archive.  but when using the IDE it uses the modified files each time you hit run.

So i changed it to this, hopefully makes more sense…

wiki said

While running your project in the IDE, project files can be modified by using absolute paths and will be refreshed with each run. But not if your project is run as an executable. It will then be using archived files not the project folder ones, you will modify the files at their absolute path but they will not add to the application executable unless you re-make it.

If you think it should be worded differently ,,,.      then word it differently :)  :lol:  :D
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 How 'bout:

Dont write code that tries to modify files inside your project.

Seems to cover it without referring to any type of pathing.

b

Online now: No Back to the top

Post

Posted
Rating:
#8
Regular
chrisRoald is in the usergroup ‘Regular’
…Hi, the only bit I'm not sure about from your above responses is: does the executable support writing to a .public folder outside of 'the archive'? If that is IDE only, then where do you normally save/modify exe-operational files in your hard-drive? ~ for example:-
 The user is entering data that I want saved, field by field, to prevent data loss through power-failure or program-crash! The final location/path of their data (json files) hasn't been decided yet by the user.  My plan was to save in the .public directory.  Does such a 'location' exist in executableLand?
 
Sorry to appear dim, but terms such as 'project', 'ide' and 'archive' I'm having to try and decide precisely what they mean/imply gambasWise as I go along.
I think it's too early to be amending open documentation as I'm still on a huge learningCurve; but I'm enjoying the journey - mostly  :D
Regards,
C.
Online now: No Back to the top

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘Guru’
Well yes you can write to anything outside of the executable but the executable itself is a self contained archive made when you hit "Make executable"
the executable (archive, same thing) it is completely read only.

But generally you do not want to use your project folder.  you are free to use it if you like, it's just bad practice as moving/renaming it will mess things up in your code paths. plus there can be other issues.

For your case I would maybe use a folder in ~/.config/  or even just simply use Setting.class
Ie.
As the user types data in the fields just save some settings…

Code (gambas)

  1.  
  2. Public Sub StartNewEntry()
  3.  
  4.   Settings.Clear("CurrentEntry") ' clear CurrentEntry settings when starting a new form.
  5.  
  6.  
  7. Public Sub TextBoxName_Change()
  8.  
  9.   Settings["CurrentEntry/Name"] = Last.Text  ' save current Name text setting
  10.   Settings.Save  ' force to save settings file now
  11.  
  12.  
  13. ' do the same for all other entry fields
  14.  

Then if crash when program is run the Settings["CurrentEntry/xxx"] fields will be available to restore.

Code (gambas)

  1.  
  2. Public Sub LoadLastConfig()
  3.  
  4.   TextBoxName.Text = Settings["CurrentEntry/Name", ""]
  5.  ' and so on for the rest of the fields
  6.  
  7.  
Online now: No Back to the top

Post

Posted
Rating:
#10
Guru
BruceSteers is in the usergroup ‘Guru’
And don't feel dim.  there is a lot to understand about how a gambas project/executable runs.

You'll soon figure it all out :)
Online now: No Back to the top

Post

Posted
Rating:
#11
Guru
BruceSteers is in the usergroup ‘Guru’
Just FYI (to give you something else to consider ;) and to help you see some difference between project and executable)

You do not actually need to make a gambas executable at all and can just run the project from the folder.

There are 2 ways to run a gambas project/program..

1. As an executable.
  This it's typical use.  Use the IDE to "Make an executable"  then you have a "projectName.gambas" file that is actually just an archive.
  All the projects files (excluding ALL .hidden files and folders except .public) are added to the archive and can be accessed to read.
  (for certain usages a file may not be accessible and you have to move it out of the archive to a temp folder first)

But for the most part the .gambas executable file can then be placed anywhere outside of the project dir and run from there and have all it's project files in it's internal relative paths.
./ for project root
../ for components (like gb.form) to see their own root not the projects.


2. As a project.
  Just use gbx3 /path/to/project-dir
  This way your actual "project" is run, not an "executable"
  It is basically the same as loading the IDE and hitting "Run" but without the IDE and debugging
  So you can access .hidden folders that would otherwise not be added to the executable.
  Any changes to project files would not need to be re-added to any executable, it's all live.


Hope that helps
Online now: No Back to the top
1 guest and 0 members have just viewed this.