21

I need to delete some files after the installation finishes.

I was using the [Run] section to call cmd to delete the files but I wanted to improve the deletions by using Inno Setup code and not batch then I've seen the [InstallDelete] section but this delete the files BEFORE the [Run] section so... there is something I can do to delete the files after [Run] section?

Here is my script:

#define InstallerName "VirtualBox-4.2.16-r86992-MultiArch_amd64.msi"
#define ExtensionName "Oracle_VM_VirtualBox_Extension_Pack-4.2.16-86992.vbox-extpack"

[Files]
Source: {tmp}\*; DestDir: {tmp}; Flags: deleteafterinstall recursesubdirs createallsubdirs ignoreversion

[Run]
Filename: {tmp}\{#InstallerName}; Parameters: "/passive /norestart ADDLOCAL=VBoxApplication INSTALLDIR=""{app}"""; StatusMsg: Instalando VirtualBox...; Flags: shellexec RunHidden WaitUntilTerminated
Filename: {tmp}\xml.exe; Parameters: "ed --inplace -N N=""http://www.innotek.de/VirtualBox-settings"" --update ""//N:ExtraDataItem[@name='GUI/UpdateDate']/@value"" --value never ""{%userprofile}\.virtualbox\virtualbox.xml"""; StatusMsg: Instalando VirtualBox...; Flags: RunHidden WaitUntilTerminated
Filename: {app}\VBoxManage.exe; Parameters: "extpack install --replace ""{tmp}\{#ExtensionName}"""; StatusMsg: Instalando Extension Pack...; Flags: RunHidden WaitUntilTerminated
Filename: {app}\virtualbox.exe; Description: {cm:LaunchProgram,VirtualBox}; Flags: shellexec postinstall unchecked skipifsilent nowait

[InstallDelete]
Name: {commondesktop}\Oracle VM VirtualBox.lnk; Type: files
Name: {commonstartmenu}\Programs\Oracle VM VirtualBox; Type: filesandordirs
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417

3 Answers3

20

If you're trying to delete temporary files (eg. needed by a subinstall):

Anything you install to {tmp} will automatically be deleted at the end of the install.

If you cant' install to {tmp} for some reason then you can use the deleteafterinstall flag on the [Files] entry.

If you're trying to delete files created by that subinstall, then you should contact the vendors or check their documentation and see if there's a command line parameter you can pass to suppress installation of that item in the first place. There usually should be for optional things like desktop icons.

Miral
  • 12,637
  • 4
  • 53
  • 93
  • 1
    As far as I understand these files are not part of the installation, so 'deleteafterinstall' is not an option. I may have got it wrong of course.. – Sertac Akyuz Sep 04 '13 at 00:59
  • Thanks for your response, yes maybe the best way is to investigate if the MSI has a package/parameter to don't install the lnk files but I need to do the same for a lot of installers so I can't spent that time searching for every MSI package options. – ElektroStudios Sep 04 '13 at 12:24
16

You can delete your files in the post install step of the CurStepChanged event handler

[Code]
procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssPostInstall then begin
    DeleteFile(ExpandConstant('{commondesktop}\Oracle VM VirtualBox.lnk'));
    ..
Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
4

Finally what I've did is this.

Firts I try to delete the original MSI package files if them exists in the [InstallDelete] section but that doesnt's delete the folder at all so then after that I extract a dummy file and folder in the same locations with the "deleteafterinstall" flag to delete them.

If you think this can be improved then please just tell me how to do it, really I don't want to use external code for this because I need a "generic" way to do it for about 200 installers, writting code for that quantity of installers take a lot of time.

#define InstallerName "VirtualBox-4.2.16-r86992-MultiArch_amd64.msi"
#define ExtensionName "Oracle_VM_VirtualBox_Extension_Pack-4.2.16-86992.vbox-extpack"

[Setup]
AppName=VirtualBox
...
...

[InstallDelete]
Name: {commondesktop}\Oracle VM VirtualBox.lnk; Type: files
Name: {commonstartmenu}\Programs\Oracle VM VirtualBox; Type: filesandordirs

[Dirs]
Name: {commonstartmenu}\Programs\Oracle VM VirtualBox; Flags: deleteafterinstall; attribs: hidden

[Files]
Source: {commondesktop}\Oracle VM VirtualBox.lnk; DestDir: {commondesktop}; Flags: deleteafterinstall ignoreversion; Attribs: hidden
Source: {tmp}\*; DestDir: {tmp}; Flags: deleteafterinstall recursesubdirs createallsubdirs ignoreversion

[Icons]
Name: {userstartmenu}\Programs\Multimedia\VirtualBox; Filename: {app}\virtualbox.exe; WorkingDir: {app}

[Run]
Filename: {tmp}\{#InstallerName}; Parameters: "/passive /norestart ADDLOCAL=VBoxApplication INSTALLDIR=""{app}"""; StatusMsg: Instalando VirtualBox...; Flags: shellexec RunHidden WaitUntilTerminated
Filename: {tmp}\xml.exe; Parameters: "ed --inplace -N N=""http://www.innotek.de/VirtualBox-settings"" --update ""//N:ExtraDataItem[@name='GUI/UpdateDate']/@value"" --value never ""{%userprofile}\.virtualbox\virtualbox.xml"""; StatusMsg: Instalando VirtualBox...; Flags: RunHidden WaitUntilTerminated
Filename: {app}\VBoxManage.exe; Parameters: "extpack install --replace ""{tmp}\{#ExtensionName}"""; StatusMsg: Instalando Extension Pack...; Flags: RunHidden WaitUntilTerminated
Filename: {app}\virtualbox.exe; Description: {cm:LaunchProgram,VirtualBox}; Flags: shellexec postinstall unchecked skipifsilent nowait
TLama
  • 75,147
  • 17
  • 214
  • 392
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • The `[InstallDelete]` section is the first step of the installation process, the `[Run]` section is performed much later. So if you're going to delete something what is installed from the `[Run]` section, you are doing it wrong. If you don't want to use a code in the `[Code]` section, use e.g. `del` command and run it from the `[Run]` section somewhere after the installation entry. But, as Miral suggests, the best is to look if there's a way to prevent the installer even create those shortcuts. – TLama Sep 04 '13 at 12:26
  • @TLama you will mean "Del" CMD command or maybe is a InnoSetup Command?. I need to say that the installer works fine and delete the file and folder. I know is a little tricky way to do the things but... it works xD – ElektroStudios Sep 04 '13 at 12:26
  • Well, so then I totally misunderstood your question. I thought you're installing something from the `[Run]` section and that something creates the shortcuts on desktop and start menu. And that you want to delete those shortcuts after the installation of that something is done. – TLama Sep 04 '13 at 12:28
  • @TLama yes you're right, I install an MSI from the RUN section, that MSI install the shortcuts, and that is what I delete, thanks for comments – ElektroStudios Sep 04 '13 at 12:30
  • My initial question was how to do it without using code, I know maybe using Pascal or CMD in the run section can be more simplify but I only posted this because it also works – ElektroStudios Sep 04 '13 at 12:32
  • So is this a trick that the installer is deleting files with 'deleteafterinstall' that it thinks it installed? If this is the case, I think, you'd like to switch the accept to Miral's answer. – Sertac Akyuz Sep 04 '13 at 12:33
  • @TLama - I think in the current implementation the [InstallDelete] section is just obsolete. It's the 'deleteafterinstall' that's taking effect. – Sertac Akyuz Sep 04 '13 at 12:41
  • Ah, I got it now. You're having a set of some (fake) shortcut files copying them to the target having the `deleteafterinstall` flag specified. That's really utter way... This solution will lead to more work than if you were used the code section. Good might also be to use e.g. the `AfterInstall` parameter and run a cleaning function from there. – TLama Sep 04 '13 at 12:47
  • I'd consider this to be a bug in the installer. Nevertheless, nice trick, +1 – Sertac Akyuz Sep 04 '13 at 13:49
  • @TLama can you explain me why you edited my code? the code it's as is... with your modification (elimination of [InstallDelete] section) the trick will not work as expected if the files already exists on that locations. – ElektroStudios Sep 04 '13 at 19:32
  • Well, for the last time :-) The `[InstallDelete]` section is [`the first step`](http://jrsoftware.org/ishelp/topic_installorder.htm) of the installation process, `[Run]` section one of the latest. So, if you install something from the `[Run]` section you can't expect you'll clean something from that `[Run]` section installation from the `[InstallDelete]` section because that section has already been executed. `[InstallDelete]` should rather be named `PreinstallDelete`. – TLama Sep 04 '13 at 19:39
  • You are telling me what I already know, I repeat the [InstallDelete] section what you deleted (without my permission), the first step, is TOTALLY necessary to make the trick work. you are only supposing things, it's me who has tested it in various sceneries. If you don't want to accept it or trust it then... I don't know what you need to hear. – ElektroStudios Sep 04 '13 at 19:50
  • In other words: If you first don't try to delete the files inside the [InstallDelete] section then the dummy file/folder will not be deleted after the installation, simply as that. With [InstallDelete] section I ensure previous files of previous installations are removed BEFORE the [RUN] section, then I copy the dummy file/folder and then starts the subinstaller, and after all that I delete the dummy files. – ElektroStudios Sep 04 '13 at 19:51
  • Let it be. I'm giving up. This is an utter solution in my view. Btw. have you heard about `AfterInstall` parameter, right ? [not voting down though] – TLama Sep 04 '13 at 19:56
  • Thanks for rectify the edit modification. I don't know the Stackoverflow rules but I will suggest you to suggest people to edit their own code instead of manupulating it yourself, one thing is manipulate millspelling or that things but other is to delete code even if you think code is necessary or not. thanks for your all help in my question. – ElektroStudios Sep 04 '13 at 19:58