Inno Setup: Backup external file before install de

2019-01-15 12:21发布

问题:

I would make a backup of files and folders before the [InstallDelete] section deletes them

[Files]
Source: "{app}\res_mods\configs\wotstat\cache.json"; \
  DestDir: "{app}\_backup\res_mods_{#DateTime}\configs\wotstat\"; \
  Flags: external skipifsourcedoesntexist uninsneveruninstall 
Source: "{app}\res_mods\0.9.17.1\vehicles\*"; \
  DestDir:"{app}\_backup\res_mods_{#DateTime}\0.9.17.1\vehicles\"; \
  Flags: external skipifsourcedoesntexist createallsubdirs recursesubdirs uninsneveruninstall

This works fine. But, if i check

[InstallDelete]
Type: filesandordirs; Name: "{app}\mods\*.*"; Tasks: cleanres
Type: filesandordirs; Name: "{app}\res_mods\*.*"; Tasks: cleanres

No files are saved

How I can made it work. Thx

回答1:

The [InstallDelete] section is processed (as one would expect) before the [Files] section. See the installation order.

You can code the backup in the CurStepChanged(ssInstall) event, that happens before the installation starts:

[Code]

procedure CurStepChanged(CurStep: TSetupStep);
var
  SourcePath: string;
  DestPath: string;
begin
  if CurStep = ssInstall then
  begin
    SourcePath := ExpandConstant('{app}\res_mods\0.9.17.1\vehicles');
    DestPath := ExpandConstant('{app}\_backup\res_mods_{#DateTime}\0.9.17.1\vehicles');
    Log(Format('Backing up %s to %s before installation', [SourcePath, DestPath]));
    if not ForceDirectories(DestPath) then
    begin
      Log(Format('Failed to create %s', [DestPath]));
    end
      else
    begin
      DirectoryCopy(SourcePath, DestPath);
    end;

    SourcePath := ExpandConstant('{app}\res_mods\configs\wotstat\cache.json');
    DestPath := ExpandConstant('{app}\_backup\res_mods_{#DateTime}\configs\wotstat');
    if not ForceDirectories(DestPath) then
    begin
      Log(Format('Failed to create %s', [DestPath]));
    end
      else
    begin
      if not FileCopy(SourcePath, DestPath + '\cache.json', False) then
      begin
        Log(Format('Failed to copy %s', [SourcePath]));
      end
        else 
      begin
        Log(Format('Backed up %s', [SourcePath]));
      end;
    end;
  end;
end;

The code uses the DirectoryCopy function from the Inno Setup: copy folder, subfolders and files recursively in Code section.