Archive: Unistall only installed files


Unistall only installed files
can anybody write an exmaple script for me :(


This has already been asked before. You can only do this by creating a list of files on installation (write to a file using FileOpen, FileWrite) and then read from this file on uninstallation and delete each file.

e.g.

Var UninstLog

; Add file macro
!macro File FileName
File "${FileName}"
FileWrite $UninstLog "$OUTPATH\${FileName}$\r$\n"
!macroend
!define File "!insertmacro File"

Section -openlogfile
FileOpen $UninstLog "$INSTDIR\uninstall.log" w
SectionEnd

Section "Install Main"

SetOutPath $INSTDIR
${File} "file1.ext"
${File} "file2.ext"
${File} "file3.ext"

SectionEnd

Section "Install Other"

SetOutPath "$INSTDIR\Other"
${File} "file4.ext"
${File} "file5.ext"
${File} "file6.ext"

SectionEnd

Section -closelogfile
FileClose $UninstLog
SectionEnd

Section Uninstall

; Can't uninstall if uninstall.log is missing!
IfFileExists "$EXEDIR\uninstall.log" +3
MessageBox MB_OK|MB_ICONSTOP "uninstall.log not found!$\r$\nUninstallation cannot be done!"
Abort

Push $R0
FileOpen $UninstLog "$EXEDIR\uninstall.log" r

LoopRead:
ClearErrors
FileRead $UninstLog $R0
IfErrors LoopDone

Push $R0
Call TrimNewLines
Pop $R0
Delete $R0

Goto LoopRead
LoopDone:
FileClose $UninstLog
Pop $R0

SectionEnd


You will need the TrimNewLines function to be present also.

-Stu

how can i present TrimNewLines function ?


TrimNewlines is in the NSIS Users Manual

http://nsis.sourceforge.net/Docs/AppendixC.html#C.2


how can i use this in script?

sorry i'm just 12 :(


To use TrimNewlines, just insert the code into your script.

 Function TrimNewlines
Exch $R0
Push $R1
Push $R2
StrCpy $R1 0

loop:
IntOp $R1 $R1 - 1
StrCpy $R2 $R0 1 $R1
StrCmp $R2 "$\r" loop
StrCmp $R2 "$\n" loop
IntOp $R1 $R1 + 1
IntCmp $R1 0 no_trim_needed
StrCpy $R0 $R0 $R1

no_trim_needed:
Pop $R2
Pop $R1
Exch $R0
FunctionEnd
For example, insert it at the end of the code Afrow UK provided.

Call must be used with function names starting with "un." in the uninstall section.
Usage: Call function_name | [:label_name]
Error in script "I:\Documents and Settings\Administrator\Desktop\unistallonlyinstalled\example.nsi" on line 52 -- aborting creation process
error at "Call TrimNewLines"

i've used un.TrimNewLines and FileWrite $INSTDIR "$OUTPATH\${FileName}$\r$\n" so no more problem about making setup but now uninstall.log is empty


Script on Basic.nsi, but unistaller doesnt work.


Sorry I updated my code above after you must have copied it. I have made some alterations so you'll have to copy and paste it again.

-Stu


Sorry about the "un.TrimNewlines" problem - I did not look at the code to see where the TrimNewlines function was being used.

In your script the "FileOpen" command will fail to create the uninstall.log file if the $INSTDIR folder does not exist. One way to fix this is to change the -openlogfile section:

Section -openlogfile
SetOutPath $INSTDIR
FileOpen $UninstLog "$INSTDIR\uninstall.log" w
SectionEnd
Another problem is that the "File" macro uses $OUTPATH instead of $OUTDIR (you should have got some warnings about this when you compiled the script, eg unknown variable/constant "OUTPATH\file1.ext" detected, ignoring (macro:File:2)).

To fix this change the line to
FileWrite $UninstLog "$OUTDIR\${FileName}$\r$\n"
These two changes will let your installer create the uninstsall.log file with the correct data.

You'll need to add some code to the uninstaller to make it delete the uninstall.log file, the uninstall.exe program, the empty folders and the registry data (have another look at the original Basic.nsi script to see how to do this).

[edit]
Another thing that might cause problems is that the "File" macro can store the wrong information if you include a path instead of just a filename, e.g.

${File} "path\file7.ext"
[/edit]


Var UninstLog

; Add file macro
!macro File FileName
File "${FileName}"
FileWrite $UninstLog "$OUTPATH\${FileName}$\r$\n"
!macroend
!define File "!insertmacro File"

Section -openlogfile
FileOpen $UninstLog "$INSTDIR\uninstall.log" w
SectionEnd

Section "Install Main"

SetOutPath $INSTDIR
${File} "file1.ext"
${File} "file2.ext"
${File} "file3.ext"

SectionEnd

Section "Install Other"

SetOutPath "$INSTDIR\Other"
${File} "file4.ext"
${File} "file5.ext"
${File} "file6.ext"

SectionEnd

Section -closelogfile
FileClose $UninstLog
SectionEnd

Section Uninstall

; Can't uninstall if uninstall.log is missing!
IfFileExists "$INSTDIR\uninstall.log" +3
MessageBox MB_OK|MB_ICONSTOP "uninstall.log not found!$\r$\nUninstallation cannot be done!"
Abort

Push $R0
FileOpen $UninstLog "$INSTDIR\uninstall.log" r

LoopRead:
ClearErrors
FileRead $UninstLog $R0
IfErrors LoopDone

Push $R0
Call un.TrimNewLines
Pop $R0
Delete $R0

Goto LoopRead
LoopDone:
FileClose $UninstLog
Pop $R0

SectionEnd

Function un.TrimNewlines
Exch $R0
Push $R1
Push $R2
StrCpy $R1 0

loop:
IntOp $R1 $R1 - 1
StrCpy $R2 $R0 1 $R1
StrCmp $R2 "$\r" loop
StrCmp $R2 "$\n" loop
IntOp $R1 $R1 + 1
IntCmp $R1 0 no_trim_needed
StrCpy $R0 $R0 $R1

no_trim_needed:
Pop $R2
Pop $R1
Exch $R0
FunctionEnd


i've used this code, only problem with $OUTPATH

this is contents of uninstall.log

$OUTPATH\file1.ext
$OUTPATH\file2.ext
$OUTPATH\file3.ext
$OUTPATH\file4.ext
$OUTPATH\file5.ext
$OUTPATH\file6.ext

Originally posted by pengyou
Sorry about the "un.TrimNewlines" problem - I did not look at the code to see where the TrimNewlines function was being used.

In your script the "FileOpen" command will fail to create the uninstall.log file if the $INSTDIR folder does not exist. One way to fix this is to change the -openlogfile section:
Section -openlogfile
SetOutPath $INSTDIR
FileOpen $UninstLog "$INSTDIR\uninstall.log" w
SectionEnd
Another problem is that the "File" macro uses $OUTPATH instead of $OUTDIR (you should have got some warnings about this when you compiled the script, eg unknown variable/constant "OUTPATH\file1.ext" detected, ignoring (macro:File:2)).

To fix this change the line to
FileWrite $UninstLog "$OUTDIR\${FileName}$\r$\n"
These two changes will let your installer create the uninstsall.log file with the correct data.

You'll need to add some code to the uninstaller to make it delete the uninstall.log file, the uninstall.exe program, the empty folders and the registry data (have another look at the original Basic.nsi script to see how to do this).

[edit]
Another thing that might cause problems is that the "File" macro can store the wrong information if you include a path instead of just a filename, e.g.

${File} "path\file7.ext"
[/edit]
ok i've used $OUTDIR for $OUTPATH, so it works completely. Thanx for your helps :)

Ah yes sorry about that. I meant to put $OUTDIR but I put $OUTPATH instead sorry :D

I never actually tested the code (although I should have). It was written here and then.

-Stu


is this possible for registry strings too?