使用Inno Setup备份文件并在卸载时恢复它们

时间:2015-08-10 11:46:33

标签: inno-setup

如何在卸载旧版本并使用Inno Setup编译器安装新版本时备份旧版本文件?

[Files] 
Source: "RecordUpdateProcessor\*"; DestDir: "{app}\RecordUpdateProcessor"; \
    Flags: ignoreversion recursesubdirs createallsubdirs; \
    Source: "setup\nssm.exe*"; DestDir: "{app}\"; Flags: ignoreversion

并且已经安装了使用nssm服务的运行部分。

1 个答案:

答案 0 :(得分:1)

没有内置支持。

您可以使用Pascal Script实现它。

使用BeforeInstall function可能是最好的方法。

[Files]
Source: "RecordUpdateProcessor*"; DestDir: "{app}\RecordUpdateProcessor"; \
    Flags: ignoreversion recursesubdirs createallsubdirs; \
    Source: "setup\nssm.exe*"; DestDir: "{app}\"; \
    BeforeInstall: BackupBeforeInstall

[Code]

function BackupFolderPath: string;
begin
  Result := AddBackslash(ExpandConstant('{app}')) + 'Backup';
end;

procedure BackupBeforeInstall;
var
  SourcePath: string;
  DestPath: string;
begin
  if not DirExists(BackupFolderPath) then
  begin
    Log('Creating ' + BackupFolderPath);
    CreateDir(BackupFolderPath);
  end;

  SourcePath := ExpandConstant(CurrentFileName);
  DestPath := BackupFolderPath + '\' + ExtractFileName(SourcePath);
  Log(Format('Backing up %s to %s', [SourcePath, DestPath]));
  if not FileCopy(SourcePath, BackupFolderPath + '\' + ExtractFileName(CurrentFileName), False) then
  begin
    Log('Backup failed');
  end;
end;

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
var
  FindRec: TFindRec;
  SourceDir: string;
  SourcePath: string;
  DestDir: string;
  DestPath: string;
begin
  if CurUninstallStep = usPostUninstall then
  begin
    SourceDir := BackupFolderPath;
    DestDir := RemoveBackslash(ExpandConstant('{app}'));
    if FindFirst(SourceDir + '\*.*', FindRec) then
    begin
      repeat
        if (FindRec.Name <> '.') and (FindRec.Name <> '..') then
        begin
          SourcePath := SourceDir + '\' + FindRec.Name;
          DestPath := DestDir + '\' + FindRec.Name
          Log(Format('Restoring %s to %s', [SourcePath, DestPath]));
          if not RenameFile(SourcePath, DestPath) then
          begin
            Log('Restore failed');
          end;
        end;
      until not FindNext(FindRec);
    end;  
    FindClose(FindRec);

    RemoveDir(SourceDir);
  end;
end;

如果需要递归备份,请注意代码需要改进。 recursesubdirs表明了这一点。相反,setup\nssm.exe*看起来不像文件夹。