Difference between revisions of "CopyFile"

From Lazarus wiki
Jump to navigationJump to search
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 +
{{CopyFile}}
  
Unit: Lazarus [[fileutil]] (UTF-8 replacements for FPC RTL code and additional file/directory handling)
+
[[Unit]]: Lazarus [[fileutil]] ([[UTF-8]] replacements for FPC [[RTL]] code and additional file/directory handling)
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
// flags for copy
 
// flags for copy
 
type
 
type
Line 17: Line 18:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
'''copyfile''' copies a source file to a destination file location. Optionally it preserves the file's timestamp.  
+
[[Function]] '''copyfile''' copies a source file to a destination file location. Optionally it preserves the file's timestamp.  
  
Example:
+
'''Function result'''
<syntaxhighlight>
+
Returns [[Boolean|boolean]] value [[True]] if successful, [[False]] if there was an error.
 +
 
 +
{{Note|If you want to use this function in [[Command-line interface|command line]] programs, add a project requirement for [[LazUtils]], which will not pull in the entire [[LCL]]}}
 +
 
 +
== Windows example ==
 +
<syntaxhighlight lang="pascal">
 
uses  
 
uses  
 
...
 
...
Line 28: Line 34:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
'''Function result'''
 
Returns 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}}
+
== Lazarus example ==
 +
 
 +
 
 +
The following components or functions are used in this example::
 +
* [[TOpenDialog]] [[Image:topendialog.png]]
 +
* [[TSaveDialog]] [[Image:tsavedialog.png]]
 +
* [[Dialog_Examples#MessageDlg|MessageDlg]]
 +
 
 +
 
 +
<syntaxhighlight lang="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;
  
[[category:Lazarus]]
+
</syntaxhighlight>
[[category:fileutil]]
 
[[Category:Code]]
 

Revision as of 20:21, 7 September 2019

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.

Light bulb  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;