CopyFile function, file copy function. Its basic structure is as follows:
copyfile(
lpcstr lpexistingfilename, // Source file path
lpcstr lpnewfilename, //New file path
bool bfailifexists // If true, if the new file already exists, return false; if false, if the new file already exists, the original one will be
cover.
);
The function returns true for success, and false for failure;
Example:
CopyFile(pChar('sql.txt'),pChar(ExtractFilePath(application.ExeName) + '2.txt'),true);
Later, when experimenting, I found that the source file can be converted without adding pChar function, but the new file cannot be used, and a type conversion error will occur. Moreover, when the source file name is not added, the default is to
Use this directory of the program.
Because the function returns a boolean type, it can also be written like this:
if CopyFile('D:/yun_yue/sql.txt',pChar(ExtractFilePath(Application.ExeName) + '2.txt'),true) then
//And, please make sure your file path is correct, otherwise the function returns fail.
ShowMessage('Copy File Completed!')
else
ShowMessage('Copy File Failed!');
The CopyFile function can also be copied with files in network neighbors. Using the above example, the format can be changed to the following:
if CopyFile('//sh-sfis/Yun_Yue/yun_yue/Copytext.txt',pChar(ExtractFilePath(Application.ExeName) + '2.txt'),false) then
// Setting the third parameter of the function here to false will overwrite the '2.txt' file that exists in this directory of my application.
ShowMessage('Copy File Completed!')
else
ShowMessage('Copy File Failed!');