2

How can I delete a series of files once the Run has been executed?

I want to delete a lot of DLL files and some other files because they are now in separate sub folders. But some of the DLL files are registered COM etc. Once Run has finished the old DLLs will not be registered anymore. So it will be safe for me to delete them.

InstallDelete is too early:
https://jrsoftware.org/ishelp/index.php?topic=installorder

So I want ability to delete *.dll but exclude pattern*.dll ONCE install finished.


I've seen a similar question:

Delete a file AFTER installation in Inno Setup

It seems I need to use something like:

[Code]
procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssPostInstall then begin
    DeleteFile(ExpandConstant('path'));
    ..

Ideally I want to delete:

  • {app}\*.dll
  • {app}\*.config
  • {app}\GoogleAuthAndSync.exe

But, I want to KEEP these files in the {app} folder:

  • {app}\MeetSchedAssist*.dll
  • {app}\VclStylesinno.dll

That is what I want to achieve. This is because the files are now being installed into distinct sub-folders and being managed there, and not all mixed up in the app folder.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164

2 Answers2

1

The CurStepChanged(ssPostInstall) seems like the the right approach to me. So all you need on top of that is a way to delete all files, with some exceptions, right?

Start here: Delete whole application folder except for "data" subdirectory in Inno Setup


If you need to know how to test for partial filename (MeetSchedAssist*.dll):

  • If you need just one-off test, an ad-hoc condition like this will do:

    (CompareText(Copy(FindRec.Name, 1, 15), 'MeetSchedAssist') = 0) and
    (CompareText(Copy(FindRec.Name, Length(FindRec.Name) - 3, 4), '.dll') = 0)
    
  • If you have many tests, you can use MatchesMaskEx function from Inno Setup - Integer or Set/Range wildcard?:

    MatchesMaskEx('MeetSchedAssist*.dll', FindRec.Name)
    
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
1

I thought I would add my final code:

(* Cannot use built-in DeleteFile directly in AfterInstall as it's a function,
    not a procedure. And this way we can add some error handling too. *)
procedure DoDeleteFile(FileName: string);
begin
    if (FileExists(FileName)) then
    begin
        if DeleteFile(FileName) then
        begin
            Log(Format('"%s" deleted', [FileName]));
        end
        else begin
            MsgBox(Format('Failed to delete "%s"', [FileName]), mbError, MB_OK);
        end;
    end;
end;

{ Deletes obsolete DLL files etc. }
procedure DelObsoleteFiles(Path: string);
var
    FindRec: TFindRec;
    FilePath: string;
begin
    if FindFirst(Path + '\*.dll', FindRec) then
    begin
        try
            repeat
                FilePath := Path + '\' + FindRec.Name;
                if CompareText(FindRec.Name, 'VclStylesinno.dll') = 0 then
                begin
                    Log(Format('Keeping DLL %s', [FilePath]));
                end else if CompareText(Copy(FindRec.Name, 1, 15), 'MeetSchedAssist') = 0 then 
                begin
                    Log(Format('Keeping DLL %s', [FilePath]));
                end else begin
                    DoDeleteFile(FilePath);
                end;
            until not FindNext(FindRec);
        finally
            FindClose(FindRec);
        end;
    end else
    begin
      Log(Format('Failed to list %s', [Path]));
    end;
  
    DoDeleteFile(Path + '\GoogleAuthAndSync.exe')
    DoDeleteFile(Path + '\GoogleAuthAndSync.exe.config')
  
end;

procedure CurStepChanged(CurStep: TSetupStep);
var
    ResultCode: integer;
begin
    if (CurStep = ssPostInstall) then
    begin
        (* Delete obsolete DLL files etc. because they are now maintained in
          separate sub-folders *)
        DelObsoleteFiles(ExpandConstant('{app}'));
    end;
end;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • I am puzzled by your comment in the code: "Cannot use built-in DeleteFile directly in AfterInstall as it's a function, not a procedure" - why do you say this? (It doesn't matter if `DeleteFile` is a function.) – Bill_Stewart Dec 28 '22 at 21:47
  • @Bill_Stewart That is a comment carried forward from another answer provided to me here on SO over the years. – Andrew Truckle Dec 28 '22 at 21:57
  • @Bill_Stewart See the answer here where the code provided has that comment: https://stackoverflow.com/q/48807033/2287576 – Andrew Truckle Dec 29 '22 at 08:17
  • 1
    Never mind; I confused myself. (An `AfterInstall` parameter isn't an event function and musn't have a return value per the docs.) – Bill_Stewart Dec 29 '22 at 19:13