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

时间:2021-10-09 05:02:24

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

我正在尝试使用批处理文件设置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实例有效。我希望它是永久性的,因为我首先设置路径,然后运行一个需要在那个文件夹中定位库的程序。

7 个解决方案

#1


60  

Use setx.exe instead of set.

对于setx使用。exe不是集。

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

Note that this sets the path for all future cmd instances, but not for 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%变量中删除任何尾反斜杠,它将正常工作。

#2


21  

If you want to do it in a batch file, use the reg command 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 key上更改注册表中的路径值。

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%变量中的路径是否与系统路径匹配。

#3


17  

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

#4


12  

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

要正确地做到这一点,我认为您确实需要超越一个简单的批处理文件。MSDN文档:

To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set 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 registry key中,然后使用lParam设置的字符串“environment”广播WM_SETTINGCHANGE消息。这允许应用程序(如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_SETTINGCHANGE isn'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.

在你的位置上,我会写一个简短的控制台应用来完成这项工作。

#5


4  

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

pathmgr_1.0.2。邮政在https://gallery.technet.microsoft.com/Batch-Script-To-Manage-7d0ef21e

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

解压缩并放置路径管理器。与批处理文件相同的文件夹中的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)只更新用户变量路径,2)不包含多次系统路径

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

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

#6


0  

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添加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添加“HKLM\SYSTEM\CurrentControlSet\ \Session Manager\Environment”/v ZIP_PROGRAM /t REG_SZ /d”C:\程序文件7-Zip\7z。exe”楼

Step2: Log Off then Login

第二步:注销然后登录

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

步骤3:打开DOS提示并执行:“设置z”,您应该能够看到系统环境变量的更新

#7


0  

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\"

设置路径= " % %路径;% MVN_HOME % \ bin \”

#1


60  

Use setx.exe instead of set.

对于setx使用。exe不是集。

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

Note that this sets the path for all future cmd instances, but not for 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%变量中删除任何尾反斜杠,它将正常工作。

#2


21  

If you want to do it in a batch file, use the reg command 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 key上更改注册表中的路径值。

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%变量中的路径是否与系统路径匹配。

#3


17  

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

#4


12  

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

要正确地做到这一点,我认为您确实需要超越一个简单的批处理文件。MSDN文档:

To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set 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 registry key中,然后使用lParam设置的字符串“environment”广播WM_SETTINGCHANGE消息。这允许应用程序(如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_SETTINGCHANGE isn'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.

在你的位置上,我会写一个简短的控制台应用来完成这项工作。

#5


4  

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

pathmgr_1.0.2。邮政在https://gallery.technet.microsoft.com/Batch-Script-To-Manage-7d0ef21e

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

解压缩并放置路径管理器。与批处理文件相同的文件夹中的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)只更新用户变量路径,2)不包含多次系统路径

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

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

#6


0  

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添加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添加“HKLM\SYSTEM\CurrentControlSet\ \Session Manager\Environment”/v ZIP_PROGRAM /t REG_SZ /d”C:\程序文件7-Zip\7z。exe”楼

Step2: Log Off then Login

第二步:注销然后登录

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

步骤3:打开DOS提示并执行:“设置z”,您应该能够看到系统环境变量的更新

#7


0  

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\"

设置路径= " % %路径;% MVN_HOME % \ bin \”