Archive: Not able to delete ENV variable & its value


Not able to delete ENV variable & its value
Hi

Using installer i am successfully adding the ENV variable. but not able to delete it.
please let me know if anything chage is required in the following code to make this working fine.(giving parital code)

Var /GLOBAL AcsRoot

Function .onInit
StrCpy $AcsRoot "C:\Rajani\home\Administrator"
FunctionEnd
;-----in install----
Push "ACS_ROOT"
Push $AcsRoot
Call AddToEnvVar
;-----------------


;-------in uninstall--
Push "ACS_ROOT_DIR"
Push $AcsRootDir
Call un.RemoveFromEnvVar
;-------------------




Function un.RemoveFromEnvVar

Exch $1 ; $1 has environment variable value
Exch
Exch $0 ; $0 has environment variable name

DetailPrint "Removing $1 from $0"
Push $2
Push $3
Push $4
Push $5
Push $6
Push $7

IntFmt $7 "%c" 26 # DOS EOF
;DetailPrint "Print 10"
Call un.IsNT
Pop $2
StrCmp $2 1 unRemoveFromEnvVar_NT
; Not on NT
StrCpy $2 $WINDIR 2
;DetailPrint "Print 11"
FileOpen $2 "$2\autoexec.bat" r
GetTempFileName $5
FileOpen $3 $5 w
GetFullPathName /SHORT $1 $1
StrCpy $1 "SET $0=%$0%;$1"
Goto unRemoveFromEnvVar_dosLoop

unRemoveFromEnvVar_dosLoop:
;DetailPrint "Print 12"
FileRead $2 $4
StrCpy $6 $4 1 -1 # read last char
StrCmp $6 $7 0 +2 # if DOS EOF
StrCpy $4 $4 -1 # remove DOS EOF so we can compare
StrCmp $4 "$1$\r$\n" unRemoveFromEnvVar_dosLoopRemoveLine
StrCmp $4 "$1$\n" unRemoveFromEnvVar_dosLoopRemoveLine
StrCmp $4 "$1" unRemoveFromEnvVar_dosLoopRemoveLine
StrCmp $4 "" unRemoveFromEnvVar_dosLoopEnd
FileWrite $3 $4
Goto unRemoveFromEnvVar_dosLoop
unRemoveFromEnvVar_dosLoopRemoveLine:
;DetailPrint "Print 13"
SetRebootFlag true
Goto unRemoveFromEnvVar_dosLoop

unRemoveFromEnvVar_dosLoopEnd:
;DetailPrint "Print 14"
FileClose $3
FileClose $2
StrCpy $2 $WINDIR 2
Delete "$2\autoexec.bat"
CopyFiles /SILENT $5 "$2\autoexec.bat"
Delete $5
Goto unRemoveFromEnvVar_done

unRemoveFromEnvVar_NT:
;DetailPrint "Print 15"
ReadRegStr $2 HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" $0
;DetailPrint $2
StrCpy $6 $2 1 -1 # copy last char
;DetailPrint $6
StrCmp $6 ";" +2 # if last char != ;
StrCpy $2 "$2;" # append ;
Push $2
Push "$1;"
Call un.StrStr ; Find `$1;` in $2
Pop $3 ; pos of our dir
StrCmp $3 "" unRemoveFromEnvVar_done
; else, it is in path
# $1 - path to add
# $2 - path var
StrLen $4 "$1;"
StrLen $5 $3
StrCpy $6 $2 -$5 # $6 is now the part before the path to remove
StrCpy $7 $3 "" $4 # $7 is now the part after the path to remove
StrCpy $4 $6$7

StrCpy $6 $4 1 -1 # copy last char
StrCmp $6 ";" 0 +2 # if last char == ;
StrCpy $4 $4 -1 # remove last char

WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" $0 $4
;DetailPrint "Print 16"
; delete reg value if null
StrCmp $4 "" 0 +2 # if null delete reg
DeleteRegValue HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" $0

;SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000

unRemoveFromEnvVar_done:
;DetailPrint "Print 17"
Pop $7
Pop $6
Pop $5
Pop $4
Pop $3
Pop $2
Pop $1
Pop $0
FunctionEnd


Please use pastebin or an attached nsi file for sharing large amounts of code.


Try EnvVarUpdate.nsh

http://nsis.sourceforge.net/Environm...tries#Examples