[Code]
function GetBackupFilePath(filename: string): string;
begin
Result := ExpandConstant('{app}\Backup\' + filename);
end;
procedure CopyFilesToBackup;
var
fileList: TStringList;
i: Integer;
sourceFile, destFile: string;
begin
fileList := TStringList.Create;
try
fileList.LoadFromFile(ExpandConstant('{tmp}\MyFilesList.txt'));
for i := 0 to fileList.Count - 1 do begin
sourceFile := ExpandConstant('{app}\' + fileList[i]);
destFile := GetBackupFilePath(fileList[i]);
if FileExists(sourceFile) then begin
if not ForceDirectories(ExtractFilePath(destFile)) then begin
MsgBox('Failed to create backup directory', mbError, MB_OK);
Exit;
end;
if not FileCopy(sourceFile, destFile, False) then begin
MsgBox('Failed to copy file ' + sourceFile + ' to backup', mbError, MB_OK);
Exit;
end;
end else begin
MsgBox('Source file ' + sourceFile + ' does not exist', mbError, MB_OK);
Exit;
end;
end;
finally
fileList.Free;
end;
end;
function MoveFile(const srcFile, destFile: PAnsiChar): Integer;
external 'MoveFileA@kernel32.dll stdcall';
procedure MoveDir(BaseDir, DestDir, SubDirs, Filename: String; Flag: DWORD);
var
FSR: TFindRec;
file1, file2, sub: String;
begin
log(AddBackslash(BaseDir) + Addbackslash(SubDirs) + Filename);
if FindFirst(AddBackslash(BaseDir) + Addbackslash(SubDirs) + Filename, FSR) then
begin
try
repeat
if (FSR.Name = '.') or (FSR.Name = '..') then Continue;
if FSR.Attributes and FILE_ATTRIBUTE_DIRECTORY <> 0 then
begin
if SubDirs = '' then
sub := FSR.Name
else
sub := AddBackslash(SubDirs) + FSR.Name;
MoveDir(BaseDir, DestDir, sub, Filename, Flag);
end
else
begin
file1 := AddBackslash(BaseDir) + Addbackslash(SubDirs) + FSR.Name;
file2 := file1;
StringChange(file2, BaseDir, DestDir);
ForceDirectories(ExtractFilePath(File2));
if Flag = 2 then
MoveFile(PAnsiChar(File1), PAnsiChar(File2))
else
FileCopy(PAnsiChar(File1), PAnsiChar(File2), false);
end;
until not
FindNext(FSR);
finally
FindClose(FSR);
end;
end;
end;
procedure RestoreBackup(FromDir, DestDir: String);
begin
MoveDir(FromDir, DestDir, '', '*', 1); //2 MOVE 1 COPY
end;