Uninstall only installed files: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Updated by user: Afrow UK (talk, contrib).)
m (Adding new author and category links.)
Line 1: Line 1:
{|align=right
{{PageAuthor|Afrow UK}}
|<small>Author: [[{{ns:2}}:Afrow UK|Afrow UK]] ([[{{ns:3}}:Afrow UK|talk]], [[{{ns:-1}}:Contributions/Afrow UK|contrib]])</small>
 
|}
<br style="clear:both;">
== Description ==
== Description ==


Line 100: Line 98:
-Stu
-Stu


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

Revision as of 14:01, 24 June 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