Re: How to Copy/Move/Delete files


Posted by webmaster Guido on June 19, 2000 at 15:56:43:

In Reply to: How to Copy/Move/Delete files posted by Leox, again :) on June 19, 2000 at 15:53:04:

: How do I Copy/Move/Delete files? Thanks again :)

To copy a file, use this WinApi function, which returns TRUE if the file is copied:

CopyFile(
lpExistingFileName, // pointer to name of an existing file
lpNewFileName, // pointer to filename to copy to
bFailIfExists) // flag for operation if file exists

Example:
CopyFile(PChar('c:\test\file1.txt'), PChar('c:\test\file2.txt'), TRUE)
will return TRUE if the second file doesn't exist yet, and FALSE if it already exists (then no copy is done). I wrapped it up in a more friendlier form as the function FileCopy() which is included in the DClub library.

To delete a file, simply use the Delphi function DeleteFile(const FileName: string): Boolean;
Example: DeleteFile('c:\test\file1.txt');

Moving a file: first copy it, then delete the original file.

The DClub Libary contains some wrappers for the functions to copy and to move files. These functions also return messages that show if the operation succeeded, and if not, what went wrong (because in a MOVE two things can go wrong: copying the file, but also deleting the original file).




Related Articles and Replies: