Inno Setup-更新时删除旧/过时的文件 [英] Inno Setup - Delete old/obsolete files on update
问题描述
因此,我意识到这个问题已经被问过了.实际上,在撰写本文之前,我已经阅读了其中的10篇文章,但是他们中没有一个有适用的解决方案,我希望今天有人能找到一些东西.
So, I realize that this question has been asked before. In fact I read through a good 10 of them before writing this, but none of them have a applicable solution and I'm hoping that nowadays someone has found something.
问题: 我的程序是使用脚本构建的,可以在一个文件夹中创建所有最终文件. 这些文件包含在inno中,如下所示:
The problem: My program is build with a script, creating all final files in a single folder. Those files are included in inno like this:
[Files]
Source: "build\exe.win-amd64-3.6\*"; Excludes: "*.key, *.log"; DestDir: "{app}"; \
Flags: ignoreversion recursesubdirs createallsubdirs
该应用程序已经发布了几个月,并进行了不同的更新.没有旧文件的记录,尽管由于我们具有版本控制权,可能会费心地重新组装旧文件,而我可以再次构建旧安装程序.
The application has been out there for a few months with different updates. There is no record anymore of old files, though it could be painstakingly re-assembled as we do have version control and I could build the old installers again.
据我了解,您打算使用InstallDelete
部分来删除旧文件-但是,您并非要使用通配符,也没有Excludes
部分来保护我们拥有的单个文件夹其中可能包含他们可能想要保留的用户数据.
From what I understand, you're meant to use the InstallDelete
section to get rid of old files - however you are not meant to use wildcards and there is also no Excludes
section to safeguard the single folder we have that may contain user data they may want to keep.
那么,如何清除旧文件?该应用程序的大小为100 MB,当前用户可能拥有不再需要的300 MB以上的旧文件,我很乐意将其清理干净.
So, how do I get rid of old files? The application is 100 MB and a current user may have 300+ MB of old files that are no longer necessary, I'd love to clean that up.
TL; DR::我希望安装程序覆盖或删除应用程序目录中的所有文件,但其中一个文件夹包含用户数据.
TL;DR: I want the installer to either overwrite or delete all files in the application directory, except for one folder which contains user data.
推荐答案
最简单的解决方案是在安装之前删除安装文件夹中的所有文件.
The easiest solution is to delete all files in the installation folder before the installation.
如您所知,您可以使用 [InstallDelete]
部分 .但这不允许您排除数据"文件夹.
As you know, you can use [InstallDelete]
section for that. But that does not allow you to exclude the "data" folder.
您可以改为编写该Pascal脚本.请参阅 Inno设置-删除整个应用程序文件夹(数据子目录除外).您可以从我对 CurStepChanged(ssInstall)
事件功能:
You can code that Pascal Scripting instead. See Inno Setup - Delete whole application folder except for data subdirectory. You can call the DelTreeExceptSavesDir
function from my answer to the that question from CurStepChanged(ssInstall)
event function:
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssInstall then
begin
DelTreeExceptSavesDir(WizardDirValue);
end;
end;
如果您确实只想删除过时的文件,为避免删除和重新创建现有文件(无论如何,不是您的情况,因为您始终使用ignoreversion
标志),可以使用预处理器生成要为Pascal脚本安装的文件列表,并使用该列表仅删除真正过时的文件.
If you really want to delete only obsolete files, to avoid deleting and re-creating existing files (what's not your case, as you are using ignoreversion
flag anyway), you can use preprocessor to generate a list of files to be installed for the Pascal Scripting and use that to delete only really obsolete files.
#pragma parseroption -p-
#define FileEntry(DestDir) \
" FilesNotToBeDeleted.Add('" + LowerCase(DestDir) + "');\n"
#define ProcessFile(Source, Dest, FindResult, FindHandle) \
FindResult \
? \
Local[0] = FindGetFileName(FindHandle), \
Local[1] = Source + "\\" + Local[0], \
Local[2] = Dest + "\\" + Local[0], \
(Local[0] != "." && Local[0] != ".." \
? FileEntry(Local[2]) + \
(DirExists(Local[1]) ? ProcessFolder(Local[1], Local[2]) : "") \
: "") + \
ProcessFile(Source, Dest, FindNext(FindHandle), FindHandle) \
: \
""
#define ProcessFolder(Source, Dest) \
Local[0] = FindFirst(Source + "\\*", faAnyFile), \
ProcessFile(Source, Dest, Local[0], Local[0])
#pragma parseroption -p+
[Code]
var
FilesNotToBeDeleted: TStringList;
function InitializeSetup(): Boolean;
begin
FilesNotToBeDeleted := TStringList.Create;
FilesNotToBeDeleted.Add('\data');
{#Trim(ProcessFolder('build\exe.win-amd64-3.6', ''))}
FilesNotToBeDeleted.Sorted := True;
Result := True;
end;
procedure DeleteObsoleteFiles(Path: string; RelativePath: string);
var
FindRec: TFindRec;
FilePath: string;
FileRelativePath: string;
begin
if FindFirst(Path + '\*', FindRec) then
begin
try
repeat
if (FindRec.Name <> '.') and (FindRec.Name <> '..') then
begin
FilePath := Path + '\' + FindRec.Name;
FileRelativePath := RelativePath + '\' + FindRec.Name;
if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY <> 0 then
begin
DeleteObsoleteFiles(FilePath, FileRelativePath);
end;
if FilesNotToBeDeleted.IndexOf(Lowercase(FileRelativePath)) < 0 then
begin
if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY <> 0 then
begin
if RemoveDir(FilePath) then
begin
Log(Format('Deleted obsolete directory %s', [FilePath]));
end
else
begin
Log(Format('Failed to delete obsolete directory %s', [FilePath]));
end;
end
else
begin
if DeleteFile(FilePath) then
begin
Log(Format('Deleted obsolete file %s', [FilePath]));
end
else
begin
Log(Format('Failed to delete obsolete file %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 CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssInstall then
begin
Log('Looking for obsolete files...');
DeleteObsoleteFiles(WizardDirValue, '');
end;
end;
有关其他选项,请参见 Inno设置:删除以前版本安装的文件.
这篇关于Inno Setup-更新时删除旧/过时的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!