Recent

Author Topic: Filepath  (Read 1047 times)

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 69
Filepath
« on: August 08, 2024, 10:23:55 am »
Hi All

Still finding my feet in the Pascal/Freebasic differences....

In the TDBF control, there is a place you need to specify the filepath.
But how do I set that to be the app path when the project deploys?

In Basic you would have app.path or me.path, but I never used visual data controls.
I find if i select a path in that control it will look for the DBF in that set path and cause trouble if it cannot find it.

It has to be a simple setting.

-Peter

Thaddy

  • Hero Member
  • *****
  • Posts: 15648
  • Censorship about opinions does not belong here.
Re: Filepath
« Reply #1 on: August 08, 2024, 10:31:21 am »
ExtractFilePath(Application.ExeName);
If I smell bad code it usually is bad code and that includes my own code.

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 69
Re: Filepath
« Reply #2 on: August 08, 2024, 10:35:53 am »
Hi

I see omitting the filepath in the control does that.

Regards, Peter

Thaddy

  • Hero Member
  • *****
  • Posts: 15648
  • Censorship about opinions does not belong here.
Re: Filepath
« Reply #3 on: August 08, 2024, 10:39:47 am »
No it omits the exename and returns the path.
If you do not use Application the code would be e
ExtractFilePath(ParamStr(0));
The latter has issues on Macs, but works on Windows and Linux.
Code: Pascal  [Select][+][-]
  1. uses sysutils;
  2. begin
  3.   writeln(ExtractFilePath(ParamStr(0)));
  4. end.
« Last Edit: August 08, 2024, 10:43:23 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 69
Re: Filepath
« Reply #4 on: August 08, 2024, 03:24:00 pm »
HI

Thank you for the response.
Also, apologies for the newby questions.

I just need to check 100% for clarity.
The Tdbf control has two settings:

FILEPATH
FILEPATHFULL

It automatically fill those two fields if i select the TABLENAME.
It then sets the current path to e.g. c:\Lazprojects\SMStool\Data.

If I leave both of them like that the project works fine, but if i should copy it onto a stick it will still go look for the data in C:\Lazprojects\SMStool\Data and crash if that folder does not exist on the target pc.

Where do i enter:
Code: Pascal  [Select][+][-]
  1. ExtractFilePath(Application.ExeName);
?

Its but the first week on Pascal. Lots new to learn.

Truly appreciate the help.

-Peter

wp

  • Hero Member
  • *****
  • Posts: 12354
Re: Filepath
« Reply #5 on: August 08, 2024, 03:44:36 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Dbf1.FilePathFull := Application.Location;  // or: ExtractFilePath(Application.ExeName);
  4.   Dbf1.TableName := 'name_of_the_dbf_file.dbf';   // or set it in the Object Inspector.
  5. end;

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 69
Re: Filepath
« Reply #6 on: August 08, 2024, 03:51:49 pm »
HI

Thank you. That makes more sense.
Like we did it over at Freebasic.

Its truly a great piece of work you fellows did here.

-Peter

Bart

  • Hero Member
  • *****
  • Posts: 5356
    • Bart en Mariska's Webstek
Re: Filepath
« Reply #7 on: August 08, 2024, 04:11:40 pm »
Having the database in the folder where the executable is, is not recommended practice.
Most times that folder is read-only.

This of course may very well not be the case in your scenario, in which case: don't worry.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 15648
  • Censorship about opinions does not belong here.
Re: Filepath
« Reply #8 on: August 08, 2024, 05:50:02 pm »
What Bart means is to set those paths in code, not through the object inspector.
I am also a bit bemused about your choice for TDBF? That is real legacy and will hinder you later. Better to use something like SQLite. Because that is also as simple as TDBF but supports sql and that will make your code easier to adapt to more powerful database engines in the future.
DBF format is only used if it is absolutely necessary to support old databases and code.
(not that I have anything against dbf format, it payed for part of my pension, but it is not a wise choice nowadays.)
« Last Edit: August 08, 2024, 05:54:07 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Bart

  • Hero Member
  • *****
  • Posts: 5356
    • Bart en Mariska's Webstek
Re: Filepath
« Reply #9 on: August 08, 2024, 09:11:27 pm »
What Bart means is to set those paths in code, not through the object inspector.

No, that is not what I mean.
You should not store a database in the programs location, unless this is read-only (or only to be changed by administrators).
By default the location of programs (i.e. C:\Prgram Files\Path\To\Executable) is read-only for users.
The same goes for *nix systems.

Bart

 

TinyPortal © 2005-2018