Sign in Join
Wiki copytemporary

CopyFile: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: CopyFile

OldNewLine
1+[[Unit]]: Lazarus [[fileutil]] ([[UTF-8]] replacements for FPC [[RTL]] code and additional file/directory handling)
2+
3+``` pascal
4+// flags for copy
5+type
6+ TCopyFileFlag = (
7+ cffOverwriteFile,
8+ cffCreateDestDirectory,
9+ cffPreserveTime
10+ );
11+ TCopyFileFlags = set of TCopyFileFlag;
12+
13+function CopyFile(const SrcFilename, DestFilename: string): boolean;
14+function CopyFile(const SrcFilename, DestFilename: string; PreserveTime: boolean): boolean;
15+function CopyFile(const SrcFilename, DestFilename: string; Flags: TCopyFileFlags=[cffOverwriteFile]): boolean;
16+```
17+
18+[[Function]] **copyfile** copies a source file to a destination file location. Optionally it preserves the file's timestamp.
19+
20+**Function result** Returns [[Boolean|boolean]] value [[True]] if successful, [[False]] if there was an error.
21+
22+> **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]]
23+
24+> **Note:** This function can**not** be used with wildcards (*.*, etc)
25+
26+## Windows example
27+
28+``` pascal
29+uses
30+...
31+fileutil
32+...
33+CopyFile('c:\autoexec.bat','c:\windows\temp\autoexec.bat.backup');
34+```
35+
36+## Lazarus example
37+
38+The following components or functions are used in this example::
39+
40+- [[TOpenDialog]] ![](/wiki-images/9/90/Topendialog.png)
41+- [[TSaveDialog]] ![](/wiki-images/e/e1/Tsavedialog.png)
42+- [[Dialog Examples|MessageDlg]]
43+
44+``` pascal
45+
46+procedure TForm1.Button1Click(Sender: TObject);
47+var
48+ ok:boolean;
49+begin
50+ ok := false;
51+ if OpenDialog1.Execute then
52+ if SaveDialog1.Execute then
53+ ok := CopyFile(OpenDialog1.FileName, SaveDialog1.FileName);
54+ if ok then MessageDlg('File '+OpenDialog1.FileName+' successfully copied to '+
55+ SaveDialog1.FileName,mtInformation,[mbOk],0)
56+ else MessageDlg('Copying failed',mtWarning,[mbOk],0);
57+end;
58+```
59+
60+---
61+*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](/wiki-contact).*

Back to history