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 5: Line 5:
== Description ==
== Description ==


This a code written in the NSIS forums for [[user:Rookie12|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.
This code was written for [[User:Rookie12|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 [http://nsis.sourceforge.net/Docs/AppendixC.html#C.2 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.").
You need the [http://nsis.sourceforge.net/Docs/AppendixC.html#C.2 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:
<highlight-nsis>
${SetOutPath} "$INSTDIR\dir\subdir\anotherdir"
...
${SetOutPath} "$INSTDIR\dir\subdir"
...
${SetOutPath} "$INSTDIR\dir"
</highlight-nsis>


== The Script ==
== The Script ==
Line 95: Line 85:
   Pop $R0
   Pop $R0
   IfFileExists "$R0\*.*" 0 +3
   IfFileExists "$R0\*.*" 0 +3
     RMDir $R0  #is dir
     RMDir /r $R0  #is dir
   Goto LoopRead
   Goto LoopRead
     Delete $R0 #is file
     Delete $R0 #is file
Line 107: Line 97:
SectionEnd
SectionEnd
</highlight-nsis>
</highlight-nsis>
-Stu
-Stu


[[{{ns:14}}:Code Examples]]
[[{{ns:14}}:Code Examples]]

Revision as of 16:19, 28 May 2005

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

!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 /r $R0  #is dir
   Goto LoopRead
    Delete $R0 #is file
 
  Goto LoopRead
 LoopDone:
 FileClose $UninstLog
 Delete "$INSTDIR\${UninstLog}"
 Pop $R0
 
SectionEnd

-Stu