如何使用 setx 添加到 Windows PATH 变量?遇到奇怪的问题 [英] How do I add to the Windows PATH variable using setx? Having weird problems

查看:18
本文介绍了如何使用 setx 添加到 Windows PATH 变量?遇到奇怪的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 setx 修改 Windows PATH 变量.以下在 Windows 8 上至少有 50% 的时间有效:

I want to modify the Windows PATH variable using setx. The following works at least 50% of the time on Windows 8:

setx PATH %PATH%;C:Python27;C:Python27Scripts

如果它给出错误默认参数只能使用 2 次",那么以下在某些时候会起作用:

If it gives the error "the default argument can only be used 2 times", then the following works some of the time:

setx PATH "%PATH%;C:Python27;C:Python27Scripts"

不同之处在于我们将第二个参数用引号括起来.我相信当 %PATH% 扩展为包含空格时,引号是必要的.

The difference is that we wrapped the second argument in quotes. I believe the quotes are necessary when %PATH% expands to include spaces.

然而,我在 Windows 7 上遇到了一些奇怪的问题.在一台特定的 Windows 7 机器上,我遇到了这个问题:

However, I have encountered some weird problems on Windows 7. On one particular Windows 7 machine, I had this problem:

echo %PATH%

它打印:

C:Foo;C:Bar;[...lots of stuff...]C:Baz

然后我这样做:

setx PATH "%PATH%;C:Quux"

然后显示错误:截断为 1,024 个字符".现在让我们检查一下 PATH 包含什么:

Then it says "Error: Truncated at 1,024 characters." Now let's check what PATH contains:

echo %PATH%

它打印:

C:Foo;C:Foo;C:Bar;C:Bar;[...lots of stuff, now duplicated...]C:B

...它在 1,024 个字符处被截断.它因为重复而跑了.同样有趣的是:尽管 setx 引发了错误并且没有说成功",但 PATH 的值会发生变化.

...and it is cut off at 1,024 characters. It ran over because of the duplicates. Also interesting: The value of PATH changes despite the fact that setx raised an error and did not say "Success".

我能够多次重复这种奇怪的行为(幸运的是我保存了 PATH 的原始内容).

I was able to repeat this strange behavior several times (luckily I had saved the original contents of PATH).

目前,我知道附加到 PATH 的唯一可靠方法如下:

At the moment, the only surefire way I know to append to the PATH is the following:

  1. echo 路径.

将PATH的内容复制到一个文本文件中,手动添加;C:Python27;C:Python27Scripts到PATH的末尾.

Copy the contents of PATH into a text file and manually add ;C:Python27;C:Python27Scripts to the end of the PATH.

从文本文件中复制整个内容.

Copy the whole thing out of the text file.

setx PATH "<在此处粘贴字符串>>>>>

该过程每次都适用于 Windows 7 和 Windows 8.

That process works every single time on both Windows 7 and Windows 8.

我真的应该能够在一个命令中做到这一点.我做错了什么?

I should really be able to do this in one command. What am I doing wrong?

推荐答案

以管理员身份运行cmd,然后:

Run cmd as administrator, then:

setx /M PATH "%PATH%;<your-new-path>"

/M 选项在 SYSTEM 范围内设置变量.默认行为是为 USER 设置.

The /M option sets the variable at SYSTEM scope. The default behaviour is to set it for the USER.

TL;DR

发生截断是因为当您回显 %PATH% 时,您会获得 SYSTEM 和 USER 值的串联,因此,当您将其添加到 setx 的第二个参数中时,命令将尝试在 USER 变量中适应 SYSTEM 和 USER 的内容.当你再次 echo 时,结果将翻倍.

The truncation happens because when you echo %PATH% you get the concatenation of SYSTEM and USER values so, when you add it in your second argument to setx, the command will try to fit the contents of both SYSTEM and USER within the USER var. When you echo again, the result will be doubled.

/M 选项需要管理员权限,因此您需要使用以管理员身份运行"打开终端否则 setx 将因访问注册表路径被拒绝"而失败.

The /M option requires administrator privilege, so you need to open your terminal with "run as administrator" otherwise setx will fail with "access to registry path is denied".

注意:这样设置后回显%PATH%是看不到新值的,需要关闭cmd再打开.

Note: You won't see the new value when you echo %PATH% just after setting it this way, you need to close cmd and open again.

如果您想检查注册表中存储的实际值,请检查这个问题.

If you want to check the actual values stored in registry check this question.

这篇关于如何使用 setx 添加到 Windows PATH 变量?遇到奇怪的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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