如何从批处理文件在 Windows 7 中持久设置变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6523979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:32:21  来源:igfitidea点击:

How to persistently set a variable in Windows 7 from a batch file?

windowspathbatch-fileenvironment-variables

提问by KaiserJohaan

I am trying to set the PATH environment variable in windows 7 using a bat-file; however it does not seem to work.

我正在尝试使用 bat 文件在 Windows 7 中设置 PATH 环境变量;但是它似乎不起作用。

I am using this windows command:

我正在使用此 Windows 命令:

set PATH=%cd%;%path%
pause

However it only appears to be valid for this cmd instance. I want it to be permanent, since I first set the PATH and then run a program which needs to locate the libraries in that folder.

但是它似乎只对这个 cmd 实例有效。我希望它是永久性的,因为我首先设置了 PATH,然后运行一个需要在该文件夹中定位库的程序。

回答by Ryan Bemrose

Use setx.exe instead of set.

使用 setx.exe 而不是 set。

setx PATH "%cd%;%path%;"
pause

Note that this sets the path for all future cmd instances, but notfor the current one. If you need that, also run your original set command.

请注意,这会为所有未来的 cmd 实例设置路径,但不会为当前的实例设置路径。如果需要,还可以运行原始的 set 命令。

UPDATE: The second parameter needs to be quoted if it contains spaces (which %path% always has). Be warned that if the last character in your %path% is a backslash, it will escape the trailing quote and the last path entry will stop working. I get around that by appending a semicolon before the closing quote.

更新:如果第二个参数包含空格(%path% 总是有),则需要引用它。请注意,如果 %path% 中的最后一个字符是反斜杠,它将转义尾随引号并且最后一个路径条目将停止工作。我通过在结束语前附加一个分号来解决这个问题。

If you don't want to risk getting ";;;;;;" at the end of your path after repeated runs, then instead strip any trailing backslash from the %path% variable before setting, and it will work correctly.

如果您不想冒险获得“;;;;;;” 在重复运行后的路径末尾,然后在设置之前从 %path% 变量中删除任何尾随反斜杠,它会正常工作。

回答by simon

If you want to do it in a batch file, use the regcommand to change the path value in the registry at the HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment key.

如果要在批处理文件中执行此操作,请使用reg命令更改注册表中 HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment 项的路径值。

Something like:

就像是:

reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_SZ /d "%path%;c:\newpath"

Check that the path in the %path% variable matches the system path.

检查 %path% 变量中的路径是否与系统路径匹配。

回答by DenNukem

As wizlb noted, doing

正如 wizlb 所指出的,做

setx PATH "%cd%;%path%;" -m

will copy local env to system env, and without -m it will copy system env to user env. Neither is desirable. In order to accurately edit only one part of registry (system or user, system in my below example) you need to do this:

将本地 env 复制到系统 env,如果没有 -m,它会将系统 env 复制到用户 env。两者都不是可取的。为了准确地只编辑注册表的一部分(系统或用户,在我下面的例子中是系统),你需要这样做:

for /F "tokens=2* delims= " %%f IN ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path ^| findstr /i path') do set OLD_SYSTEM_PATH=%%g
setx.exe PATH "%OLD_SYSTEM_PATH%;%OTHER_STUFF%;" -m

Credit for the solution goes to http://www.robvanderwoude.com/ntregistry.php

解决方案的功劳转到http://www.robvanderwoude.com/ntregistry.php

回答by David Heffernan

To do this properly I think you really need to go beyond a simple batch file. The MSDN documentationstates:

要正确执行此操作,我认为您确实需要超越简单的批处理文件。在MSDN文档状态:

To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environmentregistry key, then broadcast a WM_SETTINGCHANGEmessage with lParamset to the string "Environment". This allows applications, such as the shell, to pick up your updates.

要以编程方式添加或修改系统环境变量,请将它们添加到HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment注册表项,然后广播一条WM_SETTINGCHANGE消息,其中lParam设置为字符串“Environment”。这允许应用程序(例如 shell)获取您的更新。

First of all you won't be able to write to that key without a UAC elevation prompt. That's best arranged by adding the appropriate manifest to an executable file. Secondly, broadcasting WM_SETTINGCHANGEisn't simple from a batch file.

首先,如果没有 UAC 提升提示,您将无法写入该密钥。最好通过将适当的清单添加到可执行文件来安排。其次,WM_SETTINGCHANGE从批处理文件中广播并不简单。

In your position I'd write a short and simple console app to do the job.

在您的职位上,我会编写一个简短的控制台应用程序来完成这项工作。

回答by Changwang Zhang

A simple (may be better) solution is to use PathMgr.cmd

一个简单(可能更好)的解决方案是使用 PathMgr.cmd

Down the pathmgr_1.0.2.zip in https://gallery.technet.microsoft.com/Batch-Script-To-Manage-7d0ef21e

https://gallery.technet.microsoft.com/Batch-Script-To-Manage-7d0ef21e 中的 pathmgr_1.0.2.zip

Unzip and put the pathmgr.cmd in the same folder as your batch file, then in your batch file write these two lines:

解压缩并将 pathmgr.cmd 放在与批处理文件相同的文件夹中,然后在批处理文件中写入以下两行:

call pathmgr.cmd /del %cd% /y
call pathmgr.cmd /add %cd% /y

This will:

这会:

1) only update the user variable PATH, 2) will not include system PATH multiple times

1)只更新用户变量PATH,2)不会多次包含系统PATH

You can also run the batch file multiple times, and it will only include your current path ONCE in the PATH.

您也可以多次运行批处理文件,它只会在 PATH 中包含您当前的路径一次。

回答by Paul Mai

Assuming I want to create a System Environment Variable called "ZIP_PROGRAM" and I want to point it to the executable at path "reg add C:\Program Files\7-Zip\7z.exe

假设我想创建一个名为“ZIP_PROGRAM”的系统环境变量,并且我想将它指向路径“reg add C:\Program Files\7-Zip\7z.exe”中的可执行文件

I will perform following at DOS Prompt:

我将在 DOS 提示符下执行以下操作:

Step1: execute following code reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v ZIP_PROGRAM /t REG_SZ /d "C:\Program Files\7-Zip\7z.exe" /f

Step1:执行以下代码 reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v ZIP_PROGRAM /t REG_SZ /d "C:\Program Files\7-Zip\7z.exe" /f

Step2: Log Off then Login

步骤2:注销然后登录

Step3: Open DOS Prompt and execute: "set z" and you should be able to see the System Environment Variable update

Step3:打开DOS Prompt,执行:“set z”,应该可以看到系统环境变量更新了

回答by Santosh b

Use This command setx PATH "%PATH%;%MVN_HOME%\bin\"

使用此命令 setx PATH "%PATH%;%MVN_HOME%\bin\"

Anyways it wont be set in current session you need to use

无论如何它不会在您需要使用的当前会话中设置

set PATH="%PATH%;%MVN_HOME%\bin\"

设置 PATH="%PATH%;%MVN_HOME%\bin\"