CopyFile

From Free Pascal wiki

English (en) suomi (fi) français (fr) русский (ru)

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

// 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-icon.png

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

Windows example

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


Lazarus example

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


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;