Uninstall only installed files: Difference between revisions
From NSIS Wiki
Jump to navigationJump to search
m (Added category links.) |
|||
Line 4: | Line 4: | ||
<br style="clear:both;"> | <br style="clear:both;"> | ||
== 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 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. | ||
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 == | ||
<highlight-nsis> | <highlight-nsis> | ||
!define UninstLog "uninstall.log" | |||
Var UninstLog | Var UninstLog | ||
; Add file macro | ; Add file macro | ||
!macro File FileName | !macro File FilePath FileName | ||
File "${FileName}" | !define FileID ${__LINE__} | ||
IfFileExists "$OUTDIR\${FileName}" NoExtract_${FileID} | |||
File "${FilePath}${FileName}" | |||
NoExtract_${FileID}: | |||
FileWrite $UninstLog "$OUTDIR\${FileName}$\r$\n" | FileWrite $UninstLog "$OUTDIR\${FileName}$\r$\n" | ||
!undef FileID | |||
!macroend | !macroend | ||
!define File "!insertmacro File" | !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 | Section -openlogfile | ||
FileOpen $UninstLog "$INSTDIR\ | 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 | SectionEnd | ||
Section "Install Main" | Section "Install Main" | ||
SetOutPath $INSTDIR | ${SetOutPath} $INSTDIR | ||
${File} "file1.ext" | ${File} "dir1\" "file1.ext" | ||
${File} "file2.ext" | ${File} "dir1\" "file2.ext" | ||
${File} "file3.ext" | ${File} "dir1\" "file3.ext" | ||
SectionEnd | SectionEnd | ||
Line 32: | Line 62: | ||
Section "Install Other" | Section "Install Other" | ||
SetOutPath "$INSTDIR\Other" | ${SetOutPath} "$INSTDIR\Other" | ||
${File} "file4.ext" | ${File} "dir2\" "file4.ext" | ||
${File} "file5.ext" | ${File} "dir2\" "file5.ext" | ||
${File} "file6.ext" | ${File} "dir2\" "file6.ext" | ||
SectionEnd | SectionEnd | ||
Line 41: | Line 71: | ||
Section -closelogfile | Section -closelogfile | ||
FileClose $UninstLog | FileClose $UninstLog | ||
SetFileAttributes "$INSTDIR\${UninstLog}" READONLY|SYSTEM|HIDDEN | |||
SectionEnd | SectionEnd | ||
Line 46: | Line 77: | ||
; Can't uninstall if uninstall.log is missing! | ; Can't uninstall if uninstall.log is missing! | ||
IfFileExists "$EXEDIR\ | IfFileExists "$EXEDIR\${UninstLog}" +3 | ||
MessageBox MB_OK|MB_ICONSTOP " | MessageBox MB_OK|MB_ICONSTOP "${UninstLog} not found!$\r$\n \ | ||
Uninstallation cannot proceed!" | Uninstallation cannot proceed!" | ||
Abort | Abort | ||
Push $R0 | Push $R0 | ||
FileOpen $UninstLog "$EXEDIR\ | SetFileAttributes "$INSTDIR\${UninstLog}" NORMAL | ||
FileOpen $UninstLog "$EXEDIR\${UninstLog}" r | |||
LoopRead: | LoopRead: | ||
Line 62: | Line 94: | ||
Call un.TrimNewLines | Call un.TrimNewLines | ||
Pop $R0 | Pop $R0 | ||
Delete $R0 | IfFileExists "$R0\*.*" 0 +3 | ||
RMDir $R0 #is dir | |||
Goto LoopRead | |||
Delete $R0 #is file | |||
Goto LoopRead | Goto LoopRead | ||
LoopDone: | LoopDone: | ||
FileClose $UninstLog | FileClose $UninstLog | ||
Delete "$EXEDIR\${UninstLog}" | |||
Pop $R0 | Pop $R0 | ||
Revision as of 06:00, 8 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 "$EXEDIR\${UninstLog}" +3 MessageBox MB_OK|MB_ICONSTOP "${UninstLog} not found!$\r$\n \ Uninstallation cannot proceed!" Abort Push $R0 SetFileAttributes "$INSTDIR\${UninstLog}" NORMAL FileOpen $UninstLog "$EXEDIR\${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 "$EXEDIR\${UninstLog}" Pop $R0 SectionEnd
-Stu