Movefile for Darwin/macOS lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- han · 2022-09-26 16:15 For Windows I can move files to an other directory as follows: Code: [Select]            err:=movefile(pchar(filename2),pchar(thepath +'\'+extractfilename(filename2))); or             err:=renamefile(pchar(filename2),pchar(thepath+'\'+extract filename(filename2)));//rename is the same as movefile In Linux I can also movefiles by renamefile Code: [Select]            err:=renamefile(pchar(filename2),pchar(thepa th+'/'+extractfilename(filename2)));//rename is the same as movefile But the renamefile solution to move a file doesn't work for macOS. How can I move a file in macOS? Han ---------------------------------------------------------------------- marcov · 2022-09-26 16:24 (note that rename instead of move only works for moves within one filesystem) ---------------------------------------------------------------------- han · 2022-09-26 16:39 Quote from: marcov on September 26, 2022, 06:24:17 pm(note that rename instead of move only works for moves within one filesystem) For Windows I can rename files from my harddisk to an USB stick. Is there any other limitation? Probably for macOS I can execute a command-line but hopefully there is a more elegant solution. ---------------------------------------------------------------------- dsiders · 2022-09-26 16:42 Quote from: han on September 26, 2022, 06:15:15 pmFor Windows I can move files to an other directory as follows: Code: [Select]            err:=movefile(pchar(filename2),pchar(thepath +'\'+extractfilename(filename2))); or             err:=renamefile(pchar(filename2),pchar(thepath+'\'+extract filename(filename2)));//rename is the same as movefile In Linux I can also movefiles by renamefile Code: [Select]            err:=renamefile(pchar(filename2),pchar(thepa th+'/'+extractfilename(filename2)));//rename is the same as movefile But the renamefile solution to move a file doesn't work for macOS. How can I move a file in macOS? Han If you look at the source, the arguments are defined as RawByteString in the macOS RTL. Think different. ---------------------------------------------------------------------- han · 2022-09-26 17:24 Think different? That is a Mac slogan. Thinking out of the box maybe. The pchar() should not be there for renamefile, only for movefile. But it still doesn't work for macOS. I don't see a solution. Code: [Select]err:=renamefile(filename2,thepath+'/'+extractfilename(fi lename2)); ---------------------------------------------------------------------- Jonas Maebe · 2022-09-26 20:35 See the description of https://www.freepascal.org/docs- html/rtl/sysutils/renamefile.html . It is explicitly documented as not supporting moving a file across different disks/partitions on Unix platforms (like Darwin/macOS). In that case, you have to copy the file and then delete the original one. If you want, you can use https://wiki.freepascal.org/CopyFile from Lazarus for this (I don't think its fileutil unit depends on the LCL). ---------------------------------------------------------------------- han · 2022-09-27 07:44 Okay now it is all clear.  Contradicting with previous finding, in macOS renamefile works in one partition. I missed then due to an other fault I did not notice due to a problem with the debugger. The copyfile flag cffPreserveTime makes an elegant solution possible for more then one partition: Movefile for more then one partition: Code: [Select]  {$ifdef mswindows}   // succ:=movefile(pchar(filename2),pchar(thepath+'\'+extractfilename (filename2)));     succ:=renamefile(filename2,thepath+'\'+extractfilename(filename2)); //rename works the same as movefile   {$else} {Linux, Darwin}   succ:=copyfile(filename2,thepath+'/'+extractfilename(filename2), [cffPreserveTime]); //works for more then one partition.   if succ then    succ:=deletefile(filename2);   {$endif} For Windows I don't see a difference between movefile and renamefile so you can use both. Movefile for a single partition: Code: [Select]  {$ifdef mswindows}   // succ:=movefile(pchar(filename2),pchar(thepath+'\'+extractfilename (filename2)));     succ:=renamefile(filename2,thepath+'\'+extractfilename(filename2))   {$else} {Linux, Darwin}    succ:=renamefile(filename2,thepath+'/'+extractfilename(filename2));   {$endif}