Uninstall only installed files: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Adding new author and category links.)
Line 15: Line 15:
!macro File FilePath FileName
!macro File FilePath FileName
  !define FileID ${__LINE__}
  !define FileID ${__LINE__}
  IfFileExists "$OUTDIR\${FileName}" NoExtract_${FileID}
  IfFileExists "$OUTDIR\${FileName}" NoLog_${FileID}
   File "${FilePath}${FileName}"
   FileWrite $UninstLog "$OUTDIR\${FileName}$\r$\n"
  NoExtract_${FileID}:
  NoLog_${FileID}:
  FileWrite $UninstLog "$OUTDIR\${FileName}$\r$\n"
  File "${FilePath}${FileName}"
  !undef FileID
  !undef FileID
!macroend
!macroend

Revision as of 07:50, 3 April 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

!define UninstLog "uninstall.log"
Var UninstLog
 
; 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
 
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