我试图在Windows中添加C:\xampp\php到我的系统PATH环境变量。

我已经使用环境变量对话框添加了它。

但当我在控制台输入:

C:\>path

它不会显示新的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

我有两个问题:

为什么会发生这种情况?我做错什么了吗? 另外,如何使用控制台(并以编程方式,使用批处理文件)向PATH变量添加目录?


当前回答

在命令提示符中,您告诉Cmd使用Windows资源管理器的命令行,在它前面加上start。

因此,启动Yourbatchname。

注意,您必须注册,如果它的名称是batchfile.exe。

可以将程序和文档添加到注册表中,因此在“开始-运行”对话框或快捷方式中输入它们的名称而不输入路径,Windows就可以找到它们。

这是一个通用的reg文件。复制下面的行到一个新的文本文档,并将其保存为anyname.reg。用你的程序或文档编辑它。

在路径中,使用\\分隔键路径中的文件夹名称,就像regedit使用单个\分隔键名称一样。所有reg文件都以REGEDIT4开头。分号将一行转换为注释。符号@表示将值赋给键,而不是指定值。

该文件不必存在。这可以用来设置Word.exe打开Winword.exe。

输入start batchfile将启动iexplorer .exe。

REGEDIT4
;The bolded name below is the name of the document or program, <filename>.<file extension>

[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\Batchfile.exe]

; The @ means the path to the file is assigned to the default value for the key.
; The whole path in enclosed in a quotation mark ".

@="\"C:\\Program Files\\Internet Explorer\\iexplore.exe\""

; Optional Parameters. The semicolon means don't process the line. Remove it if you want to put it in the registry

; Informs the shell that the program accepts URLs.

;"useURL"="1"

; Sets the path that a program will use as its' default directory. This is commented out.

;"Path"="C:\\Program Files\\Microsoft Office\\Office\\"

你已经在另一个答案中知道了路径。也参见doskey /?对于CMD宏(它们只在输入时工作)。

您可以在CMD下执行启动命令。来自Windows资源包技术参考

自动运行

HKCU\Software\Microsoft\Command Processor

Data type Range Default value
REG_SZ  list of commands  There is no default value for this entry.

描述

包含每次启动Cmd.exe时执行的命令。

其他回答

您不需要任何set或setx命令。只需打开终端,输入:

PATH

这显示了PATH变量的当前值。现在要添加目录吗?简单的类型:

PATH %PATH%;C:\xampp\php

如果出于任何原因你想清除PATH变量(完全没有路径或删除其中的所有路径),输入:

PATH ;

更新

就像Danial Wilson在下面的评论中指出的那样,它只在当前会话中设置路径。要永久地设置路径,请使用setx,但要注意,尽管它会永久地设置路径,但不会在当前会话中设置,因此必须启动一个新的命令行才能查看更改。更多信息在这里。

使用ECHO命令检查环境变量是否存在或查看其值:

echo %YOUR_ENV_VARIABLE%

选项1

在使用GUI更改PATH之后,关闭并重新打开控制台窗口。

这是可行的,因为只有在更改之后启动的程序才能看到新的PATH。

选项2

此选项仅影响当前shell会话,而不会影响整个系统。在已打开的命令窗口中执行以下命令:

set PATH=%PATH%;C:\your\path\here\

该命令将C:\your\path\here\追加到当前路径。如果路径包含空格,则不需要包含引号。

分解一下:

set -仅为当前cmd会话更改cmd的环境变量的命令;其他程序和系统不受影响。 PATH= -表示PATH是要临时更改的环境变量。 %PATH%;C:\your\ PATH \here\ - %PATH%部分扩展为PATH的当前值,然后;C:\your\ PATH \here\被连接到它。这将成为新的PATH。

更安全的 SETX

对@Nafscript的初始SETX答案的所有注释点头。

默认情况下SETX将更新您的用户路径。 对于SETX……/M将更新您的系统路径。 %PATH%包含附加用户路径的系统路径

警告

备份你的路径- SETX将截断你的垃圾超过1024个字符 不要调用SETX %PATH%;xxx -将系统路径添加到用户路径中 不要调用SETX %PATH%;xxx /M -将用户路径添加到系统路径中 过度使用批处理文件会导致盲目

ss64 SETX页面有一些非常好的例子。重要的是,它指向了SETX vs SETX /M的注册表项的位置

用户变量: HKCU \环境 系统变量: HKLM \ SYSTEM \ CurrentControlSet \ \会话管理器\环境控制

使用说明

附加到用户路径

append_user_path.cmd

@ECHO OFF
REM usage: append_user_path "path"
SET Key="HKCU\Environment"
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
ECHO %CurrPath% > user_path_bak.txt
SETX PATH "%CurrPath%";%1

附加到系统路径

append_system_path.cmd。必须以管理员身份运行。

(除了Key和SETX /M修饰符不同之外,基本上是一样的。)

@ECHO OFF
REM usage: append_system_path "path"
SET Key="HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
ECHO %CurrPath% > system_path_bak.txt
SETX PATH "%CurrPath%";%1 /M

选择

最后,ss64 SETX页面推荐了一个名为SETENV的改进版本,它将设置用户或系统环境变量分开。


例子

下面是在Windows 7上设置PATH环境变量的完整示例。该示例在尝试更改值之前检测软件是否已经添加到PATH。与上面给出的例子有一些小的技术差异:

@echo off
set OWNPATH=%~dp0
set PLATFORM=mswin

if defined ProgramFiles(x86)                        set PLATFORM=win64
if "%PROCESSOR_ARCHITECTURE%"=="AMD64"              set PLATFORM=win64
if exist "%OWNPATH%tex\texmf-mswin\bin\context.exe" set PLATFORM=mswin
if exist "%OWNPATH%tex\texmf-win64\bin\context.exe" set PLATFORM=win64

rem Check if the PATH was updated previously
echo %PATH% | findstr "texmf-%PLATFORM%" > nul

rem Only update the PATH if not previously updated
if ERRORLEVEL 1 (
  set Key="HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
  for /F "USEBACKQ tokens=2*" %%A in (`reg query %%Key%% /v PATH`) do (
    if not "%%~B" == "" (
      rem Preserve the existing PATH
      echo %%B > currpath.txt

      rem Update the current session
      set PATH=%PATH%;%OWNPATH%tex\texmf-%PLATFORM%\bin
      
      rem Persist the PATH environment variable
      setx PATH "%%B;%OWNPATH%tex\texmf-%PLATFORM%\bin" /M
    )
  )
)

1. 并不完全正确

在Windows 10上,我能够搜索set path环境变量,并得到这些指令:

From the desktop, right-click the very bottom-left corner of the screen to get the Power User Task Menu. From the Power User Task Menu, click System. In the Settings window, scroll down to the Related settings section and click the System info link. In the System window, click the Advanced system settings link in the left navigation panel. In the System Properties window, click the Advanced tab, then click the Environment Variables button near the bottom of that tab. In the Environment Variables window (pictured below), highlight the Path variable in the System variables section and click the Edit button. Add or modify the path lines with the paths you want the computer to access. Each different directory is separated with a semicolon, as shown below:

C:\程序文件;C:\Winnt;C:\Winnt\System32

我第一次搜索它时,它立即弹出了系统属性窗口。之后,我找到了上面的说明。

下面的解决方案非常有效。

在你的Windows终端上试试下面的命令。

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

SUCCESS:保存了指定的值。

你可以参考更多在这里。