Wiki copymirror

This is a mirror of the wiki at pascal.chat. Editing, history and sign-in are there.

CopyFile

English · Français · Suomi · Русский

Unit: Lazarus fileutil (UTF-8 replacements for FPC RTL code and additional file/directory handling)

pascal
// flags for copy
type
 TCopyFileFlag = (
   cffOverwriteFile,
   cffCreateDestDirectory,
   cffPreserveTime
   );
 TCopyFileFlags = set of TCopyFileFlag;

function CopyFile(const SrcFilename, DestFilename: string): boolean;
function CopyFile(const SrcFilename, DestFilename: string; PreserveTime: boolean): boolean;
function CopyFile(const SrcFilename, DestFilename: string; Flags: TCopyFileFlags=[cffOverwriteFile]): boolean;

Function copyfile copies a source file to a destination file location. Optionally it preserves the file's timestamp.

Function result Returns boolean value True if successful, False if there was an error.

Note: If you want to use this function in command line programs, add a project requirement for LazUtils, which will not pull in the entire LCL

Note: This function cannot be used with wildcards (., etc)

Windows example

pascal
uses 
...
fileutil
...
CopyFile('c:\autoexec.bat','c:\windows\temp\autoexec.bat.backup');

Lazarus example

The following components or functions are used in this example::

pascal

procedure TForm1.Button1Click(Sender: TObject);
var
  ok:boolean;
begin
  ok := false;
  if OpenDialog1.Execute then
    if SaveDialog1.Execute then
      ok := CopyFile(OpenDialog1.FileName, SaveDialog1.FileName);
  if ok then MessageDlg('File '+OpenDialog1.FileName+' successfully copied to '+
      SaveDialog1.FileName,mtInformation,[mbOk],0)
  else MessageDlg('Copying failed',mtWarning,[mbOk],0);
end;

From the Free Pascal wiki, page "CopyFile", as it stood on 2022-01-20 after a change by Paskal, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .