Uninstall only installed files: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Updated by user: Afrow UK (talk, contrib).)
m (Updated by user: Afrow UK (talk, contrib).)
Line 77: Line 77:


  ; Can't uninstall if uninstall.log is missing!
  ; Can't uninstall if uninstall.log is missing!
  IfFileExists "$EXEDIR\${UninstLog}" +3
  IfFileExists "$INSTDIR\${UninstLog}" +3
   MessageBox MB_OK|MB_ICONSTOP "${UninstLog} not found!$\r$\n \
   MessageBox MB_OK|MB_ICONSTOP "${UninstLog} not found!$\r$\n \
   Uninstallation cannot proceed!"
   Uninstallation cannot proceed!"
Line 84: Line 84:
  Push $R0
  Push $R0
  SetFileAttributes "$INSTDIR\${UninstLog}" NORMAL
  SetFileAttributes "$INSTDIR\${UninstLog}" NORMAL
  FileOpen $UninstLog "$EXEDIR\${UninstLog}" r
  FileOpen $UninstLog "$INSTDIR\${UninstLog}" r


  LoopRead:
  LoopRead:
Line 102: Line 102:
  LoopDone:
  LoopDone:
  FileClose $UninstLog
  FileClose $UninstLog
  Delete "$EXEDIR\${UninstLog}"
  Delete "$INSTDIR\${UninstLog}"
  Pop $R0
  Pop $R0



Revision as of 11:43, 10 May 2005

Author: Afrow UK (talk, contrib)


Description

This a code written in the NSIS forums 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.").

Note: ${SetOutPath} must be used in reverse order of the directory tree, or else directories will not be removed from uninstall. For example:

${SetOutPath} "$INSTDIR\dir\subdir\anotherdir"
...
${SetOutPath} "$INSTDIR\dir\subdir"
...
${SetOutPath} "$INSTDIR\dir"

The Script

!define UninstLog "uninstall.log"
Var UninstLog
 
; Add file macro
!macro File FilePath FileName
 !define FileID ${__LINE__}
 IfFileExists "$OUTDIR\${FileName}" NoExtract_${FileID}
  File "${FilePath}${FileName}"
 NoExtract_${FileID}:
 FileWrite $UninstLog "$OUTDIR\${FileName}$\r$\n"
 !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
 
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
 
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 "${UninstLog} not found!$\r$\n \
  Uninstallation cannot proceed!"
   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 $R0  #is dir
   Goto LoopRead
    Delete $R0 #is file
 
  Goto LoopRead
 LoopDone:
 FileClose $UninstLog
 Delete "$INSTDIR\${UninstLog}"
 Pop $R0
 
SectionEnd

-Stu