在 Inno Setup InstallDelete 部分之前备份外部文件

yay*_*070 1 backup inno-setup

我会在该部分删除文件和文件夹之前[InstallDelete]对其进行备份

[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
Run Code Online (Sandbox Code Playgroud)

这很好用。但是,如果我检查

[InstallDelete]
Type: filesandordirs; Name: "{app}\mods\*.*"; Tasks: cleanres
Type: filesandordirs; Name: "{app}\res_mods\*.*"; Tasks: cleanres
Run Code Online (Sandbox Code Playgroud)

没有保存文件

我怎样才能让它发挥作用。谢谢

Mar*_*ryl 5

[InstallDelete]部分在该部分之前被处理(正如人们所期望的)[Files]。请参阅安装顺序

您可以在安装开始之前发生的CurStepChanged(ssInstall)事件中对备份进行编码:

[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;
Run Code Online (Sandbox Code Playgroud)

该代码使用了Inno SetupDirectoryCopy中的功能:在代码部分递归复制文件夹、子文件夹和文件