uninstall: do not remove file if it has changed - Advanced Installer Community

We’re moving our forums to a new platform.

You can still browse and search existing topics, but you can no longer create new topics or post replies.

For questions, technical issues, or feature requests, please don’t hesitate to contact our support team at support@advancedinstaller.com.

Thank you for your understanding!

shelmers
Posts: 63
Joined: Thu Sep 03, 2009 9:02 pm
Location: Andover, Massachusetts USA

uninstall: do not remove file if it has changed

My installer places a text file on the target system. Normally, I want to remove this file during uninstall -- nothing unusual so far. However, if the user has changed the contents of the text file, I do not want to uninstall it (I still want to remove everything else). I've experimented with a number of things but haven't found the trick yet. Is there a technique for conditional removal of a file that I can use?
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: uninstall: do not remove file if it has changed

Hello,

I am afraid the only solution to this is using a custom action. You would schedule a File Removal operation on the said file, and condition it using a property. Your custom action would have to set this property to some value or another, depending on whether your file has changed or not, before the File Removal operation is executed. Nonetheless, the component to which your file belongs would have to be marked as "Do not register this component with Windows Installer" so that conflict with future updates may be avoided.

Regards,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/
shelmers
Posts: 63
Joined: Thu Sep 03, 2009 9:02 pm
Location: Andover, Massachusetts USA

Re: uninstall: do not remove file if it has changed

Thanks for the idea. Can you suggest an easy way to detect whether the file has changed? Is there a way to use the "hash files" feature? For example, can I access the hash value for the file and store it in a property so it will be available for comparison during uninstall?

I can write a script to store the original file's LastModified date in a property and then compare it during uninstall. It's no big deal to use a script to do this but if there's a way to capture a unique file property from within AI, that would be cleaner.
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: uninstall: do not remove file if it has changed

Hello,

Indeed you could use a hash instead of the last modified date. Nonetheless neither Advanced Installer nor Windows Installer have built-in support for hashing. You would have to find a vbscript hashing algorithm.

I am afraid there is no property unique to a file that you could store as an alternative to a hash. The hashing in Advanced Installer is explained in the File Hashes page of our guide. There is no easy way to access these hashes, and enabling this option would create hashes for all the files in your project, not just for the particular file we are talking about, thus adding build time.

Regards,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/
shelmers
Posts: 63
Joined: Thu Sep 03, 2009 9:02 pm
Location: Andover, Massachusetts USA

Re: uninstall: do not remove file if it has changed

I'm working on this again and have most pieces in place but one problem remains. My logic on install is this:
  • VBscript captures DateLastModified of installed file and writes is to property (DATELASTMOD)
  • installer writes DATELASTMOD to registry
My logic on uninstall is this:
  • Search reads value from registry and sets DATELASTMOD
  • VBscript compares DATELASTMOD (from registry) with DateLastModified for the current file
  • if dates match, VBscript removes file
I've placed the install script (immediate execution) under Before Finalization/Create Shortcuts as this appears to be after files have been copied but before reg values are written. However, the file is NOT on the destination drive at this point, consequently the reg key created by the installer has a null value. I also tried placing the custom action after Before Finalization/RegisterMIMEInfo as this is immediately before WriteRegistryValue.

Am I misunderstanding the installer sequence? Is there another place I can put my script that will capture the date last modified and allow the installer to write the value to the registry? Or do I have to add code to my script to write the reg keys from there?
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: uninstall: do not remove file if it has changed

Hello,

Please note that InstallFiles is a deferred custom action. Deferred custom actions write themselves in the install script. The install script is executed by InstallExecute standard action. Since your custom action is placed before InstallExecute and set to Immediate, the files are not yet installed when your custom action runs.

I am afraid there is no way to achieve this by relying on WriteRegistryValues standard action to write your property to the registry. Files are written by a deferred action, which means files are written during InstallExecute standard action. The only way to place your custom action between InstallFiles and WriteRegistryValues would be to set your custom action as deferred. But that would mean your custom action wouldn't be able to set properties.

Your best bet is to use the reg command to write your property to the registry.

Regards,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/
shelmers
Posts: 63
Joined: Thu Sep 03, 2009 9:02 pm
Location: Andover, Massachusetts USA

Re: uninstall: do not remove file if it has changed

With your comments in mind, Gabriel, I did come up with a solution that achieves my goal. I used the DateLastModified property of the file object and stored it in the registry. As you suggested, it is necessary to write the reg keys from a script that is running deferred but that’s not too difficult.

In case it’s useful for others with a similar problem, here’s a summary of what I needed and what I did. I’m sure there are other ways to accomplish this, but this is what worked for me. I’ve attached the three scripts I wrote.

Goal: Install a file and then remove it during uninstall ONLY if it has not changed since installation.

AI Settings and VBscripts:
  • Go to the component for the file and check “Do not register this component with Windows Installer” (this prevents the installer from removing the file)
  • Create a search ([MYFILE_DATELASTMOD]) that reads the registry value created by script #1; this search will only return a value during uninstall; the value is passed to script #2.
  • Create Attached Custom Action: Script #1 gets DateLastModified for the file and writes that value to a registry key in HKCU.
    • Execution = "Immediate"
    • Condition = (Not Installed)
    • Location = Before Finalization/InstallExecute
  • Create Attached Custom Action: Script #2 reads the registry entry created by script #1, compares that value to the DateLastModified of the current file and decides whether to remove the file. This script runs deferred with no impersonation only because the file I needed to remove is in a protected directory (C:\Program Files), otherwise this script could be combined with script #3.
    • Action Data = [APPDIR];[MYFILE_DATELASTMOD]
    • Execution = "Deferred with no impersonation"
    • Condition = (REMOVE="ALL")
    • Location = Before File Installation/Uninstall
  • Create Attached Custom Action: Script #3 removes the registry entry created by script #1. This script is separate from #2 because #2 requires elevation (see comment above) and this one can't run with elevation or it will access the admin user's HKCU and not the one where the reg key was written.
    • Action Data = [Manufacturer];[ProductName]
    • Execution = "Deferred"
    • Condition = (REMOVE="ALL")
    • Location = Before File Installation/Uninstall (note: must be placed *after* script #2)
If anyone has recommendations or suggested improvements, please let me know.

Scott
Attachments
DateLastMod scripts.zip
Three scripts for deleting installed file only if it hasn't changed
(2.43 KiB) Downloaded 492 times
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: uninstall: do not remove file if it has changed

Hi Scott,

Thank you for your contribution. Perhaps other users will benefit from this guide.

Best regards,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/
shelmers
Posts: 63
Joined: Thu Sep 03, 2009 9:02 pm
Location: Andover, Massachusetts USA

Re: uninstall: do not remove file if it has changed

You're welcome. If there are simpler ways to accomplish any of these things, please let me know. Or if you find any errors or omissions, I'd like to know that too.

Return to “Common Problems”