Recent

Author Topic: best way to move file to directory?  (Read 584 times)

Чебурашка

  • Hero Member
  • *****
  • Posts: 568
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
best way to move file to directory?
« on: December 22, 2023, 12:11:44 pm »
Hello,

if I have the linux command:

Code: Bash  [Select][+][-]
  1. # mv /root/sourcefile /home/user/destinationdirectory
  2.  

where "sourcefile" is a file, and "destinationdirectory" is a existing empty directory

What is best way to do it in fp?

I did not find anything in FileUtil (there is CopyFile but wants filenamesource to filenamedestination, not a destination dir)

Maybe best way is do a FPExec that just runs the shown command?
« Last Edit: December 22, 2023, 12:16:42 pm by Чебурашка »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: best way to move file to directory?
« Reply #1 on: December 22, 2023, 12:56:16 pm »
Code: Pascal  [Select][+][-]
  1. function MyRenameFile(SourceFile, DestinationDir: UnicodeString): Boolean;
  2. begin
  3.   Result := RenameFile(SourceFile, IncludeTrailingPathDelimiter(DestinationDir) + ExtractFileName(SourceFile));
  4. end;
  5.  
  6. procedure TForm1.Button1Click(Sender: TObject);
  7. begin
  8.   MyRenameFile('/root/sourcefile', '/home/user/destinationdirectory');
  9. end;  
Best regards / Pozdrawiam
paweld

Чебурашка

  • Hero Member
  • *****
  • Posts: 568
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: best way to move file to directory?
« Reply #2 on: December 22, 2023, 01:35:22 pm »
  Result := RenameFile(SourceFile, IncludeTrailingPathDelimiter(DestinationDir) + ExtractFileName(SourceFile));

Thank you
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

TRon

  • Hero Member
  • *****
  • Posts: 2435
Re: best way to move file to directory?
« Reply #3 on: December 22, 2023, 01:51:05 pm »
Thank you

Do note the comment in the RenameFile documentation:
Quote
Remark: The implementation of RenameFile relies on the underlying OS's support for renaming/moving a file. Whether or not a file can be renamed across disks or partitions depends entirely on the OS. On unix-like OS-es, the rename function will fail when used across partitions. On Windows, it will work.

Just keep that in mind in case you run into such a situation where a file is not 'moved'.

Чебурашка

  • Hero Member
  • *****
  • Posts: 568
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: best way to move file to directory?
« Reply #4 on: December 22, 2023, 03:49:51 pm »
Thank you

Do note the comment in the RenameFile documentation:
Quote
Remark: The implementation of RenameFile relies on the underlying OS's support for renaming/moving a file. Whether or not a file can be renamed across disks or partitions depends entirely on the OS. On unix-like OS-es, the rename function will fail when used across partitions. On Windows, it will work.

Just keep that in mind in case you run into such a situation where a file is not 'moved'.

Thanks for the detailed remark!
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

 

TinyPortal © 2005-2018