Back
Lennie's Weekly Tip: Copying Files in Delphi
22 May 2000 Ref. 24

To copy files from your Delphi application(s), you can use the CopyFile Windows API function.

Declaration:
  function CopyFile(lpExistingFileName: PChar; lpNewFileName: PChar; bFailIfExists: LongBool) : boolean;

Parameters

lpExistingFileName
  Points to a null-terminated string that specifies the name of an existing file (the file to copy).

lpNewFileName
  Points to a null-terminated string that specifies the name of the new file.

bFailIfExists
  Specifies how this operation is to proceed if a file of the same name as that specified by lpNewFileName already exists.

If this parameter is TRUE and the new file already exists, the function fails.
If this parameter is FALSE and the new file already exists, the function overwrites the existing file and succeeds.

This function will return TRUE if successful or FALSE, otherwise.

Example:
========

if (not CopyFile('c:\Test\1.txt', 'c:\Test\2.txt', TRUE)) then
    Messagedlg('Error: CopyFile WinAPI Fails!', mterror, [mbok], 0);
 

In this example the CopyFile-function will copy an existing file ('1.txt') to a new file ('2.txt').

If the last parameter is TRUE and the new file already exists, the function fails with a error message. If this parameter is FALSE and the new file already exists, the function overwrites the existing file and succeeds.

For more information read the Microsoft® Win32® Programmer's Reference available with Delphi.


Back