Jump to content

Add to PATH via Registry


Recommended Posts

I want to append to the PATH, I'm close but just can't figure this part out... I found some of this code in another topic

; Append to PATH

Func _ToolKit()

$sSep = ";"

$sType = "REG_SZ"

$path = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "PATH")

If @extended = 7 Then

$sSep = @LF

$sType = "REG_MULTI_SZ"

EndIf

$path1 = $sSep & "c:\program files\mytools"

$path2 = $path & $path1

RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", $path2)

If @error <> 0 Then RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "PATH", $sType, $path2)

EndFunc ;==>_ToolKit

Link to comment
Share on other sites

I want to append to the PATH, I'm close but just can't figure this part out... I found some of this code in another topic:

; Append to PATH
Func _ToolKit()
    $sSep = ";"
    $sType = "REG_SZ"
    $path = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "PATH")
    If @extended = 7 Then
        $sSep = @LF
        $sType = "REG_MULTI_SZ"
    EndIf
    $path1 = $sSep & "c:\program files\mytools"
    $path2 = $path & $path1
    RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", $path2)
    If @error <> 0 Then RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "PATH", $sType, $path2)
EndFunc  ;==>_ToolKit
What was the point of the second RegRead() and error checking before RegWrite()?

Also, I don't think PATH will ever be a REG_MULTI_SZ type, so that check is probably a waste of time and effort.

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Modified function (below), use it like this:

_Append2Path "C:\Program Files\Another Path"

Func _Append2Path($Path2Append)
Const $RegPath = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Local $sSep = ";", $sType = "REG_SZ", $nPath
Local $oPath = RegRead($RegPath, "PATH")
    If @extended = 7 Then
       $sSep = @LF
       $sType = "REG_MULTI_SZ"
    EndIf
        $nPath = $oPath & $sSep & $Path2Append
    RegWrite($RegPath, "PATH", $sType, $nPath)
EndFunc

You are passing the Path you want appended to the Function as a parameter

The function reads the existing path from the Registry,

If the @EXTENDED macro (set by the call to RegRead) is 7, then type of the value and separator variable are changed.

The new Path variable is build using the separator and written back to the registry.

Link to comment
Share on other sites

Modified function (below), use it like this:

_Append2Path "C:\Program Files\Another Path"

Func _Append2Path($Path2Append)
Const $RegPath = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Local $sSep = ";", $sType = "REG_SZ", $nPath
Local $oPath = RegRead($RegPath, "PATH")
    If @extended = 7 Then
       $sSep = @LF
       $sType = "REG_MULTI_SZ"
    EndIf
        $nPath = $oPath & $sSep & $Path2Append
    RegWrite($RegPath, "PATH", $sType, $nPath)
EndFunc

You are passing the Path you want appended to the Function as a parameter

The function reads the existing path from the Registry,

If the @EXTENDED macro (set by the call to RegRead) is 7, then type of the value and separator variable are changed.

The new Path variable is build using the separator and written back to the registry.

But when is PATH ever going to be a REG_MULTI_SZ type? I think that whole check can safely go away.

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

If I re-run the script it keeps appending to the PATH, thanks for your help but how do I RegRead before appending to see if the value is already in the string?

something like this?

If RegRead($RegPath & $Path2Append) Then @error <> 0 Then RegWrite ($RegPath, "PATH", $sType, $nPath)

Link to comment
Share on other sites

Use this:

Func _Append2Path($Path2Append)
Const $RegPath = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Local $sSep = ";", $sType = "REG_SZ", $nPath
Local $oPath = RegRead($RegPath, "PATH")
    If @extended = 7 Then
       $sSep = @LF
       $sType = "REG_MULTI_SZ"
    EndIf
    If StringInStr($oPath, $Path2Append) < 1 Then 
       $nPath = $oPath & $sSep & $Path2Append
       RegWrite($RegPath, "PATH", $sType, $nPath)
    EndIf
EndFunc
Link to comment
Share on other sites

Use this:

Func _Append2Path($Path2Append)
Const $RegPath = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Local $sSep = ";", $sType = "REG_SZ", $nPath
Local $oPath = RegRead($RegPath, "PATH")
    If @extended = 7 Then
       $sSep = @LF
       $sType = "REG_MULTI_SZ"
    EndIf
    If StringInStr($oPath, $Path2Append) < 1 Then 
       $nPath = $oPath & $sSep & $Path2Append
       RegWrite($RegPath, "PATH", $sType, $nPath)
    EndIf
EndFunc
Thanks DaRam you are DaMan! That worked perfectly!
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...