Uninstall only installed files: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
Line 7: Line 7:


== The Script ==
== The Script ==
<p>'''This goes before all your own sections in your script...'''</p>


<highlight-nsis>
<highlight-nsis>
!define UninstLog "uninstall.log"
!define UninstLog "uninstall.log"
Var UninstLog
Var UninstLog
; Uninstall log file missing.
LangString UninstLogMissing ${LANG_ENGLISH} "${UninstLog} not found!$\r$\nUninstallation cannot proceed!"


; Add file macro
; Add file macro
Line 37: Line 42:
   FileOpen $UninstLog "$INSTDIR\${UninstLog}" a
   FileOpen $UninstLog "$INSTDIR\${UninstLog}" a
   FileSeek $UninstLog 0 END
   FileSeek $UninstLog 0 END
SectionEnd
SectionEnd</highlight-nsis>
 
<p>Your own sections need to be placed here in your script.<br />
Instead of using SetOutPath and File instructions in your sections, use ${SetOutPath} and ${File} instead.</p>
 
<p>'''This is an example of what your sections may now look like...'''</p>


Section "Install Main"
<highlight-nsis>Section "Install Main"


  ${SetOutPath} $INSTDIR
  ${SetOutPath} $INSTDIR
Line 55: Line 65:
  ${File} "dir2\" "file6.ext"
  ${File} "dir2\" "file6.ext"


SectionEnd
SectionEnd</highlight-nsis>
 
<p>'''This comes after all your own sections in your script...'''</p>


Section -closelogfile
<highlight-nsis>Section -closelogfile
  FileClose $UninstLog
  FileClose $UninstLog
  SetFileAttributes "$INSTDIR\${UninstLog}" READONLY|SYSTEM|HIDDEN
  SetFileAttributes "$INSTDIR\${UninstLog}" READONLY|SYSTEM|HIDDEN
Line 64: Line 76:
Section Uninstall
Section Uninstall


  ; Can't uninstall if uninstall.log is missing!
  ; Can't uninstall if uninstall log is missing!
  IfFileExists "$INSTDIR\${UninstLog}" +3
  IfFileExists "$INSTDIR\${UninstLog}" +3
   MessageBox MB_OK|MB_ICONSTOP "${UninstLog} not found!$\r$\n \
   MessageBox MB_OK|MB_ICONSTOP "$(UninstLogMissing)"
  Uninstallation cannot proceed!"
   Abort
   Abort



Revision as of 09:13, 12 May 2006

Author: Afrow UK (talk, contrib)


Description

This code was written for Rookie12 a long while back. So many people have asked the same question since, so I thought that it was a good idea to put it up on here. You need the TrimNewLines function present in your script also. Make sure you rename the TrimNewLines function name to un.TrimNewLines (as the function is used in the uninstaller, ala "un.").

The Script

This goes before all your own sections in your script...

!define UninstLog "uninstall.log"
Var UninstLog
 
; Uninstall log file missing.
LangString UninstLogMissing ${LANG_ENGLISH} "${UninstLog} not found!$\r$\nUninstallation cannot proceed!"
 
; Add file macro
!macro File FilePath FileName
 !define FileID ${__LINE__}
 IfFileExists "$OUTDIR\${FileName}" NoLog_${FileID}
  FileWrite $UninstLog "$OUTDIR\${FileName}$\r$\n"
 NoLog_${FileID}:
 File "${FilePath}${FileName}"
 !undef FileID
!macroend
!define File "!insertmacro File"
 
; Set output path macro
!macro SetOutPath Path
 SetOutPath "${Path}"
 FileWrite $UninstLog "${PATH}$\r$\n"
!macroend
!define SetOutPath "!insertmacro SetOutPath"
 
Section -openlogfile
 IfFileExists "$INSTDIR\${UninstLog}" +3
  FileOpen $UninstLog "$INSTDIR\${UninstLog}" w
 Goto +4
  SetFileAttributes "$INSTDIR\${UninstLog}" NORMAL
  FileOpen $UninstLog "$INSTDIR\${UninstLog}" a
  FileSeek $UninstLog 0 END
SectionEnd

Your own sections need to be placed here in your script.
Instead of using SetOutPath and File instructions in your sections, use ${SetOutPath} and ${File} instead.

This is an example of what your sections may now look like...

Section "Install Main"
 
 ${SetOutPath} $INSTDIR
 ${File} "dir1\" "file1.ext"
 ${File} "dir1\" "file2.ext"
 ${File} "dir1\" "file3.ext"
 
SectionEnd
 
Section "Install Other"
 
 ${SetOutPath} "$INSTDIR\Other"
 ${File} "dir2\" "file4.ext"
 ${File} "dir2\" "file5.ext"
 ${File} "dir2\" "file6.ext"
 
SectionEnd

This comes after all your own sections in your script...

Section -closelogfile
 FileClose $UninstLog
 SetFileAttributes "$INSTDIR\${UninstLog}" READONLY|SYSTEM|HIDDEN
SectionEnd
 
Section Uninstall
 
 ; Can't uninstall if uninstall log is missing!
 IfFileExists "$INSTDIR\${UninstLog}" +3
  MessageBox MB_OK|MB_ICONSTOP "$(UninstLogMissing)"
   Abort
 
 Push $R0
 SetFileAttributes "$INSTDIR\${UninstLog}" NORMAL
 FileOpen $UninstLog "$INSTDIR\${UninstLog}" r
 
 LoopRead:
  ClearErrors
   FileRead $UninstLog $R0
   IfErrors LoopDone
 
   Push $R0
    Call un.TrimNewLines
   Pop $R0
   IfFileExists "$R0\*.*" 0 +3
    RMDir /r $R0  #is dir
   Goto LoopRead
    Delete $R0 #is file
 
  Goto LoopRead
 LoopDone:
 FileClose $UninstLog
 Delete "$INSTDIR\${UninstLog}"
 Pop $R0
 
SectionEnd

-Stu