Inno Setup-删除整个应用程序文件夹(数据子目录除外) [英] Inno Setup - Delete whole application folder except for data subdirectory

查看:57
本文介绍了Inno Setup-删除整个应用程序文件夹(数据子目录除外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常备份备份游戏没有问题,但是对于这个特定的游戏,我使用FreeArc,因为游戏很大,因此Inno Setup并不真正知道要删除哪些文件.使用FreeArc,您需要使用

I usually have no problem backing up savegames, but for this particular game, I use FreeArc, since the game is very large, so Inno Setup doesn't really know which files to delete. With FreeArc, you need to use

[UninstallDelete]
Type: files; Name: "{app}"

但是此特定游戏将保存游戏存储在{app}\data\save中.我需要一个函数将该文件夹移到某个位置,卸载游戏,然后再将其移回.

But this particular game stores savegames in {app}\data\save. I need a function to move that folder somewhere, uninstall the game, and then move it back.

这是我的代码:

procedure DirectoryCopy(SourcePath, DestPath: string);
var
  FindRec: TFindRec;
  SourceFilePath: string;
  DestFilePath: string;
begin
  if FindFirst(SourcePath + '\*', FindRec) then
  begin
    try
      repeat
        if (FindRec.Name <> '.') and (FindRec.Name <> '..') then
        begin
          SourceFilePath := SourcePath + '\' + FindRec.Name;
          DestFilePath := DestPath + '\' + FindRec.Name;
          if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then
          begin
            if FileCopy(SourceFilePath, DestFilePath, False) then
            begin
              Log(Format('Copied %s to %s', [SourceFilePath, DestFilePath]));
            end
              else
            begin
              Log(Format('Failed to copy %s to %s', [SourceFilePath, DestFilePath]));
            end;
          end
            else
          begin
            if CreateDir(DestFilePath) then
            begin
              Log(Format('Created %s', [DestFilePath]));
              DirectoryCopy(SourceFilePath, DestFilePath);
            end
              else
            begin
              Log(Format('Failed to create %s', [DestFilePath]));
            end;
          end;
        end;
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
  end
    else
  begin
    Log(Format('Failed to list %s', [SourcePath]));
  end;
end;

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
Begin
  if (CurUninstallStep = usUninstall) and
     DirExists(ExpandConstant('{app}\data\SAVE')) then
  begin
    if MsgBox('Do you want to delete all save games?',
         mbConfirmation, MB_YESNO) = IDYES then
      begin
        DelTree(ExpandConstant('{app}'), True, True, True);
      end
    else
      begin
        DirectoryCopy(ExpandConstant('{app}\data\SAVE'), ExpandConstant('{app}\SAVE'));
        Deltree(ExpandConstant('{app}\data'), True, True, True);
        DirectoryCopy(ExpandConstant('{app}\SAVE'), ExpandConstant('{app}\data\SAVE'));
      end
  end;
End;

这是日志:

2016-04-08 10:16:02.420   Log opened. (Time zone: UTC+04:00)
2016-04-08 10:16:02.421   Setup version: Inno Setup version 5.5.6 (u)
2016-04-08 10:16:02.421   Original Uninstall EXE: C:\Games\MyApp\unins001.exe
2016-04-08 10:16:02.421   Uninstall DAT: C:\Games\MyApp\unins001.dat
2016-04-08 10:16:02.421   Uninstall command line: /SECONDPHASE="C:\Games\MyApp\unins001.exe" /FIRSTPHASEWND=$B5111C /log=C:\setup.log
2016-04-08 10:16:02.421   Windows version: 6.1.7601 SP1  (NT platform: Yes)
2016-04-08 10:16:02.421   64-bit Windows: Yes
2016-04-08 10:16:02.421   Processor architecture: x64
2016-04-08 10:16:02.421   User privileges: Administrative
2016-04-08 10:16:02.421   64-bit install mode: No
2016-04-08 10:16:02.422   Created temporary directory: C:\Users\George\AppData\Local\Temp\is-GSI4R.tmp
2016-04-08 10:16:02.440   Message box (Yes/No):
                          Are you sure you want to completely remove MyApp and all of its components?
2016-04-08 10:16:04.014   User chose Yes.
2016-04-08 10:16:04.031   Message box (Yes/No):
                          Do you want to delete all save games?
2016-04-08 10:16:04.790   User chose No.
2016-04-08 10:16:04.791   Failed to create C:\Games\MyApp\SAVE\scores
2016-04-08 10:16:04.805   Failed to list C:\Games\MyApp\SAVE
2016-04-08 10:16:04.805   Starting the uninstallation process.
2016-04-08 10:16:04.806   Deleting Uninstall data files.
2016-04-08 10:16:05.326   Uninstallation process succeeded.
2016-04-08 10:16:05.326   Removed all? Yes
2016-04-08 10:16:05.326   Need to restart Windows? No
2016-04-08 10:16:05.814   Message box (OK):
                          MyApp was successfully removed from your computer.
2016-04-08 10:16:06.678   User chose OK.
2016-04-08 10:16:06.679   Log closed.

推荐答案

将保存的内容复制掉然后再返回是不是太过分了?

Isn't it an overkill to copy the saves away and then back?

一旦您已经有了用于迭代目录树的代码,请对其进行迭代以删除文件(保存"文件夹除外).

Once you already have a code to iterate a directory tree, iterate it to delete the files, except the "save" folder.

下面的代码完成所有删除工作(在任何一种模式下),因此您需要删除[UninstallDelete]部分.

The below code does all the delete work (in either mode), so you need to remove the [UninstallDelete] section.

procedure DelTreeExceptSavesDir(Path: string);
var
  FindRec: TFindRec;
  FilePath: string;
begin
  if FindFirst(Path + '\*', FindRec) then
  begin
    try
      repeat
        if (FindRec.Name <> '.') and (FindRec.Name <> '..') then
        begin
          FilePath := Path + '\' + FindRec.Name;
          if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then
          begin
            if DeleteFile(FilePath) then
            begin
              Log(Format('Deleted file %s', [FilePath]));
            end
              else
            begin
              Log(Format('Failed to delete file %s', [FilePath]));
            end;
          end
            else
          begin
            if CompareText(FindRec.Name, 'save') = 0 then
            begin
              Log(Format('Keeping directory %s', [FilePath]));
            end
              else
            begin
              DelTreeExceptSavesDir(FilePath);

              if RemoveDir(FilePath) then
              begin
                Log(Format('Deleted directory %s', [FilePath]));
              end
                else
              begin
                Log(Format('Failed to delete directory %s', [FilePath]));
              end;
            end;
          end;
        end;
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
  end
    else
  begin
    Log(Format('Failed to list %s', [Path]));
  end;
end;

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
var
  SavePath: string;
  AppPath: string;
begin
  SavePath := ExpandConstant('{app}\data\SAVE');

  if CurUninstallStep = usUninstall then
  begin
    AppPath := ExpandConstant('{app}')

    if (not DirExists(SavePath)) or
       (MsgBox('Do you want to delete all save games?',
          mbConfirmation, MB_YESNO) = IDYES) then
    begin
      Log(Format('Deleting application folder %s', [AppPath]));
      DelTree(AppPath, True, True, True);
    end
      else
    begin
      Log(Format('Deleting application folder %s except saves', [AppPath]));
      DelTreeExceptSavesDir(AppPath);
    end;
    Log('Delete done');
  end
end;


但是,在[UninstallDelete]部分中列出要删除的目录难道不是很容易吗?文件夹不能太多.


Though, wouldn't it be easier to list directories to delete in the [UninstallDelete] section? There cannot be that many folders.

[UninstallDelete]
Type: files; Name: "{app}\*"
Type: filesandordirs; Name: "{app}\data\data1"
Type: filesandordirs; Name: "{app}\data\data2"
Type: filesandordirs; Name: "{app}\anotherfolder"

这篇关于Inno Setup-删除整个应用程序文件夹(数据子目录除外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆