通过命令行添加系统路径环境变量?

时间:2014-04-15 07:43:41

标签: windows batch-file command-line cmd

我试图通过命令行向系统路径添加变量,但无法找到"系统路径"

的变量

如果我运行以下代码:

setx path "%path%;C:\Python27\;C:\Python27\Scripts\"

它将它添加到本地路径,而不是系统路径,所以我尝试了这样的事情:

setx syspath "%syspath%;C:\Python27\;C:\Python27\Scripts\"
setx systempath "%systempath%;C:\Python27\;C:\Python27\Scripts\"

除了能够编辑本地路径变量之外,我找不到位于http://technet.microsoft.com/en-us/library/cc755104.aspx的任何内容。

我没有找到通过GUI进行操作的方法,我只想通过代码(命令行希望)来实现它

在Autoit中我可以通过注册表编辑它:

$SystemPath = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "Path")
If StringRight($SystemPath, 1) = ";" Then
    RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "Path", "REG_SZ", $SystemPath & "C:\Python27\;C:\Python27\Scripts\")
Else
    RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "Path", "REG_SZ", $SystemPath & ";C:\Python27\;C:\Python27\Scripts\")
EndIf

或通过GUI

Run("SystemPropertiesAdvanced.exe")
WinWait("System Properties")
ControlClick("System Properties", "Enviro&nment Variables...", "[CLASS:Button; INSTANCE:7]")
WinWait("Environment Variables")
ControlListView("Environment Variables", "", "[CLASS:SysListView32; INSTANCE:2]", "Select", ControlListView("Environment Variables", "", "[CLASS:SysListView32; INSTANCE:2]", "FindItem", "Path"))
ControlClick("Environment Variables", "", "[CLASS:Button; INSTANCE:7]")
WinWait("Edit System Variable")
$SystemPath = ControlGetText("Edit System Variable", "", "[CLASS:Edit; INSTANCE:2]")

If StringRight($SystemPath, 1) = ";" Then
    ControlSetText("", "", "", $SystemPath & "C:\Python27\;C:\Python27\Scripts\")
Else
    ControlSetText("", "", "", $SystemPath & ";C:\Python27\;C:\Python27\Scripts\")
EndIf

ControlClick("Edit System Variable", "", "[CLASS:Button; INSTANCE:1]")
ControlClick("Environment Variables", "", "[CLASS:Button; INSTANCE:9]")
ControlClick("System Properties", "", "[CLASS:Button; INSTANCE:8]")

1 个答案:

答案 0 :(得分:2)

来自setx命令的linked documentation开关

  

/ m 指定在系统环境中设置变量。该   默认设置是本地环境。