Copy files in Delphi

Function : CopyFile(lpExistingFileName, lpNewFileName: PChar; bFailIfExists: Boolean): Boolean;

In the Delphi program, to copy the file we can use the function : CopyFile(lpExistingFileName, lpNewFileName: PChar; bFailIfExists: Boolean): Boolean;

lpExistingFileName   : The file will be copied
lpNewFileName        : Destination file (complete with path)
bFailIfExists             : action to be performed, when filled with False, it will overwrite

Example:
Create a Form with an Edit, 1 DriveComboBox, 1 DirectoryListBox1, 2 Button, and a CheckBox, do double-clicking the Button1 component and button2, write the following program listing :

procedure TF_CopyFile.Button1Click(Sender: TObject);
begin
 if OpenDialog1.Execute then Edit1.Text:=OpenDialog1.FileName;
end;

procedure TF_CopyFile.Button2Click(Sender: TObject);
begin
 if copyfile(pchar(Edit1.Text),
pchar(DirectoryListBox1.Directory+'\'+extractfilename(Edit1.Text)),
not CheckBox1.Checked)=true
 then showmessage('Managed to copy files')
 else showmessage('Failed to copy files');
end;

No comments:

Post a Comment