Appending to the System PATH from batch file?

Namuna

Platinum Member
Jun 20, 2000
2,435
1
0
Hopefully this is an easy answer...

I want to append a path to the global system path, permanently.

I'm already familiar with using:
SET PATH=c:\whatever\here;%PATH%

But this only seems to set the path while that command window is open, how do I make this change apply to the system permanently?

Thanks!
 

Namuna

Platinum Member
Jun 20, 2000
2,435
1
0
Thanks for the reply, but as stated in the thread title I need to do it from a batch file.
 

skace

Lifer
Jan 23, 2001
14,488
7
81
use "reg update" from batch file to update registry variables. reboot will be required post editing path in registry.
 

Namuna

Platinum Member
Jun 20, 2000
2,435
1
0
reg update sounds like a plan...But how do you specify a variable in a reg update line?

What I mean is, I don't want to disturb whatever PATH the user already has, only want to append another path.

With the set command you can do the ;%PATH% at the end so it doesn't disturb what's already there...Is the same syntax allowed when doing 'reg update' ?

thanks.
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
The command line can't do what you are looking for without some outside utilities.
It might be easier in vbscript if you can use that. Save the following as a .vbs file



Sub AddToSystemPath (strnewPath)
Set WshShell = WScript.CreateObject("WScript.Shell")

On Error Resume Next

'Read the system path. Exit on errors.
strPath = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path")

If Err.Number <> 0 Then Exit Sub
If Instr(strPath, strnewPath) > 0 Then 'if the variable is already in the path then alert
MsgBox "Already in the path"
Else
Dim seperatorchar
If Right(strPath,1) =";" Then
seperatorchar = ""
Else
seperatorchar = ";"
End if
strPath = strPath & seperatorchar & strNewPath

WshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path", strPath, "REG_EXPAND_SZ"
If Err.Number = 0 Then
WScript.Echo "System path updated to:" & vbCrLf & strPath
Else
WScript.Echo "Path not updated - no updates necessary."
End If
End if
End Sub

'call to add the path to the system path
AddToSystemPath "C:\Temp"
 

skace

Lifer
Jan 23, 2001
14,488
7
81
Just figured I'd answer your question for the hell of it, even though KB has given another method. Yes %path% can be used anywhere on the dos level, which means from echo, a for command, or even reg update.

in windows XP this would be the proper command format:

reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d "c:\whatever\here;%path%" /f