系统变量路径在命令提示符

时间:2015-06-06 16:41:00

标签: ruby windows cmd registry environment-variables

我已经看到了不同类型的问题,但是我整整都在与之斗争,所以请给我一些帮助:)

短篇小说:我在“系统变量”面板和cmd中有不同的PATH变量。无法运行exe文件吗?

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

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

enter image description here

用户变量的值: 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%会返回真实(已更新)路径,并且我可以在此cmd中运行ruby 。如果我将其关闭,并以管理员身份重新打开 - 更改将丢失。

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

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

编辑:在几个人说我不需要引号之后,我已经删除了它们,并准确地放置了@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中我看到了不同的东西!

enter image description here

更新 - 今天我尝试从PowerShell运行Ruby - 一切顺利。因此,似乎只有CMD不知道发生了什么!只是提醒你 - 重启没有帮助! :)

5 个答案:

答案 0 :(得分:1)

如果系统变量PATH长度超过某个值,则会在cmd提示符中截断它。根据此answer,此值为2047.删除重复项并修剪路径,您将看到两个变量都相同。

答案 1 :(得分:1)

当你使用SET操作数时,它只更改当前控制台的环境变量,它就是它的用途。要永久更改环境变量,应使用命令SETX。并重新启动Windows。该 技术上,您无法重启,只需重启所有服务,但轻松重启。

答案 2 :(得分: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 包裹!

答案 3 :(得分:1)

我为自动部署进行软件打包,有时候应用程序需要在%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%"

我没有从头开始创建这个脚本,我在线找到了它们并根据我的需要调整它们。

编辑:我稍微更新了代码,您不再需要逃避反斜杠或关注前面的; ,还有其他有用的东西。

答案 4 :(得分:0)

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