【问题标题】:System Variable path is different in command prompt命令提示符中的系统变量路径不同
【发布时间】:2015-06-06 16:41:00
【问题描述】:

我见过各种各样的问题,但我已经为此奋斗了一整天,所以请给我一些帮助:)

小故事:我在系统变量面板和 cmd 中有不同的 PATH 变量。无法运行exe文件?

更长的故事:我正在尝试安装 Ruby。尝试了不同的版本,但问题是一样的:我已经更新了我的 PATH 值,Ruby 在那里。 但是在 cmd 中使用它时,无法识别 ruby​​。 echo %PATH% 与环境变量面板中的值不同!

第一个附件:环境变量面板:

用户变量的值: C:\Users\morifey\nvmw\nodejs\v0.10.36;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;"C:\Program Files\Intel\WiFi\bin\";"C:\Program Files\Common Files\Intel\WirelessCommon\";"C:\Program Files (x86)\Skype\Phone\";"C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86";"C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64";"C:\Program Files\nodejs\";C:\wamp\bin\php\php5.5.12;C:\ProgramData\ComposerSetup\bin;"C:\Program Files\TortoiseGit\bin";"C:\Program Files (x86)\Git\cmd";"C:\Program Files\Intel\WiFi\bin\";"C:\Program Files\Common Files\Intel\WirelessCommon\";C:\Users\morifey\AppData\Roaming\npm;C:\Ruby21\bin

系统变量的值: C:\Ruby21\bin;C:\Users\morifey\nvmw\nodejs\v0.10.36;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;"C:\Program Files\Intel\WiFi\bin\";"C:\Program Files\Common Files\Intel\WirelessCommon\";"C:\Program Files (x86)\Skype\Phone\";"C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86";"C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64";"C:\Program Files\nodejs\";C:\wamp\bin\php\php5.5.12;C:\ProgramData\ComposerSetup\bin;"C:\Program Files\TortoiseGit\bin";"C:\Program Files (x86)\Git\cmd";"C:\Program Files\Intel\WiFi\bin\";"C:\Program Files\Common Files\Intel\WirelessCommon\";"C:\Users\morifey\AppData\Roaming\npm";%SystemRoot%\system32;%SystemRoot%

我将它们与 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment 进行比较 - 它们都一样

但是我运行 cmd 并使用 echo %PATH%。结果: C:\Users\morifey\nvmw\nodejs\v0.10.36;C:\ProgramData\Oracle\Java\javapath;C:\Win dows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPow erShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Int el\WirelessCommon\;C:\Program Files (x86)\Skype\Phone\;C:\Program Files (x86)\In tel\OpenCL SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64;C :\Program Files\nodejs\;C:\wamp\bin\php\php5.5.12;C:\ProgramData\ComposerSetup\b in;C:\Program Files\TortoiseGit\bin;C:\Program Files (x86)\Git\cmd;C:\Program Fi les\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Users \morifey\AppData\Roaming\npm

缺少 Ruby! 以管理员身份运行和不以管理员身份运行都是一样的(实际上我只有一个管理员用户)。

我尝试过设置路径(通过 set 或 setx),同时更改注册表和环境变量 - 没有任何效果。我尝试在有空格的路径中添加引号 (") - 没有区别。

除了如果我以管理员身份运行 cmd 并使用 setx PATH "%PATH%;C:\Ruby21\bin"!然后,echo %PATH% 返回 real(更新的)路径,我可以在这个 cmd 中运行ruby ONLY。如果我关闭它,然后以管理员身份重新打开它 - 更改将丢失。

请给我任何建议如何处理?我只需要运行 Ruby :)

Windows 7 Ultimate,Service Pack 1,64 位; 下载的 Ruby(来自http://rubyinstaller.org/downloads/)——Ruby 2.2.2(32 位和 64 位)、Ruby 2.1.6(32 位和 64 位)——结果都一样。


编辑:在几个人说我不需要引号之后,我删除了它们,并完全按照@eryksun 所说的进行放置。结果如下:

C:\Users\morifey>echo %PATH% C:\Users\morifey\nvmw\nodejs\v0.10.36;C:\ProgramData\Oracle\Java\javapath;C:\Win dows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPow erShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Int el\WirelessCommon\;C:\Program Files (x86)\Skype\Phone\;C:\Program Files (x86)\In tel\OpenCL SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64;C :\Program Files\nodejs\;C:\wamp\bin\php\php5.5.12;C:\ProgramData\ComposerSetup\b in;C:\Program Files\TortoiseGit\bin;C:\Program Files (x86)\Git\cmd;C:\Program Fi les\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Users \morifey\AppData\Roaming\npm

有些输入重复很奇怪: C:\Program Files\Intel\WiFi\bin\; C:\Program Files\Intel\WiFi\bin\; C:\Program Files\Common Files\Intel\WirelessCommon\; C:\Program Files\Common Files\Intel\WirelessCommon\; C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86; C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64; 尽管它们与我粘贴在系统变量路径中的行不同!


更新 - 我使用 PowerShell 和 this 脚本检查系统变量中是否存在重复。我正在上传带有结果的图像。 很奇怪路径正是应该的,但正如你所见,在 CMD 中我看到了不同的东西!


更新 - 今天我尝试从 PowerShell 运行 Ruby - 一切顺利。所以似乎只有 CMD 不知道发生了什么!只是提醒您-重新启动无济于事! :)

【问题讨论】:

  • 退出并登录(或重新启动)。
  • 重启没有帮助。我每次卸载 Ruby 后都尝试重新启动,然后在安装新的 Ruby 后,然后以任何方式更改 %PATH% 后。每次结果都是一样的。
  • 从用户和系统 path 环境变量设置中删除所有 " 双引号(使用 GUI,no setx)。这些在那里被认为是有害的。 Read more.
  • 我看到很少有帖子说空格 anywhere 不是一个好习惯。我已经手动添加了引号。我已经从系统变量中手动删除了它们 - 没有任何改变。
  • 为什么用户和系统PATH之间有重复?绝不应该是这样。这两个值在登录时合并。看起来用户 PATH 应该只包含 C:\Users\morifey\nvmw\nodejs\v0.10.36;C:\Users\morifey\AppData\Roaming\npm

标签: ruby windows cmd registry environment-variables


【解决方案1】:

结果是我每次启动 cmd 时运行的 bat 文件存在问题,它修改了它的变量!感谢@eryksun 和@ılǝ - 似乎在注册表中(HKCU\Software\Microsoft\Command Processor\AutoRun)你可能有类似的东西。我的问题是 nvmw 包 (https://www.npmjs.com/package/nvmw) 在安装时创建了这个 .bat 文件并存储了我当前的 PATH 变量。然后它将它设置为 cmd 每次我使用它,所以它永远不会更新。 你可以在这里看到我创建的问题:https://github.com/nanjingboy/nvmw/issues/5

简短的故事:检查您的HKCU\Software\Microsoft\Command Processor\AutoRun,并不要使用 NVMW 包!

【讨论】:

  • 这解决了我一年多来阻止我使用 Qt Creator 的问题。非常感谢!
  • 我刚刚遇到了这个问题,试图弄清楚为什么 Anaconda 会出现在我的路径中。原来 Andaconda 设置 AutoRun 运行 condabin\conda_hook.bat
【解决方案2】:

如果您的系统变量 PATH 长度超过某个值,它会在 cmd 提示符下被截断。根据这个answer,这个值是2047。删除重复项并修剪你的路径,你会看到两个变量都是一样的。

【讨论】:

  • 很容易看出我的 PATH 不超过 2047。事实上,它是 669 个字符 - 远低于限制。但从图片中可以看出,并不是截断了,而是不同
【解决方案3】:

当您使用 SET 操作数时,它只更改当前控制台的环境变量,这就是它的用途。要永久更改环境变量,您应该使用命令 SETX。并重新启动窗口。这 从技术上讲,您不能重新启动,只需重新启动所有服务,但很容易重新启动。

【讨论】:

  • 如果您阅读我的详细说明,您会发现我使用了setx,但它仅适用于当前的 cmd 提示符。重新启动没有帮助(如几次所述)。只有 cmd 读取了错误的路径 - 其他一切似乎都是合法的。
  • 欧。然后应该是长度限制,就像 Atilla Ozgur 说的那样。在windows中以Unicode格式保存的路径(您的大小* 2),并且用户的变量与系统的变量合并。合并后可能会溢出长度限制并丢弃合并。
【解决方案4】:

我为自动部署进行软件打包,有时有些应用程序需要在 %PATH% 中设置文件夹,为此我使用这个 Powershell 脚本。 它会检查该文件夹是否已经存在(因此您不会有重复),并在需要时将其删除(即静默卸载)。

您使用 ADD-PATH 添加文件夹并使用 REMOVE-PATH 删除它。 像魅力一样工作。

#REQUIRES -Version 3.0

<#
.SYNOPSIS
    A script to add or remove folders to %PATH%
.DESCRIPTION
    It contains 2 functions, one for adding and one for removing. It reads the registry and does the modifications there.
.NOTES
    File Name      : AddRemovePath.ps1
    Author         : Iulian Dita (iulian.dita@gmail.com)
    Prerequisite   : PowerShell V3 over Vista and upper.
    Copyright 2015 - Iulian Dita
.LINK
    Script located at:
    \\%server%\dsm2$\Work\Master\Projects\29532\Extern$
.VERSION
    0.7
.VERSION_HISTORY
    0.1 Initial version
    0.2 Bug fixes
    0.3 Cosmetic fixes, automatic removal of preceding ";"
    0.4 Fixed double entries
    0.5 Kill Explorer and CMD processes before making any modifications
    0.6 Check if folder to be removed already exists in PATH
        Escaping special characters no longer needed for the removal function
    0.7 Cleaned the code and removed some syntax missmatches
        Included the sendmessage function to avoid killing the explorer task
        Used [reges]::escape() to avoid running into troubles with the path-string and -match methode
        Contributed by Maik Krammer
#>

if (-not ("win32.nativemethods" -as [type])) {
    # import sendmessagetimeout from win32
    add-type -Namespace Win32 -Name NativeMethods -MemberDefinition @"
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(
   IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam,
   uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
"@
}

$HWND_BROADCAST = [intptr]0xffff;
$WM_SETTINGCHANGE = 0x1a;
$result = [uintptr]::zero

function global:ADD-PATH
{
    [Cmdletbinding()]
    param ( 
        [parameter(Mandatory=$True, ValueFromPipeline=$True, Position=0)] 
        [string] $Folder
    )

    # See if a folder variable has been supplied.
    if (!$Folder -or $Folder -eq "" -or $Folder -eq $null) { 
        throw 'No Folder Supplied. $ENV:PATH Unchanged'
    }

    # Get the current search path from the environment keys in the registry.
    $oldPath=$(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path

    # See if the new Folder is already in the path.
    if ($oldPath | Select-String -SimpleMatch $Folder){ 
        return 'Folder already within $ENV:PATH' 
    }

    # Set the New Path and add the ; in front
    $newPath=$oldPath+';'+$Folder
    Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath -ErrorAction Stop

    # Show our results back to the world
    return 'This is the new PATH content: '+$newPath

    # notify all windows of environment block change
    [win32.nativemethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE, [uintptr]::Zero, "Environment", 2, 5000, [ref]$result)
}

function global:REMOVE-PATH {
    [Cmdletbinding()]
    param ( 
        [parameter(Mandatory=$True, ValueFromPipeline=$True, Position=0)]
        [String] $Folder
    )

    # See if a folder variable has been supplied.
    if (!$Folder -or $Folder -eq "" -or $Folder -eq $NULL) { 
        throw 'No Folder Supplied. $ENV:PATH Unchanged'
    }

    # add a leading ";" if missing
    if ($Folder[0] -ne ";") {
        $Folder = ";" + $Folder;
    }

    # Get the Current Search Path from the environment keys in the registry
    $newPath=$(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path

    # Find the value to remove, replace it with $NULL. If it's not found, nothing will change and you get a message.
    if ($newPath -match [regex]::Escape($Folder)) { 
        $newPath=$newPath -replace [regex]::Escape($Folder),$NULL 
    } else { 
        return "The folder you mentioned does not exist in the PATH environment" 
    }

    # Update the Environment Path
    Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath -ErrorAction Stop

    # Show what we just did
    return 'This is the new PATH content: '+$newPath

    # notify all windows of environment block change
    [win32.nativemethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE, [uintptr]::Zero, "Environment", 2, 5000, [ref]$result)
}


# Use ADD-PATH or REMOVE-PATH accordingly.

#Anything to Add?

ADD-PATH "%_PATH_TO_BE_ADDED%"

#Anything to Remove?

ADD-PATH "%_PATH_TO_BE_REMOVED%"

我没有从头开始创建这个脚本,我在网上找到了一些片段并根据我的需要进行了调整。

编辑:我稍微更新了代码,您不再需要转义反斜杠或关心前面的 ; 以及其他有用的东西。

【讨论】:

    【解决方案5】:

    可能是命令提示符自动运行的问题(命令提示符在运行时设置 PATH): https://superuser.com/questions/727316/error-in-command-line-the-system-cannot-find-the-path-specified

    【讨论】:

      猜你喜欢
      • 2019-01-03
      • 2019-07-30
      • 2021-11-05
      • 1970-01-01
      • 2019-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      相关资源
      最近更新 更多