Recent

Author Topic: Find A file Relative to where the .exe is ...  (Read 6738 times)

Berklesse

  • New Member
  • *
  • Posts: 16
Find A file Relative to where the .exe is ...
« on: July 25, 2017, 02:02:10 am »
Hi ,
How can i read from a file or write to a file in Free Pascal , with a relative link to where the .exe is ?
Thanks

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Find A file Relative to where the .exe is ...
« Reply #1 on: July 25, 2017, 02:17:44 am »
Usually by providing a relative path/filename from your exe.

e.g. TFileStream.Create('subdir1\subdir2\filename.txt', fmOpenRead)

Is it not working for your platform ?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Find A file Relative to where the .exe is ...
« Reply #2 on: July 25, 2017, 02:21:32 am »
Hi ,
How can i read from a file or write to a file in Free Pascal , with a relative link to where the .exe is ?
Thanks
try this one.
Code: Pascal  [Select][+][-]
  1.   ExpandFileName(IncludeTrailingPathDelimiter(ExtractFileDir(Application.ExeName))+Relativepath);
  2.  

Usually by providing a relative path/filename from your exe.

e.g. TFileStream.Create('subdir1\subdir2\filename.txt', fmOpenRead)

Is it not working for your platform ?
that is relative to currentdir which might be the application dir and might not, since currentdir is a systemwide variable and can be changed at any time by any process.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Find A file Relative to where the .exe is ...
« Reply #3 on: July 25, 2017, 02:29:05 am »
that is relative to currentdir which might be the application dir and might not, since currentdir is a systemwide variable and can be changed at any time by any process.
Sorry, you are right. thank you for the correction.

As an additional note for TS: the directory/path must already exist. It will not be automatically generated for you.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Find A file Relative to where the .exe is ...
« Reply #4 on: July 25, 2017, 02:32:48 am »
that is relative to currentdir which might be the application dir and might not, since currentdir is a systemwide variable and can be changed at any time by any process.
Sorry, you are right. thank you for the correction.

As an additional note for TS: the directory/path must already exist. It will not be automatically generated for you.
I'm glad I could be of service.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Find A file Relative to where the .exe is ...
« Reply #5 on: July 25, 2017, 04:58:46 pm »
I use 'ProgramDirectory' + filename  as in :
Code: Pascal  [Select][+][-]
  1.   system.assign(SuppProg,ProgramDirectory +'SupportProg.DTA');
  2.   system.assign(Location,ProgramDirectory +'Location.DTA');

which (once written) creates files in the same directory as the .EXE but I'm sure adding  'subdirectory name'  would also be effective   -   taking care of the path delimiter of course.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Find A file Relative to where the .exe is ...
« Reply #6 on: July 25, 2017, 05:20:11 pm »
Code: Pascal  [Select][+][-]
  1.   ExpandFileName(IncludeTrailingPathDelimiter(ExtractFileDir(Application.ExeName))+Relativepath);
  2.  

A shorter version would be
Code: Pascal  [Select][+][-]
  1. var
  2.   s: String;
  3. ...
  4.   s := ExpandFilename(Application.Location + RelativePath);

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Find A file Relative to where the .exe is ...
« Reply #7 on: July 26, 2017, 08:58:58 pm »
Why use ExpandFileName() at all?  It is used for converting a relative path to an absolute path, but since the value being passed to it is already an absolute path to begin with, I would expect it to be a no-op, so just omit it:

Code: [Select]
// these are compatible with Delphi and FreePascal...
s := IncludeTrailingPathDelimiter(ExtractFileDir(Application.ExeName)) + 'subdir1\subdir2\filename.txt';
s := IncludeTrailingPathDelimiter(ExtractFileDir(ParamStr(0))) + 'subdir1\subdir2\filename.txt';

Or:

Code: [Select]
// these are also compatible with Delphi and FreePascal...
s := ExtractFilePath(Application.ExeName) + 'subdir1\subdir2\filename.txt';
s := ExtractFilePath(ParamStr(0)) + 'subdir1\subdir2\filename.txt';

Or:

Code: [Select]
// I wish Delphi had Application.Location!
s := Application.Location + 'subdir1\subdir2\filename.txt';
« Last Edit: July 26, 2017, 09:04:10 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Find A file Relative to where the .exe is ...
« Reply #8 on: July 26, 2017, 09:38:46 pm »
Why use ExpandFileName() at all?  It is used for converting a relative path to an absolute path, but since the value being passed to it is already an absolute path to begin with, I would expect it to be a no-op, so just omit it:
I'm using to remove any and all .\ ..\ sequence of pairs the path might have other than that I guess you are right.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018