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

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

问题描述

我要使用 setx 修改Windows PATH变量。以下工作在Windows 8上至少有50%的时间:

  setx PATH%PATH%; C:\Python27\\ \\; C:\ Python27 \Scripts\ 

如果出现错误只能使用2次,那么以下工作在一些时间:

  setx PATH%PATH%; C: \Python27 \; C:\ Python27 \Scripts\

我们用引号包装第二个参数。我相信当%PATH%展开以包含空格时,引号是必要的。



在一个特定的Windows 7机器上,我有这个问题:

  echo%PATH%

打印:

  C:\ Foo \; C:\Bar \; [... many of stuff ...] C:\Baz\ 

然后我这样做:

  setx PATH%PATH%; C:\Quux\

然后它说Error:Truncated at 1,024 characters。现在让我们检查PATH包含的内容:

  echo%PATH%

它打印:

  C:\Foo\; C :\Foo\; C:\Bar \; C:\Bar\; [...很多东西,现在重复...] C:\B 

...并且以1,024个字符剪切。它跑了,因为重复。也很有趣:PATH的值改变,尽管事实上 setx 引发了错误,没有说成功。



我能够重复这个奇怪的行为几次(幸运的是,我保存了PATH的原始内容)。



目前,唯一的确定的方式,我知道追加到PATH的是以下内容:


  1. echo PATH。


  2. 将PATH的内容复制到文本文件中,并手动添加; C:\Python27\; C:\Python27 \ Scripts \ 到PATH的结尾。


  3. 将整个文本文件复制出来。 >


  4. setx PATH<粘贴字符串>


这个过程每次在Windows 7和Windows 8上都能正常工作。



在一个命令中执行此操作。我做错了什么?



谢谢。

解决方案

cmd 作为管理员,然后:

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



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



TL; DR



发生截断问题,因为当您回显%PATH%时,它将显示SYSTEM和USER值的并置。因此,当你在setx的第二个参数中添加它时,它将在USER var中匹配SYSTEM和USER值。



此外,/ M选项需要管理员权限,因此您需要以以管理员身份运行打开终端,否则setx将抱怨访问注册表路径被拒绝。



最后要注意的是:当你设置后回显%PATH%这样,您需要关闭 cmd 并重新打开。



如果要检查存储的实际值在注册表检查此问题


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:\Python27\Scripts\

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:\Python27\Scripts\"

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

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

echo %PATH%

It prints:

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

Then I do this:

setx PATH "%PATH%;C:\Quux\"

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

echo %PATH%

It prints:

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

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

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

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

  1. echo the PATH.

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

  3. Copy the whole thing out of the text file.

  4. setx PATH "<paste the string here>"

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?

Thank you.

解决方案

Run cmd as administrator, then:

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

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

TL;DR

The truncation issue happens because when you echo %PATH% it will show the concatenation of SYSTEM and USER values. So when you add it in your second argument to setx, it will be fitting SYSTEM and USER values inside the USER var. When you echo again, things will be doubled.

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

Last thing to 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天全站免登陆