Lazarus
Programming => Operating Systems => macOS / Mac OS X => Topic started by: han on September 26, 2022, 06:15:15 pm
-
For Windows I can move files to an other directory as follows:
err:=movefile(pchar(filename2),pchar(thepath+'\'+extractfilename(filename2)));
or
err:=renamefile(pchar(filename2),pchar(thepath+'\'+extractfilename(filename2)));//rename is the same as movefile
In Linux I can also movefiles by renamefile
err:=renamefile(pchar(filename2),pchar(thepath+'/'+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
-
(note that rename instead of move only works for moves within one filesystem)
-
(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.
-
For Windows I can move files to an other directory as follows:
err:=movefile(pchar(filename2),pchar(thepath+'\'+extractfilename(filename2)));
or
err:=renamefile(pchar(filename2),pchar(thepath+'\'+extractfilename(filename2)));//rename is the same as movefile
In Linux I can also movefiles by renamefile
err:=renamefile(pchar(filename2),pchar(thepath+'/'+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. :)
-
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.
err:=renamefile(filename2,thepath+'/'+extractfilename(filename2));
-
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).
-
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:
{$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:
{$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}