Recent

Author Topic: mac - If FileExists(fileName) always returns false  (Read 1841 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: 596
  • 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: 4597
  • 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: 1280
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 the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

TRon

  • Hero Member
  • *****
  • Posts: 3621
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
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

madref

  • Hero Member
  • *****
  • Posts: 1078
  • ..... 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.

Main Platform:
--------------
Mac OS X Sonoma 14.7
Lazarus 3.99 (Lazarus 3.99 (rev main_3_99-2668-g6b352d830e) FPC 3.3.1 x86_64-darwin-cocoa)

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

JdeHaan

  • Full Member
  • ***
  • Posts: 150
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: 16145
  • 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'
If I smell bad code it usually is bad code and that includes my own code.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8007
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.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
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