Difference between revisions of "CopyFile"
From Lazarus wiki
Jump to navigationJump to searchm |
|||
(10 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{CopyFile}} |
− | Unit: Lazarus | + | [[Unit]]: Lazarus [[fileutil]] ([[UTF-8]] replacements for FPC [[RTL]] code and additional file/directory handling) |
+ | |||
+ | <syntaxhighlight lang="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): boolean; | ||
function CopyFile(const SrcFilename, DestFilename: string; PreserveTime: boolean): boolean; | function CopyFile(const SrcFilename, DestFilename: string; PreserveTime: boolean): boolean; | ||
+ | function CopyFile(const SrcFilename, DestFilename: string; Flags: TCopyFileFlags=[cffOverwriteFile]): boolean; | ||
</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. |
+ | |||
+ | '''Function result''' | ||
+ | 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]]}} | ||
− | + | {{Note|This function can'''not''' be used with wildcards (*.*, etc)}} | |
− | <syntaxhighlight> | + | |
+ | == Windows example == | ||
+ | <syntaxhighlight lang="pascal"> | ||
uses | uses | ||
... | ... | ||
Line 18: | Line 35: | ||
CopyFile('c:\autoexec.bat','c:\windows\temp\autoexec.bat.backup'); | CopyFile('c:\autoexec.bat','c:\windows\temp\autoexec.bat.backup'); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | [[ | + | |
− | [[ | + | |
− | [[ | + | == 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; | ||
+ | |||
+ | </syntaxhighlight> |
Latest revision as of 12:21, 20 January 2022
│
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: 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
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;