Windows批处理只选择用户变量 [英] Windows batch, select ONLY user variables

查看:273
本文介绍了Windows批处理只选择用户变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在环境变量我有两个用户变量和系统变量PATH变量。

In environment variables I have a PATH variable for both user variables and system variables.

在一个批处理脚本,为了让我给用户的PATH变量以崭新的给定的路径追加,我需要选择当前值。不幸%PATH%返回用户变量和系统变量的组合。

In a batch script, in order for me to append the user PATH variable with a new given path, I need to select the current value. Unfortunately %PATH% returns a combination of the user variable and the system variable.

当然,我只希望在新的自定义路径值添加到用户变量。有与系统路径增强它,以及没有任何意义。这就是为什么我有2个变量。

Of course I only want to add a new custom path value to the user variable. There is no point in enhancing it with the system path as well. That's why I have 2 variables.

先谢谢了。

编辑:文档中找到下面的语句:

Found in the documentation the following statement:

的%PATH%变量设置为既是系统和用户变量,则2的值结合起来,使该路径的当前登录的用户....

The %PATH% variable is set as both a system and user variable, the 2 values are combined to give the PATH for the currently logged in user....

例如:

用户变量:

PATH
value: c:\dev

系统变量

PATH
value: c:\Program Files

我想要做的,就是要添加到用户变量的值:C:\\ tmp目录,因此,在年底的路径不会有值:C:\\ dev的; C:\\ tmp目录

What I want to do, is to add to the user variable the value: c:\tmp, so that in the end PATH will have the value: c:\dev;c:\tmp

但是,如果打开一个cmd窗口:

But, if open a cmd window:

echo %PATH%
c:\Program Files;c:\dev

所以SETX会做以下

so the setx will do the following

setx path "%path%;c:\tmp"

打开新的CMD

open new cmd

echo %PATH%
c:\Program Files;c:\dev;c:\tmp

这是错误的,因为我只需要C:\\ dev的; C:\\ tmp目录

And that is wrong, because I only needed c:\dev;c:\tmp

我希望我更清楚这个时候。

I hope I was more clear this time.

推荐答案

你是如何修改的变量?

有只有一个环境变量 PATH ,这样你就可以继续前进,改变它。这些变化是暂时的(和地方对您的过程及其子)。

There is only one environment variable PATH, so you can go ahead and change it. These changes are transient (and local to your process and its children).

有两个(实际上更多)在注册地持续地从创建进程时的环境变量进行初始化。您可以修改他们使用 工具。没有歧义,因为它们是独立的:

There are two (actually more) persistent places in Registry from which the environment variables are initialized when a process is created. You can modify them using reg utility. There is no ambiguity since they are separate:


  • HKEY_CURRENT_USER \\环境

  • HKEY_LOCAL_MACHINE \\系统\\ CurrentControlSet \\控制\\会话管理\\环境

您可能不得不重新登录的注册表更改生效(我不记得是否有通知,这些设置已经改变了资源管理器编程的方式)。还要注意,在默认情况下的子进程继承其父的环境(除非家长采取特别措施不这样做),所以如如果运行了一个 CMD 窗口,并稍后通过修改系统设置对话框中的环境中,应用程序从启动 CMD 不会看到的变化。

You may have to re-login for changes in Registry to take effect (I don't remember whether there's a programmatic way to notify explorer that these settings have changed). Also note that by default child processes inherit the environment of their parent (unless the parent takes special measures to do otherwise), so e.g. if you launch a cmd window and later modify the environment via system settings dialog, applications started from that cmd won't see the changes.

[UPD] 您可以使用从注册表获得用户特定的环境变量的值工具:

[UPD] You can get the value of user-specific environment variable from registry using reg utility:

reg query HKCU\Environment /v PATH

但你必须过滤其输出的实际价值,因为它吐出了一些无用的文字。下面一个例子咒语:

Though you'll have to filter its output for the actual value, as it spits out some useless text. Here an example incantation:

for /f "usebackq tokens=2,*" %A in (`reg query HKCU\Environment /v PATH`) do set value=%B

这将结果存储在环境变量。记住要翻一番的批处理文件中使用它的时候。

It will store the result in the environment variable value. Remember to double %'s when using it in a batch file.

这篇关于Windows批处理只选择用户变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆