Jump to content

Append to %PATH%


Uncle_Gadget

Recommended Posts

Once again I'm stuck on an inf problem. Does anyone know of a way to append to the %PATH% using an INF installer?

I know that the path is stored in the registry at:

HKLM,"SYSTEM\CurrentControlSet\Control\Session Manager\Environment","Path"

And I know it would be easy to replace the path. The question is can I append to the existing path in the registry?

And, then, to really complicate matters... Would there be a way to uninstall the appended path portion ONLY on an uninstall? I'm probably asking for too much, now. :albert:

Edited by Uncle_Gadget
Link to comment
Share on other sites

Ive struggled with this too, I can get append (0x10008) to add but never could figure out the uninstall

Thanks. Even this helps. I'll probably write a simple AutoIt executable and execute it during RunPostSetupCommands=

Link to comment
Share on other sites

I was doing a bit more research and found the Registry Data Types below. Is there a difference between NOCLOBBER and APPEND?

; REG_MULTI_SZ_APPEND = 0x0001000A
; REG_MULTI_SZ_NOCLOBBER = 0x00010002

I know it doesn't help me with a REG_EXPAND_SZ, but wouldn't the line below work as an uninstall data type for REG_MULTI_SZ?

; REG_MULTI_SZ_DELVAL = 0x00010006

Link to comment
Share on other sites

If anyone's interested, I wrote a fairly sophisticated AutoIt script to add/remove to/from %PATH% using the registry.

If you run it silently, it will generate an ERRORLEVEL if it encounters any errors while executing. Feel free to use or modify as needed.


#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=C:\Users\Public\Desktop\Uninstall.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Res_Description=Append or Remove elements from %PATH%
#AutoIt3Wrapper_Res_Fileversion=1.0.0.16
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=p
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator
#AutoIt3Wrapper_Res_Field=ProductName|PATH Appender-Remover
#AutoIt3Wrapper_Res_Field=ProductVersion|1.0
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y
#AutoIt3Wrapper_Run_Tidy=y
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.8.1
Author: Uncle Gadget
Script Function:
This script will appends or remove elements from the %PATH% Environment
#ce ----------------------------------------------------------------------------
Local $Silent, $NewRegPath

Global $CommandError = "Uninstall.exe <a/r/as/rs> <path to append/remove>" & @LF & @LF & _
" a = Append to existing %PATH%" & @LF & _
" r = Remove from existing %PATH%" & @LF & @LF & _
" as = Append (silently) to existing %PATH%" & @LF & _
" rs = Remove (silently) from existing %PATH%"


If $CmdLine[0] <> 2 Then; not enough Command Line parameters were entered
MsgBox(262160, "Command Line Error (1)", "Uninstall.exe expects parameters to be as follows:" & @LF & @LF & _
$CommandError)
Exit 1
EndIf

Global $Path = StringStripWS($CmdLine[2], 3) ; Strip all White Space from Begin and End of Command Line (2)

Global $Command = StringLower(StringStripWS($CmdLine[1], 3))

Local $RegPath = RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "Path")

Switch $Command
Case "as" ; (a)ppend / (s)ilent
$Silent = 1 ; Turns on the Silent (No Output) Flag to eliminate any user output
ContinueCase ; Continue on to Append below


Case "a" ; (a)ppend
Opt("ExpandEnvStrings", 1) ; Turns ON Expanded Environment Strings
If Not FileExists($Path) Then ; the requested Path to Append/Remove does not exist - ERROR
If Not $Silent Then ; we'll let User know about error if 's'ilent was NOT specified
MsgBox(262160, "Path Does Not Exist (2)", "The Path to append (below) does not currently exist." & @LF & @LF & _
$Path & @LF & @LF & _
"Verify that Path exists prior to execution.")
EndIf
Exit 1
EndIf

$LowerPath = StringLower($Path)
$LowerRegPath = StringLower($RegPath)
$ExpPathLen = StringLen(";" & $Path)
Opt("ExpandEnvStrings", 0) ; Turns OFF Expanded Environment Strings

If $LowerPath & ";" = StringLeft($LowerRegPath, $ExpPathLen) Or StringInStr($LowerRegPath, ";" & $LowerPath & ";") Or ";" & $LowerPath = StringRight($LowerRegPath, $ExpPathLen) Then ; the path to remove is currently somewhere in %PATH%
If Not $Silent Then ; we'll let User know that Path to Append currently exists in %PATH%
MsgBox(262208, "Path Currently Exists (3)", "The Path to append (below) is currently specified in %PATH%." & @LF & @LF & _
$Path & @LF & @LF & _
"No changes will be made to the %PATH%.")
EndIf
Exit 0 ; Although no changes were necessary we'll exit with no error since our mission WAS accomplished
EndIf

$AppendWrite = RegWrite("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "Path", "REG_EXPAND_SZ", $RegPath & ";" & $Path)
If Not $AppendWrite Then ; some sort of error occurred while writing
If Not $Silent Then ; we'll let User know about error if 's'ilent was NOT specified
MsgBox(262160, "Registry Write Error (4)", "Unable to write appended %PATH% to the registry." & @LF & @LF & _
"Error Code Returned was: " & @error & @LF & @LF & _
"Review command line options before trying again.")
EndIf
Exit 1
EndIf

If Not $Silent Then ; we'll let the User know that all changes have been made as requested.
MsgBox(262208, "Path Successfully Appended", "The path (below) was successfully appended to %PATH%." & @LF & @LF & _
$Path)
EndIf

EnvUpdate()

Exit 0


Case "rs" ; ®emove / (s)ilent
$Silent = 1 ; Turns on the Silent (No Output) Flag to eliminate any user output
ContinueCase ; Continue on to Remove below


Case "r" ; ®emove
$PathLen = StringLen(";" & $Path)

If $Path & ";" = StringLeft($RegPath, $PathLen) Then ; the path to remove is at the BEGINNING of %PATH%
$NewRegPath = StringTrimLeft($RegPath, $PathLen) ; and we'll create the new %PATH% String by Trimming of the beginning of %PATH%

ElseIf StringInStr($RegPath, ";" & $Path & ";") Then ; the path to remove is in the MIDDLE of %PATH%
$NewRegPath = StringReplace($RegPath, ";" & $Path & ";", ";") ; and we'll create the new %PATH% String

ElseIf ";" & $Path = StringRight($RegPath, $PathLen) Then ; the path to remove is at END of %PATH%
$NewRegPath = StringTrimRight($RegPath, $PathLen) ; and we'll create the new %PATH% String by trimming off the end of %PATH%

Else ; the path to remove is NOT in current %PATH% and we can't make any changes
If Not $Silent Then ; we'll let User know about error if 's'ilent was NOT specified
MsgBox(262160, "Path Removal Error(5)", "The Path specified for removal (below) does not exist in current %PATH%." & @LF & @LF & _
$Path & @LF & @LF & _
"Review command line options before trying again.")
EndIf
Exit 1
EndIf

$RemoveWrite = RegWrite("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "Path", "REG_EXPAND_SZ", $NewRegPath)

If Not $RemoveWrite Then ; some sort of error occurred while writing
If Not $Silent Then ; we'll let User know about error if 's'ilent was NOT specified
MsgBox(262160, "Registry Write Error (6)", "Unable to write removed %PATH% to the registry." & @LF & @LF & _
"Error Code Returned was: " & @error & @LF & @LF & _
"Review command line options before trying again.")
EndIf
Exit 1
EndIf

If Not $Silent Then ; we'll let the User know that all changes have been made as requested.
MsgBox(262208, "Path Successfully Removed", "The path (below) was successfully removed from %PATH%." & @LF & @LF & _
$Path)
EndIf

EnvUpdate()

Exit 0

Case Else ; an invalid parameter was entered
MsgBox(262160, "Invalid Command option (7)", "Uninstall expects parameters to be as follows:" & @LF & @LF & _
$CommandError)
Exit 1
EndSwitch

Uninstall.7z

Edited by Uncle_Gadget
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...