3 * : Environmental Variables: append, prepend, and remove entries
5 * WARNING: If you use StrFunc.nsh header then include it before this file
6 * with all required definitions. This is to avoid conflicts
9 * ${EnvVarUpdate} "ResultVar" "EnvVarName" "Action" "RegLoc" "PathString"
13 * * Cal Turney (turnec2)
14 * * Amir Szekely (KiCHiK) and e-circ for developing the forerunners of this
15 * function: AddToPath, un.RemoveFromPath, AddToEnvVar, un.RemoveFromEnvVar,
16 * WriteEnvStr, and un.DeleteEnvStr
17 * * Diego Pedroso (deguix) for StrTok
18 * * Kevin English (kenglish_hi) for StrContains
19 * * Hendri Adriaens (Smile2Me), Diego Pedroso (deguix), and Dan Fuhry
20 * (dandaman32) for StrReplace
22 * Version 1.1 (compatibility with StrFunc.nsh)
25 * http://nsis.sourceforge.net/Environmental_Variables:_append%2C_prepend%2C_and_remove_entries
30 !ifndef ENVVARUPDATE_FUNCTION
31 !define ENVVARUPDATE_FUNCTION
34 !include "LogicLib.nsh"
35 !include "WinMessages.NSH"
36 !include "StrFunc.nsh"
38 ; ---- Fix for conflict if StrFunc.nsh is already includes in main file -----------------------
39 !macro _IncludeStrFunction StrFuncName
40 !ifndef ${StrFuncName}_INCLUDED
43 !ifndef Un${StrFuncName}_INCLUDED
46 !define un.${StrFuncName} "${Un${StrFuncName}}"
49 !insertmacro _IncludeStrFunction StrTok
50 !insertmacro _IncludeStrFunction StrStr
51 !insertmacro _IncludeStrFunction StrRep
53 ; ---------------------------------- Macro Definitions ----------------------------------------
54 !macro _EnvVarUpdateConstructor ResultVar EnvVarName Action Regloc PathString
62 !define EnvVarUpdate '!insertmacro "_EnvVarUpdateConstructor"'
64 !macro _unEnvVarUpdateConstructor ResultVar EnvVarName Action Regloc PathString
72 !define un.EnvVarUpdate '!insertmacro "_unEnvVarUpdateConstructor"'
73 ; ---------------------------------- Macro Definitions end-------------------------------------
75 ;----------------------------------- EnvVarUpdate start----------------------------------------
76 !define hklm_all_users 'HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"'
77 !define hkcu_current_user 'HKCU "Environment"'
79 !macro EnvVarUpdate UN
81 Function ${UN}EnvVarUpdate
100 -------------------------
101 $0 = ResultVar (returned)
102 $1 = EnvVarName (input)
105 $4 = PathString (input)
106 $5 = Orig EnvVar (read from registry)
107 $6 = Len of $0 (temp)
109 $8 = Entry counter (temp)
111 $R0 = tempChar (temp) */
113 ; Step 1: Read contents of EnvVarName from RegLoc
115 ; Check for empty EnvVarName
118 DetailPrint "ERROR: EnvVarName is blank"
119 Goto EnvVarUpdate_Restore_Vars
122 ; Check for valid Action
127 DetailPrint "ERROR: Invalid Action - must be A, P, or R"
128 Goto EnvVarUpdate_Restore_Vars
132 ReadRegStr $5 ${hklm_all_users} $1 ; Get EnvVarName from all users into $5
134 ReadRegStr $5 ${hkcu_current_user} $1 ; Read EnvVarName from current user into $5
137 DetailPrint 'ERROR: Action is [$3] but must be "HKLM" or HKCU"'
138 Goto EnvVarUpdate_Restore_Vars
141 ; Check for empty PathString
144 DetailPrint "ERROR: PathString is blank"
145 Goto EnvVarUpdate_Restore_Vars
148 ; Make sure we've got some work to do
152 DetailPrint "$1 is empty - Nothing to remove"
153 Goto EnvVarUpdate_Restore_Vars
156 ; Step 2: Scrub EnvVar
158 StrCpy $0 $5 ; Copy the contents to $0
159 ; Remove spaces around semicolons (NOTE: spaces before the 1st entry or
160 ; after the last one are not removed here but instead in Step 3)
161 ${If} $0 != "" ; If EnvVar is not empty ...
163 ${${UN}StrStr} $7 $0 " ;"
167 ${${UN}StrRep} $0 $0 " ;" ";" ; Remove '<space>;'
170 ${${UN}StrStr} $7 $0 "; "
174 ${${UN}StrRep} $0 $0 "; " ";" ; Remove ';<space>'
177 ${${UN}StrStr} $7 $0 ";;"
181 ${${UN}StrRep} $0 $0 ";;" ";"
184 ; Remove a leading or trailing semicolon from EnvVar
187 StrCpy $0 $0 "" 1 ; Change ';<EnvVar>' to '<EnvVar>'
193 StrCpy $0 $0 $6 ; Change ';<EnvVar>' to '<EnvVar>'
195 ; DetailPrint "Scrubbed $1: [$0]" ; Uncomment to debug
198 /* Step 3. Remove all instances of the target path/string (even if "A" or "P")
199 $6 = bool flag (1 = found and removed PathString)
200 $7 = a string (e.g. path) delimited by semicolon(s)
201 $8 = entry counter starting at 0
205 ${If} $5 != "" ; If EnvVar is not empty ...
212 ${${UN}StrTok} $7 $9 ";" $8 "0" ; $7 = next entry, $8 = entry counter
214 ${If} $7 == "" ; If we've run out of entries,
215 ${ExitDo} ; were done
218 ; Remove leading and trailing spaces from this entry (critical step for Action=Remove)
224 StrCpy $7 $7 "" 1 ; Remove leading space
231 StrCpy $7 $7 -1 ; Remove trailing space
233 ${If} $7 == $4 ; If string matches, remove it by not appending it
234 StrCpy $6 1 ; Set 'found' flag
235 ${ElseIf} $7 != $4 ; If string does NOT match
236 ${AndIf} $0 == "" ; and the 1st string being added to $0,
237 StrCpy $0 $7 ; copy it to $0 without a prepended semicolon
238 ${ElseIf} $7 != $4 ; If string does NOT match
239 ${AndIf} $0 != "" ; and this is NOT the 1st string to be added to $0,
240 StrCpy $0 $0;$7 ; append path to $0 with a prepended semicolon
243 IntOp $8 $8 + 1 ; Bump counter
244 ${Loop} ; Check for duplicates until we run out of paths
247 ; Step 4: Perform the requested Action
249 ${If} $2 != "R" ; If Append or Prepend
250 ${If} $6 == 1 ; And if we found the target
251 DetailPrint "Target is already present in $1. It will be removed and"
253 ${If} $0 == "" ; If EnvVar is (now) empty
254 StrCpy $0 $4 ; just copy PathString to EnvVar
255 ${If} $6 == 0 ; If found flag is either 0
256 ${OrIf} $6 == "" ; or blank (if EnvVarName is empty)
257 DetailPrint "$1 was empty and has been updated with the target"
259 ${ElseIf} $2 == "A" ; If Append (and EnvVar is not empty),
260 StrCpy $0 $0;$4 ; append PathString
262 DetailPrint "appended to $1"
264 DetailPrint "Target was appended to $1"
266 ${Else} ; If Prepend (and EnvVar is not empty),
267 StrCpy $0 $4;$0 ; prepend PathString
269 DetailPrint "prepended to $1"
271 DetailPrint "Target was prepended to $1"
274 ${Else} ; If Action = Remove
275 ${If} $6 == 1 ; and we found the target
276 DetailPrint "Target was found and removed from $1"
278 DetailPrint "Target was NOT found in $1 (nothing to remove)"
281 DetailPrint "$1 is now empty"
285 ; Step 5: Update the registry at RegLoc with the updated EnvVar and announce the change
289 WriteRegExpandStr ${hklm_all_users} $1 $0 ; Write it in all users section
291 WriteRegExpandStr ${hkcu_current_user} $1 $0 ; Write it to current user section
295 MessageBox MB_OK|MB_ICONEXCLAMATION "Could not write updated $1 to $3"
296 DetailPrint "Could not write updated $1 to $3"
297 Goto EnvVarUpdate_Restore_Vars
299 ; "Export" our change
300 SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
302 EnvVarUpdate_Restore_Vars:
304 ; Restore the user's variables and return ResultVar
315 Push $0 ; Push my $0 (ResultVar)
317 Pop $0 ; Restore his $0
321 !macroend ; EnvVarUpdate UN
322 !insertmacro EnvVarUpdate ""
323 !insertmacro EnvVarUpdate "un."
324 ;----------------------------------- EnvVarUpdate end----------------------------------------