How to Copy / Move Files
Delphi’s runtime library does not provide any routines for copying or moving files. However, you can directly call the Windows API CopyFile() function to copy a file. CopyFile() is also useful when moving files across drives because neither Delphi’s RenameFile() function nor the Windows API MoveFile() function can rename/move files across drives.
!!Note: The file attributes for the existing file are copied to the new file, but the security attributes are not.
Here’s the Delphi code:
uses Windows; ... (* sSource - (PChar or string if typecasted) The full name of the source ** file. ** sDestination - (PChar or string if typecasted) The full name of the ** destination file. ** bNoOverwrite - (Boolean) Set False if you want to overwrite existing ** files, else set True. ** ** Function result: True if successful, else it will return False. *) CopyFile(PChar(sSource), PChar(sDestination), bNoOverwrite); ...
Examples:
{ Both examples allow file overwriting }
CopyFile('C:\\Autoexec.bat', 'A:\\Backup\\Autoexec.bat', False);
CopyFile(PChar(Edit1.Text), PChar(Edit2.Text), False);
Categories: System api, code, copy, copyfile, Delphi, delphi code, file, files, move, move files, movefile, open source, overwrite, routine, runtime library, security attributes, source, source code, source file, tip, trick, windows, windows api