简体   繁体   English

如何在不使用“我的电脑”对话框的情况下在Windows XP中设置系统环境变量

[英]How do I set a system environment variable in Windows XP without using the “My Computer” Dialog

I'm switching between different Java SDKs (1.4.2, 1.5.0 and 1.6.0) for various projects. 我正在为各种项目切换不同的Java SDK(1.4.2,1.5.0和1.6.0)。 I would like to set the JAVA_HOME environment variable on my Windows XP machine without going through the tedious My Computer -> Advanced -> [Select System Variable] -> Edit -> Ok -> Ok 我想在我的Windows XP机器上设置JAVA_HOME环境变量,而无需通过繁琐的我的电脑 - >高级 - > [选择系统变量] - >编辑 - >确定 - >确定

Is it possible to do this from the command line, or is there a utility that can do this? 是否可以从命令行执行此操作,或者是否有可以执行此操作的实用程序?

(Note that I am not referring to the standard batch file "SET" command - I want to set the environment variable "globally," not just for the life of a console window). (请注意,我不是指标准批处理文件“SET”命令 - 我想要“全局”设置环境变量,而不仅仅是控制台窗口的生命周期)。

From http://vlaurie.com/computers2/Articles/environment.htm : 来自http://vlaurie.com/computers2/Articles/environment.htm

Using the add-on tool Setx.exe 使用附加工具Setx.exe

It is not part of the standard Windows XP setup but a command-line tool called setx.exe is included in the Windows XP Service Pack 2 Support Tools . 它不是标准Windows XP安装程序的一部分,但Windows XP Service Pack 2支持工具中包含一个名为setx.exe的命令行工具 This tool extends the set command so that permanent changes in the environment variables can be made. 此工具扩展了set命令,以便可以对环境变量进行永久性更改。 For example, to add a folder C:\\New Folder to the path, the command would be setx path "%PATH%;C:\\New Folder" 例如,要将文件夹C:\\ New Folder添加到路径,该命令将是setx path“%PATH%; C:\\ New Folder”

Service Pack 2 Support Tools has a tool called "setx.exe" that can do what you are looking for. Service Pack 2支持工具有一个名为“setx.exe”的工具,可以满足您的需求。 setx path "%PATH%;C:\\New Folder" setx路径“%PATH%; C:\\ New Folder”

Source 资源

Here's some VBScript I use for this: 这是我用于此的一些VBScript:

set args = WScript.Arguments
Set objShell = WScript.CreateObject("WScript.Shell")
Set colSystemEnvVars = objShell.Environment("System")
Set colUserEnvVars = objShell.Environment("User")

' Parse args
select case args.Count
case 0, 1, 2
    help
case 3
    sVariable = args(0)
    sValue = args(1)
    sScope = UCase(args(2))
    sMode = ""
case 4
    sVariable = args(0)
    sValue = args(1)
    sScope = UCase(args(2))
    sMode = UCase(args(3))
end select

select case sScope
    case "S"
        if sMode = "A" then
            sValue = colSystemEnvVars(sVariable) & sValue
        end if
        colSystemEnvVars(sVariable) = sValue
    case "U"
        if sMode = "A" then
            sValue = colUserEnvVars(sVariable) & sValue
        end if
        colUserEnvVars(sVariable) = sValue
    case else
        help
end select

WScript.Quit

'******************************************************************************
Sub help()
    WScript.Echo ""
    WScript.Echo "Create or update an environment variable."
    WScript.Echo ""
    WScript.Echo "usage:"
    WScript.Echo "======"  
    WScript.Echo "cscript SetVar.vbs variable value {S|U} [A]"
    WScript.Echo ""
    WScript.Echo "eg:"
    WScript.Echo "==="     
    WScript.Echo "cscript SetVar.vbs MYVAR 'Hello world' U"
    WScript.Echo "cscript SetVar.vbs PATH 'C:\MyPath' S A"

    WScript.Quit
End Sub   

The scope can be 'S'ystem or 'U'ser. 范围可以是'S'系统或'U'ser。 The last argument, 'A', if present, appends the value to the existing value of the variable (useful for adding a directoy to the PATH system variable). 最后一个参数'A'(如果存在)将该值附加到变量的现有值(对于向PATH系统变量添加directoy非常有用)。

The variables will presist, but you'll have to close then re-open a console to use them. 变量将保持不变,但您必须关闭然后重新打开控制台才能使用它们。 I usually run this from the "Run..." dialog, then open a console. 我通常从“运行...”对话框中运行它,然后打开一个控制台。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不运行计算机上运行Windows 8官方操作系统的情况下为Windows 8编写应用程序? - How do I write apps for Windows 8 without running the official Windows 8 Operating System on my computer? 如何在Windows XP和Windows 7中读取系统音频输出? - How do I read system audio output in windows xp and windows 7? 如何在 Makefile 中为 Windows 设置环境变量? - How do I set an environment variable in Makefile for Windows? 如何使用scp将文件从远程计算机(运行Linux)复制到Windows计算机? - How do I copy files from a remote computer (running Linux) to my Windows computer using scp? 如何在我的 dockerfile 中为 Windows 容器设置系统路径? - How do I set System Path in my dockerfile for a Windows Container? 如何检测我的软件是否在Windows XP上运行? - How do I detect whether my software is running on windows XP? 如何使用Windows WISE安装包获取和设置环境系统变量 - How to get and set Environment System Variable using Windows WISE Installation Package 他们如何分辨Windows系统变量中的环境变量名称 - How do they tell what environment variable name is in windows' System variables 如何在系统属性下的环境变量中访问由我设置的变量值? - How do I access a variable value set by me in environment variables under system properties? 如何在Perl中设置Win32系统环境变量? - How do I set an Win32 system environment variable in Perl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM