Recent

Author Topic: mac - If FileExists(fileName) always returns false  (Read 1499 times)

CaptBenB

  • Newbie
  • Posts: 5
mac - If FileExists(fileName) always returns false
« on: June 14, 2024, 10:38:12 pm »
newbie question... I have looked all day yesterday and today... but can't seem to figure this out.  any help is appreciated.

procedure TForm1.FormActivate(Sender: TObject);
var
  fileName: string;
  answer: boolean;
begin

  fileName := 'test';  // -- this file exists
  answer:=FileExists(fileName);

  If answer=True Then  showmessage('True');
  If answer=False Then  showmessage('False');
end


BSaidus

  • Hero Member
  • *****
  • Posts: 565
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: mac - If FileExists(fileName) always returns false
« Reply #1 on: June 14, 2024, 10:46:28 pm »
try to use the full path to the file.
Code: Pascal  [Select][+][-]
  1.   filename = IncludeTrailingPathDelimiter(GetCurrentDir()) + 'test';
  2.   answer:=FileExists(fileName);
  3.  
  4.   If answer=True Then  showmessage('True');
  5.   If answer=False Then  showmessage('False');
  6.  
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

CaptBenB

  • Newbie
  • Posts: 5
Re: mac - If FileExists(fileName) always returns false
« Reply #2 on: June 14, 2024, 10:50:21 pm »
Good idea... but still the same result.

lainz

  • Hero Member
  • *****
  • Posts: 4569
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: mac - If FileExists(fileName) always returns false
« Reply #3 on: June 15, 2024, 12:06:38 am »
Remember that's macOS, and files are considered to be inside the app bundle by default.

To access external files you must include the full path.

dsiders

  • Hero Member
  • *****
  • Posts: 1167
Re: mac - If FileExists(fileName) always returns false
« Reply #4 on: June 15, 2024, 12:24:15 am »
Remember that's macOS, and files are considered to be inside the app bundle by default.

To access external files you must include the full path.

I would absolute to that requirement. There's also permissions to consider (it is Unix after all). Case. Phase of the moon. And Tim Cook's shoe size.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

TRon

  • Hero Member
  • *****
  • Posts: 2836
Re: mac - If FileExists(fileName) always returns false
« Reply #5 on: June 15, 2024, 12:26:00 am »
And Tim Cook's shoe size.
Which happens to be ... 42 ?  :D

madref

  • Hero Member
  • *****
  • Posts: 1026
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: mac - If FileExists(fileName) always returns false
« Reply #6 on: June 15, 2024, 12:54:23 am »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-2145-g5d9f393aae) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Sonoma 14.5

JdeHaan

  • Full Member
  • ***
  • Posts: 133
Re: mac - If FileExists(fileName) always returns false
« Reply #7 on: June 15, 2024, 12:11:16 pm »
Hi,

On MacOS you need to use the full file path, including the name and file extension, e.g.:

/Users/YourName/Documents/Programming/Projects/new/test.pas

In the Finder window, select the respective file. At the bottom of the window you'll find the breadcrumb path. Right click on the file name and click "Copy 'test.xyz' as Pathname".
Paste the copied path into your code.

see attached example of the breadcrumb.

Thaddy

  • Hero Member
  • *****
  • Posts: 15120
  • Censorship about opinions does not belong here.
Re: mac - If FileExists(fileName) always returns false
« Reply #8 on: June 15, 2024, 02:20:23 pm »
It does not have to be a full path, on mac it also works with relative paths (at least it used to) './test'
Of course the national anthem of the U.S.A. was written by Jimi Hendrix, didn't you know that?

MarkMLl

  • Hero Member
  • *****
  • Posts: 7037
Re: mac - If FileExists(fileName) always returns false
« Reply #9 on: June 15, 2024, 03:10:21 pm »
This one ain't my fight since I'm not a Mac user, but I'd point out that

* OP hasn't told us whether "test" is a file or a directory/folder.

* OP hasn't told us what version of FPC he's using.

* A year or so ago the behaviour of FileExists() was changed to explicitly ignore directories in all cases.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

CaptBenB

  • Newbie
  • Posts: 5
Re: mac - If FileExists(fileName) always returns false
« Reply #10 on: June 16, 2024, 03:15:15 am »
Thank you for all of your responses.  I'll look into them as I get time.  I switched to windows and my code works ok.  So it's probably some permissions and path issues with mac.  Thanks again! and fwiw it's, Lazarus/Freepascal Version 3.2, Mac OS Intel Sonoma 14.5 and test is a file.

 

TinyPortal © 2005-2018