Search code examples
macosdelphifiremonkey

Delphi Berlin 10.1 create file/folder OS X


How to create file/folder on OS X via delphi? I'm trying to use functions

System.SysUtils.FileCreate

And

System.IOUtils.TDirectory.CreateDirectory

Code:

  if FileCreate('/var/folders/bla.txt') = INVALID_HANDLE_VALUE then
 raise Exception.Create('File already exists');

or

 `TDirectory.CreateDirectory('/var/folders/mydirdelete');`

Anyway when i run app i get

Exception EFileNotFoundException in module blprtl240.dylib at 0053AFAF.
The specified file was not found. 

Even if i create folder.

Any advices?


Solution

  • The docs says FileCreate return INVALID_HANDLE_VALUE (-1) indicates that an error occurred which not only mean that file already exists e.g. it could be Permission denied ...etc.

    Regarding CreateDirectory raises an exception if the given Path is invalid or contains invalid characters.

    Try this instead:

    const
      AFilename = '/tmp/folders/bla.txt';
    begin
      if NOT FileExists(AFilename) then begin
        if NOT DirectoryExists(ExtractFilePath(AFilename)) then
          if NOT ForceDirectories(ExtractFilePath(AFilename)) then RaiseLastOSError;
        if (FileCreate(AFilename) = INVALID_HANDLE_VALUE) then RaiseLastOSError;
      end;