Recent

Author Topic: If FileExists(  (Read 829 times)

lgrfbs

  • Jr. Member
  • **
  • Posts: 80
    • My CV page (On Swedish only)
If FileExists(
« on: June 12, 2026, 04:56:15 am »
Hi, I’ve been away from the forum for a while and apparently missed a few things.
I’m stuck on how to check if a file exists.
I think you need some code to handle the “\” character as well.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormPaint(Sender: TObject);
  2. Var
  3. Index:Byte;
  4. T: String;
  5.  
  6. begin
  7.  if ParamCount>1 then Begin
  8.     //https://forum.lazarus.freepascal.org/index.php?topic=15715.0
  9.     T:='';
  10.     TabSheet2.Enabled:=True; //Debug
  11.     For Index:=1 to ParamCount do
  12.     Begin
  13.      T:=T+' '+ParamStr(Index);
  14.     End;
  15.     LabeledEdit1.Text:=T; //ParamStr(1);
  16.  
  17.     Label9.Caption:=T+'\NC_clean.ini'; //IntToStr(ParamCount);
  18.     If FileExists(T+'\NC_clean.ini') Then
  19.     Begin
  20.      ShowMessage('All is well, I seem to exist.'); //For test
  21.      //TabSheet2.Enabled:=True;
  22.     End;
  23.  end;
  24. end;
  25.  


I hope this post includes all the necessary information.
So what should I look for to get the program code to “recognize” that the file exists?


OS : Win 10 64bit Home * Win 7 64bit Professional
Lazarus 1.8.4 r57972 FPC 3.0.4 i386-win32-win32/win64
Delphi 7.0 (Build 4.453)

ASerge

  • Hero Member
  • *****
  • Posts: 2510
Re: If FileExists(
« Reply #1 on: June 12, 2026, 05:35:41 am »
I hope this post includes all the necessary information.
I didn't understand what you want to say and what FileExists has to do with it.

lgrfbs

  • Jr. Member
  • **
  • Posts: 80
    • My CV page (On Swedish only)
Re: If FileExists(
« Reply #2 on: June 12, 2026, 06:09:35 am »
Sorry for the confusion.
I wanted to find out the best way to check a path.
OS : Win 10 64bit Home * Win 7 64bit Professional
Lazarus 1.8.4 r57972 FPC 3.0.4 i386-win32-win32/win64
Delphi 7.0 (Build 4.453)

Xenno

  • Full Member
  • ***
  • Posts: 132
    • BS Programs
Re: If FileExists(
« Reply #3 on: June 12, 2026, 07:16:00 am »
Use IncludeTrailingPathDelimiter which takes care to add path delimiter when needed.

Code: Pascal  [Select][+][-]
  1. FullPath := IncludeTrailingPathDelimiter(T) + 'NC_clean.ini';
Lazarus 4.6, Windows 10, https://www.youtube.com/@bsprograms
If I want to share, I give. If I want money, I sell. I don't set traps.

hukka

  • Jr. Member
  • **
  • Posts: 63
    • Github
Re: If FileExists(
« Reply #4 on: June 12, 2026, 09:16:49 am »
Your code is adding a space to the beginning of the path.

lgrfbs

  • Jr. Member
  • **
  • Posts: 80
    • My CV page (On Swedish only)
Re: If FileExists(
« Reply #5 on: June 12, 2026, 10:45:03 am »
It's working now.
I am deeply grateful for the help I've received.
THANK YOU, everyone
OS : Win 10 64bit Home * Win 7 64bit Professional
Lazarus 1.8.4 r57972 FPC 3.0.4 i386-win32-win32/win64
Delphi 7.0 (Build 4.453)

Bart

  • Hero Member
  • *****
  • Posts: 5744
    • Bart en Mariska's Webstek
Re: If FileExists(
« Reply #6 on: June 12, 2026, 02:18:50 pm »
Use IncludeTrailingPathDelimiter which takes care to add path delimiter when needed.

Code: Pascal  [Select][+][-]
  1. FullPath := IncludeTrailingPathDelimiter(T) + 'NC_clean.ini';

Notice that this is wrong if T is in the form of DriveLetter + Colon ('C:'), since IncludeTrailingPathDelimiter will append the '\', which most likely is not what you want then.

IIRC then AppendPathDelim (somewhere in LazUtils) caters for that.

I know it's an edge case.

Bart

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1598
    • Lebeau Software
Re: If FileExists(
« Reply #7 on: June 12, 2026, 05:47:02 pm »
Notice that this is wrong if T is in the form of DriveLetter + Colon ('C:'), since IncludeTrailingPathDelimiter will append the '\', which most likely is not what you want then.

Why? If a user wants to refer to a file on 'C:' then they likely really mean 'C:\<file>' rather than 'C:\<cwd>\<file>'. Using 'C:' without a slash to refer to C's current working directory is a lost art on modern generations of users.

IIRC then AppendPathDelim (somewhere in LazUtils) caters for that.

Not according to this documentation:

Quote
Windows

Please note that a value like 'C:' in Path results in 'C:\' in the return value. AppendPathDelim does not access the disk device to determine the per-device current directory; it simply appends the trailing path delimiter to the return value.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Bart

  • Hero Member
  • *****
  • Posts: 5744
    • Bart en Mariska's Webstek
Re: If FileExists(
« Reply #8 on: June 12, 2026, 10:39:03 pm »
Why? If a user wants to refer to a file on 'C:' then they likely really mean 'C:\<file>' rather than 'C:\<cwd>\<file>'. Using 'C:' without a slash to refer to C's current working directory is a lost art on modern generations of users.

I disagree: 'C:Foo' in almost every case will not be the same as C:\Foo.
(I'm from the DOS era).

IIRC then AppendPathDelim (somewhere in LazUtils) caters for that.
Not according to this documentation:

My bad.
Was too lazy to actually check the source code.

Bart

PascalDragon

  • Hero Member
  • *****
  • Posts: 6403
  • Compiler Developer
Re: If FileExists(
« Reply #9 on: June 13, 2026, 03:24:46 pm »
Why? If a user wants to refer to a file on 'C:' then they likely really mean 'C:\<file>' rather than 'C:\<cwd>\<file>'. Using 'C:' without a slash to refer to C's current working directory is a lost art on modern generations of users.

I disagree: 'C:Foo' in almost every case will not be the same as C:\Foo.
(I'm from the DOS era).

But it's true that many (new) users don't know about that functionality. Or drive absolute paths (\some\path). Even VS 2022 doesn't allow entering them for files contained in the project anymore while past versions did (though it at least keeps them if adjusted in the project file).

Bart

  • Hero Member
  • *****
  • Posts: 5744
    • Bart en Mariska's Webstek
Re: If FileExists(
« Reply #10 on: June 13, 2026, 05:57:26 pm »
I'm getting way too ancient  :)

Bart

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1598
    • Lebeau Software
Re: If FileExists(
« Reply #11 on: June 15, 2026, 07:58:06 pm »
I disagree: 'C:Foo' in almost every case will not be the same as C:\Foo.
(I'm from the DOS era).

Yes, that is how the OS actually handles it under the hood. But, I was referring to it from a user perspective. Modern users don't operate at that level anymore, and most UIs don't work that way, either. In fact, a common advice given in forums is to never rely on relative paths anymore when opening files, always use absolute paths only. So, opening a file using 'C:\Foo' would be far more common than using 'C:Foo'.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018