I'm working on an installer that needs to create a backup of a directory prior to installing. The method I implement is purely to copy all files from the current directory to a new directory and then I am free to overwrite files (my installer) in the old directory.
However, I receive a prompt indicating file copy failed
, but I just cannot understand why it doesn't work. My error message prints the correct directory\filename and I can verify that they exist and aren't open in any external programs.
Below is the code, taken (and modified slightly) from: http://blogs.candoerz.com/question/139833/inno-setup-copy-folder-subfolders-and-files-recursively-in-code-section.aspx
function DirectoryCopy(SourcePath, DestPath: string): boolean;
var
FindRec: TFindRec;
SourceFilePath: string;
DestFilePath: string;
begin
if FindFirst(SourcePath + '\*', FindRec) then begin
try
repeat
if (FindRec.Name <> '.') and (FindRec.Name <> '..') then begin
SourceFilePath := SourcePath + '\' + FindRec.Name;
DestFilePath := DestPath + '\' + FindRec.Name;
if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then begin
if FileCopy(SourceFilePath, DestFilePath, False) then begin
Result := True;
MsgBox('Copy Worked!', mbInformation, MB_OK);
end else begin
Result := False;
MsgBox('Copy Failed!'+SourceFilePath, mbInformation, MB_OK);
end;
end else begin
if CreateDir(DestFilePath) then begin
Result := True;
MsgBox('Created Dir!', mbInformation, MB_OK);
DirectoryCopy(SourceFilePath, DestFilePath);
end else begin
Result := False;
MsgBox('Failed to create Dir!', mbInformation, MB_OK);
end;
end;
end;
until not FindNext(FindRec);
finally
FindClose(FindRec);
end;
end else begin
Result := False;
MsgBox('Failed to List!', mbInformation, MB_OK);
end;
end;
I suspect that the directory you are trying to copy to doesn't exist. You need to create the directory first using
CreateDir
orForceDirectories
. The file functions (including Martin'sDirectoryCopy
function, which uses these internal functions) require the directory to exist or they will fail. They will not automatically create the path for you. It is a pity this isn't documented anywhere (that I could find, although someone may be able to correct me on that) as it caught me out for a while too.The original link to Martin's
DirectoryCopy
function is available here.As we didn't receive any information that can be used for debugging your actual problem, I'm posting generic instructions for debugging problems with files.
To find out why any (file) system function fails, use the
GetLastError
WinAPI function. You can also use theSysErrorMessage
support function to convert an error code to a message. The function is a wrapper for theFormatMessage
WinAPI function.A correct call to the
FileCopy
(or theFileCopyLogged
) is like:As @RobertWigley already posted in his answer, make sure the target folder exists. If it does not, you will get error code 3 (The system cannot find the path specified).
Also make sure you use a full path to a target file in the
NewFile
argument (C:\folder\file
). Not just a path to a target folder (C:\folder
orC:\folder\
). If you use just a path to the target folder, you will get error code 5 (Access is denied).Of course both these codes can indicate other problems too.