本文翻译自:Adding directory to PATH Environment Variable in Windows

I am trying to add C:\\xampp\\php to my system PATH environment variable in Windows. 我试图将C:\\xampp\\php添加到Windows中的系统PATH环境变量中。

I have already added it using the Environment Variables dialog box. 我已经使用“环境变量”对话框添加了它。

But when I type into my console: 但是当我输入控制台时:

C:\>path

it doesn't show the new C:\\xampp\\php directory: 它不显示新的C:\\xampp\\php目录:

PATH=D:\Program Files\Autodesk\Maya2008\bin;C:\Ruby192\bin;C:\WINDOWS\system32;C:\WINDOWS;
C:\WINDOWS\System32\Wbem;C:\PROGRA~1\DISKEE~2\DISKEE~1\;c:\Program Files\Microsoft SQL
Server\90\Tools\binn\;C:\Program Files\QuickTime\QTSystem\;D:\Program Files\TortoiseSVN\bin
;D:\Program Files\Bazaar;C:\Program Files\Android\android-sdk\tools;D:\Program Files\
Microsoft Visual Studio\Common\Tools\WinNT;D:\Program Files\Microsoft Visual Studio\Common
\MSDev98\Bin;D:\Program Files\Microsoft Visual Studio\Common\Tools;D:\Program Files\
Microsoft Visual Studio\VC98\bin

I have two questions: 我有两个问题:

  1. Why did this happen? 为什么会这样呢? Is there something I did wrong? 我做错了什么吗?
  2. Also, how do I add directories to my PATH variable using the console (and programmatically, with a batch file)? 另外,如何使用控制台(以编程方式,使用批处理文件)将目录添加到PATH变量中?

#1楼

参考:https://stackoom.com/question/e3Qy/在Windows中将目录添加到PATH环境变量


#2楼

WARNING: This solution may be destructive to your PATH, and the stability of your system. 警告:此解决方案可能会对您的PATH和系统的稳定性造成破坏 As a side effect, it will merge your user and system PATH, and truncate PATH to 1024 characters. 副作用是,它将合并用户和系统PATH,并将PATH截断为1024个字符。 The effect of this command is irreversible. 此命令的作用是不可逆的。 Make a backup of PATH first. 首先备份PATH。 See the comments for more information. 请参阅注释以获取更多信息。

Don't blindly copy-and-paste this. 不要盲目复制并粘贴此内容。 Use with caution. 请谨慎使用。

You can permanently add a path to PATH with the setx command: 您可以使用setx命令将路径永久添加到PATH

setx /M path "%path%;C:\your\path\here\"

Remove the /M flag if you want to set the user PATH instead of the system PATH . 如果要设置用户PATH而不是系统PATH删除/M标志。

Notes: 笔记:

  • The setx command is only available in Windows 7 and later. setx命令仅在Windows 7和更高版本中可用。
  • You should run this command from an elevated command prompt. 您应该从提升权限的命令提示符下运行此命令。

  • If you only want to change it for the current session, use set . 如果只想在当前会话中更改它,请使用set 。


#3楼

I would use PowerShell instead! 我会改用PowerShell!

To add a directory to PATH using PowerShell, do the following: 要使用PowerShell将目录添加到PATH,请执行以下操作:

$PATH = [Environment]::GetEnvironmentVariable("PATH")
$xampp_path = "C:\xampp\php"
[Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path")

To set the variable for all users, machine-wide, the last line should be like: 要在整个计算机范围内为所有用户设置变量,最后一行应类似于:

[Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path", "Machine")

In a PowerShell script, you might want to check for the presence of your C:\\xampp\\php before adding to PATH (in case it has been previously added). 在PowerShell脚本中,您可能需要在添加到PATH之前检查C:\\xampp\\php的存在(如果先前已添加)。 You can wrap it in an if conditional. 您可以将其包装在if条件中。

So putting it all together: 所以放在一起:

$PATH = [Environment]::GetEnvironmentVariable("PATH")
$xampp_path = "C:\xampp\php"
if( $PATH -notlike "*"+$xampp_path+"*" ){[Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path", "Machine")
}

Better still, one could create a generic function. 更好的是,可以创建一个泛型函数。 Just supply the directory you wish to add: 只需提供您要添加的目录:

function AddTo-Path{
param([string]$Dir
)if( !(Test-Path $Dir) ){Write-warning "Supplied directory was not found!"return}$PATH = [Environment]::GetEnvironmentVariable("PATH")if( $PATH -notlike "*"+$Dir+"*" ){[Environment]::SetEnvironmentVariable("PATH", "$PATH;$Dir", "Machine")}
}

You could make things better by doing some polishing. 您可以做一些抛光来使事情变得更好。 For example, using Test-Path to confirm that your directory actually exists. 例如,使用Test-Path确认您的目录确实存在。


#4楼

You don't need any set or setx command, simply open the terminal and type: 您不需要任何setsetx命令,只需打开终端并输入:

PATH

This shows the current value of PATH variable. 这显示了PATH变量的当前值。 Now you want to add directory to it? 现在要添加目录吗? Simply type: 只需键入:

PATH %PATH%;C:\xampp\php

If for any reason you want to clear the PATH variable (no paths at all or delete all paths in it), type: 如果出于任何原因要清除PATH变量(根本没有路径或删除其中的所有路径),请键入:

PATH ;

Update 更新资料

Like Danial Wilson noted in comment below, it sets the path only in current session. 就像Danial Wilson在下面的评论中指出的那样,它仅在当前会话中设置路径。 To set the path permanently use setx but be aware, although that sets the path permanently but NOT in the current session, so you have to start a new command line to see the changes, more info here . 要永久设置路径,请使用setx但请注意,尽管这将永久设置路径,但不是在当前会话中设置路径,所以您必须启动新命令行以查看更改,更多信息在此处 。

To check if an environmental variable exist or see its value use ECHO commnad: 要检查环境变量是否存在或查看其值,请使用ECHO命令:

echo %YOUR_ENV_VARIABLE%

#5楼

Regarding point 2 I'm using a simple batch file that is populating PATH or other environment variables for me. 关于第2点,我正在使用一个简单的批处理文件,该文件正在为我填充PATH或其他环境变量。 Therefore, there is no pollution of environment variables by default. 因此,默认情况下不会污染环境变量。 This batch file is accessible from everywhere so I can type: 可以从任何地方访问此批处理文件,因此我可以输入:

c:\>mybatchfile
-- here all env. are available
c:\>php file.php

#6楼

Aside from all the answers, if you want a nice GUI tool to edit your windows environment variables you can use Rapid Environment Editor 除了所有答案之外,如果您想要一个不错的GUI工具来编辑Windows环境变量,则可以使用Rapid Environment Editor

try it! 试试吧! its safe to use and awesome! 它的使用安全和超赞!

http://www.rapidee.com/en/ http://www.rapidee.com/en/

在Windows中将目录添加到PATH环境变量相关推荐

  1. Linux 执行文件 path,linux可执行文件添加到PATH环境变量的方法

    linux命令行下面执行某个命令的时候,首先保证该命令是否存在,若存在,但输入命令的时候若仍提示:command not found 这个时候就的查看PATH环境变量的设置了,当前命令是否存在于PAT ...

  2. 如何在Linux中将目录添加到$ PATH

    Fatmawati Achmad Zaenuri/ShutterstockFatmawati Achmad Zaenuri / Shutterstock $PATH is one of the sil ...

  3. Windows下bat脚本获取Path环境变量

    :: 设置环境变量   :: 关闭终端回显 @echo off   set ENV_PATH=%PATH% @echo %ENV_PATH%   :: 添加环境变量,即在原来的环境变量后加上英文状态下 ...

  4. c语言path环境变量,Windows下PATH等环境变量详解(转载)

    在学习JAVA的过程中,涉及到多个环境变量(environment variable)的概念,如PATH.正确地配置这些环境变量,是能够顺利学习.开发的前提.而经常出现的问题是:有的学习者能够按照提示 ...

  5. Linux显示环境变量PATH的值,Linux PATH环境变量

    Linux PATH环境变量教程 Linux PATH环境变量说明 也就是说当用户在 如果命令或者程序的位置没有包括在 PATH 变量中,那么如果不使用绝对路径的话, shell 是没法找到的.如果想 ...

  6. windows下安装composer方法(不修改PATH环境变量)

    转载地址:http://www.th7.cn/Program/php/201410/297829.shtml composer是一个新崛起的PHP的依赖管理工具.官方安装方法见: https://ge ...

  7. Windows中path环境变量的理解

    path:程序的前缀路径. 设置好path后,以后在命令行中只需输入程序名称就OK 了. eg: E:\sai\sai.exe 方法1:直接在命令行输入:E:\sai\sai.exe 方法2:在pat ...

  8. 一文搞懂Path环境变量

    什么是Path环境变量? 在探讨这个问题之前,我们需要了解什么是环境变量. "环境变量"和"path环境变量"其实是两个东西,这一点大家一定要区分开,不要混为一 ...

  9. 环境变量基本操作及path环境变量

    环境变量基本操作及path环境变量 环境变量的解释 环境变量的一些基本操作 path环境变量 环境变量的解释 环境变量就是操作系统当中的一些变量,指在操作系统中用来指定操作系统运行环境的一些参数.环境 ...

最新文章

  1. 【Python】序列解包 and * 和 ** 的区别
  2. 2015 年最受 Linux 爱好者欢迎的软硬件大盘点
  3. 函数式编程语言python-用Python进行基础的函数式编程的教程
  4. 网络编程学习笔记(recvfrom和sendto函数)
  5. pyecharts离线使用说明
  6. 基于事件驱动架构构建微服务第10部分:在docker容器内运行单元测试
  7. 从真实项目中抠出来的设计模式——第三篇:责任链模式
  8. 计算机基础知识 pdf答案,计算机基础知识练习 答案版.pdf
  9. LETTERS (信息学奥赛一本通-T1212)
  10. mybatis plus 格式话_作为阿里的面试官,我有话想说
  11. 快手:笔试题(版本号比较,平方和为1,合并两个流)
  12. 指尖初体验之手指的舞蹈
  13. 如何使用postman带Token测试接口?
  14. linux网卡取消混杂模式,Linux下网卡混杂模式设置和取消
  15. Sourcetree下载-使用-快速入门
  16. Mac 系统下 xcode 卸载 清理
  17. 关于UI设计学习,推荐6个高质量的学习网站!
  18. 在单点登录中,如果 cookie 被禁用了怎么办
  19. 基于控制的角度无人机集群——避障(有源代码)
  20. 基于大中台架构的电商业务中台最佳实践之一:业务中台总体架构介绍

热门文章

  1. View的生命周期方法和Activity生命周期方法关系
  2. Android 内存优化
  3. 程序员35岁生死大关!被迫无奈转行.....
  4. 吵架记-2020年6月22日16:47:45
  5. java高级编程技巧
  6. android中button点击频率控制
  7. Java学习笔记18
  8. python 回归去掉共线性_一文讲解机器学习算法中的共线性问题
  9. Java锁优化思路及JVM实现
  10. RAD Studio (Delphi) Firemonkey 教程