Archive: Leave a directory behind on uninstall


Leave a directory behind on uninstall
So far these forums have been very useful, special thanks to all who have posted.

I tried searching for the answer to this one but couldn't quite find the perfect solution.

I want the user to have an option to save a certain directory called cache upon uninstall.

The Directory struction of the program is:

Cache
Configuration
Data
src
Utils
and then numerous dll's and other files in the default install directory.

Any ideas how I can preserve the cache directory if the user wants to?


For similar circumstances I have used something like this.

Var keepCache

Function un.onInit
MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Are you sure you want to uninstall ${PRODUCT_NAME}?" IDYES +2
Abort
MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON1 "Do you want to keep your cache folder?" IDNO +2
StrCpy $keepCache "Yes"
FunctionEnd

Section Uninstall
StrCmp $keepCache "Yes" +2
Delete "$INSTDIR\Cache\*.*"
Delete "$INSTDIR\Configuration\*.*"
Delete "$INSTDIR\Data\*.*"
Delete "$INSTDIR\src\*.*"
Delete "$INSTDIR\Utils\*.*"

StrCmp $keepCache "Yes" +2
RMDir "$INSTDIR\Cache"
RMDir "$INSTDIR\Configuration"
RMDir "$INSTDIR\Data"
RMDir "$INSTDIR\src"
RMDir "$INSTDIR\Utils"
SectionEnd
Have the StrCmp jump over the Deletes and RMDirs that should skipped.

Originally posted by scrose
For similar circumstances I have used something like this.

(snip)

Have the
StrCmp jump over the Deletes and RMDirs that should skipped.
Thank you, one additional question. How would I remove all the loose files, "DELETE $INSTDIR\*" ?

Yes, something like this.

Delete "$INSTDIR\*.*"
RMDir "$INSTDIR"
But be careful when using *.* with Delete; you might erase something you didn't instead to. If possible, it's much safer to have a separate Delete command for each file you want to uninstall. Same is true for RMDir.

Be careful with this:

  StrCmp $keepCache "Yes" +2
Delete "$INSTDIR\Cache\*.*"
Delete "$INSTDIR\Configuration\*.*"
Delete "$INSTDIR\Data\*.*"
Delete "$INSTDIR\src\*.*"
Delete "$INSTDIR\Utils\*.*"

StrCmp $keepCache "Yes" +2
RMDir "$INSTDIR\Cache"
RMDir "$INSTDIR\Configuration"
RMDir "$INSTDIR\Data"
RMDir "$INSTDIR\src"
RMDir "$INSTDIR\Utils"


Firstly you only need one StrCmp for the whole lot and secondly you're using +2 (only jumping over the first command!)
  StrCmp $keepCache "Yes" keepCache
Delete "$INSTDIR\Cache\*.*"
Delete "$INSTDIR\Configuration\*.*"
Delete "$INSTDIR\Data\*.*"
Delete "$INSTDIR\src\*.*"
Delete "$INSTDIR\Utils\*.*"

RMDir "$INSTDIR\Cache"
RMDir "$INSTDIR\Configuration"
RMDir "$INSTDIR\Data"
RMDir "$INSTDIR\src"
RMDir "$INSTDIR\Utils"
keepCache:


-Stu

Llynix only wanted to keep the Cache directory. That's why each StrCmp only jumped over one line.


Ah my mistake.

-Stu


the RMDir skip is not really needed (unless you want to keep the folder even if it's empty)