ViciousXUSMC

Deleting System Environment Path Variable Values

8 posts in this topic

#1 ·  Posted (edited)

I have a working code, but I wanted to see if this is a good way or a way too complicated way with something more simple being possible.

Here is how I append (add) new values to the System Path Variable:

;Appends Oracle to the System Path Varibles
$key = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
$val = "PATH"
$PATH = RegRead($key, $val)

$sAddThisPath = "C:\oracle\ora92\network\admin"
$PATH = $PATH & ";" & $sAddThisPath

RegWrite($key,$val,"REG_EXPAND_SZ",$PATH)
EnvUpdate()

 

Now to do an update I need to write something to non destructively remove an entry in the path variable.  This is what I came up with and I am looking for the nod that this was a good way to handle it or for others to post up their version of a working script.

 

#Include <Array.au3>

;Read Current System Path Varible
$sSystemPath =  RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "path")
;Place String into an Array
$aSystemPath = StringSplit($sSystemPath, ";")
;Debug Show Array Results
;_ArrayDisplay($aSystemPath)

;Declare Indexes to Delete Varible
$vToDelete = ""

For $i = 1 to $aSystemPath[0]
    ;What String we are searching for
    If StringInStr($aSystemPath[$i], "oracle") Then
        ;save matching array indexes to a varible
        $vToDelete &= $i & ";"
    EndIf
Next

;Trim extra semicolon off our varible
$vToDelete = StringTrimRight($vToDelete, 1)
;Debug Display Matching Indexes to be Deleted in a MsgBox
;MsgBox(0, "", $vToDelete)
;Delete Indexes from the Array
_ArrayDelete($aSystemPath, $vToDelete)

;Debug Show New Array
;_ArrayDisplay($aSystemPath)
;Convert Array back to a String To Be Written to Registry
$sNewSystemPath = _ArrayToString($aSystemPath, ";", 1)
;Debug Look at the Strings Value with MsgBox
;MsgBox(0, "", $sNewSystemPath)

;Insert RegWrite() As Needed

And without all the commenting and debug stuff I put in there it would be as simple as:

#Include <Array.au3>

$sSystemPath =  RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "path")
$aSystemPath = StringSplit($sSystemPath, ";")
$vToDelete = ""

For $i = 1 to $aSystemPath[0]
    If StringInStr($aSystemPath[$i], "oracle") Then
        $vToDelete &= $i & ";"
    EndIf
Next

$vToDelete = StringTrimRight($vToDelete, 1)
_ArrayDelete($aSystemPath, $vToDelete)
$sNewSystemPath = _ArrayToString($aSystemPath, ";", 1)


;Insert RegWrite() As Needed

 

Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites



Edit: Saw no reason to go from Array to String at the end when I can write the string in my  loop.

This feels a bit more efficent, but I did like the idea of being able to see the Array first before committing changes if I keep that as part of the script. 

 

#Include <Array.au3>

$sSystemPath =  RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "path")
$aSystemPath = StringSplit($sSystemPath, ";")
$sNewSystemPath = ""

For $i = 1 to $aSystemPath[0]   
    If NOT StringInStr($aSystemPath[$i], "oracle") Then     
        $sNewSystemPath &= $aSystemPath[$i] & ";"
    EndIf
Next

$sNewSystemPathe = StringTrimRight($sNewSystemPath, 1)

;Insert RegWrite() As Needed

 

Share this post


Link to post
Share on other sites

Just a friendly bump since it has been a couple of days to see if anybody wants to offer up alternative or better scripting ideas to pull off this task. 

I am going to start writing my "uninstall" script today and want to make sure I have some good solid code before I put it all together.

Share this post


Link to post
Share on other sites

I was looking for something like this the other day for updating the PSModulePath in the AutoIt installer. 

Remember to call EnvUpdate() after making the change so that it takes effect without a reboot.

Share this post


Link to post
Share on other sites

Here are my two functions :

; iFlag possible combinations :
;  - 0  : add the sText to the beginning (default)
;  - 1  : add the sText to the end
;  - 2  : add the sText even if it is already present
Func _AddToPath($sText, $iFlag = 0)
    If NOT IsAdmin() Then Return SetError(4, 0, 0)
    Local $sPath  = RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "path")
    If NOT BitAND($iFlag, 2) AND StringRegExp($sPath, "(?i)(?:^|;)\Q" & $sText & "\E(?:;|$)") Then Return SetError(5, 0, 1)
    Local $sNewPath = $sText & ";" & $sPath
    If BitAND($iFlag, 1) Then $sNewPath = $sPath & ";" & $sText
    RegWrite("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "path2", "REG_EXPAND_SZ", $sNewPath)
    If @error Then Return SetError(@error, 0, 0)
    EnvUpdate()
    Return 1
EndFunc

; iFlag = 0  :  match exactly the text (default)
; iFlag = 1  :  match part of the text
Func _RemoveFromPath($sText, $iFlag = 0)
    If NOT IsAdmin() Then Return SetError(4, 0, 0)
    Local $sPattern = "\Q" & $sText & "\E*"
    If $iFlag Then $sPattern = "[^;]*" & $sPattern & "[^;]*"
    Local $sPath  = RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "path")
    Local $sNewPath  = StringRegExpReplace($sPath, "(?i)(?<=^|;)" & $sPattern & "(?:;|$)", "")
    Local $iExtended = @extended
    If $iExtended Then 
        RegWrite("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "path2", "REG_EXPAND_SZ", $sNewPath)
        If @error Then Return SetError(@error, 0, 0)
        EnvUpdate()
    EndIf
    ConsoleWrite($sNewPath)
    Return SetExtended($iExtended, 1)
EndFunc

 

1 person likes this

Share this post


Link to post
Share on other sites

Heh figures you would pull out RegEX the one thing that still blows my mind.

I'll probably save those functions as an include and keep them handy.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I ran into a little issue today.

I put this in my uninstall script for some Oracle Database Client cleanup.  

Well heh, Java uses "oracle" too so using that is not such a good idea.  Is there an easy way to add "If this string but not if also this string" using your function jguinch?

With my method I would just use something to this effect.

#Include <Array.au3>

$sSystemPath =  RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "path")
$aSystemPath = StringSplit($sSystemPath, ";")
$sNewSystemPath = ""

For $i = 1 to $aSystemPath[0]
    If NOT StringInStr($aSystemPath[$i], "oracle") OR StringInStr($aSystemPath[$i], "java") Then
        $sNewSystemPath &= $aSystemPath[$i] & ";"
    EndIf
Next
$sNewSystemPath = StringTrimRight($sNewSystemPath, 1)
RegWrite("HKLM\SYSTEM\CurrentControlSet\Session Manager\Environment", "path", "REG_EXPAND_SZ", $sNewSystemPath)

I think that would work and can be my fallback, but since your function is so much more professional, I would tweak it if possible. 

Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites

Try this one, but be careful at what you do with the mode 2 (I think it's not a good idea to use this mode, but this code should answer to your question...)

_RemoveFromPath("[^;]+oracle(?![^;]+(?:java|mysql)).+", 2)

; iFlag = 0  :  match exactly the text (default)
; iFlag = 1  :  match part of the text
; iFlag = 2  :  match a regex (advanced)
Func _RemoveFromPath($sText, $iFlag = 0)
    If NOT IsAdmin() Then Return SetError(4, 0, 0)
    Local $sPattern = "\Q" & $sText & "\E*"
    If $iFlag = 1 Then
        $sPattern = "[^;]*" & $sPattern & "[^;]*"
    ElseIf $iFlag = 2 Then
        $sPattern = "(?U)" & $sText
    EndIf
    Local $sPath  = RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "path")
    Local $sNewPath  = StringRegExpReplace($sPath, "(?i)(?<=^|;)" & $sPattern & "(?:;|$)", "")
    Local $iExtended = @extended
    If $iExtended Then 
        RegWrite("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "path2", "REG_EXPAND_SZ", $sNewPath)
        If @error Then Return SetError(@error, 0, 0)
        EnvUpdate()
    EndIf
    ConsoleWrite($sNewPath)
    Return SetExtended($iExtended, 1)
EndFunc

Note that in my code I write the path2 value (to prevent a big mistake coming my fault:sweating:)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now