Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backup files and restore them on uninstall with InnoSetup?

Tags:

inno-setup

Consider the following:

  • I have two files, for example XXX.txt and YYY.txt

  • I want to install them to a folder (let's say files), in which there are already XXX.txt and YYY.txt files

  • I want to "back up" the two original files, renaming them to XXX.txt.backup and YYY.txt.backup

  • On uninstall I want to restore the two files to their original state

How can I achieve this with Inno Setup?

like image 535
lostprophet Avatar asked Dec 17 '22 22:12

lostprophet


1 Answers

Add

[Files]
; Backup Function_Template
Source: "{app}\XXX.txt"; DestDir: "{app}"; DestName: "XXX.txt.bkup"; Flags: external skipifsourcedoesntexist uninsneveruninstall

That would move the existing file, and the flags will prevent from uninstalling it. Now in the code you can put

[Code] 
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
var
  OldFile: string;
begin
  case CurUninstallStep of    
    usPostUninstall:
      begin
        OldFile := ExpandConstant('{app}\XXX.txt.bkup');
        if FileExists(OldFile) then
          RenameFile(OldFile, ExpandConstant('{app}\XXX.txt'));
      end;
  end;
end;
like image 51
adn Avatar answered Dec 29 '22 10:12

adn