将当前目录永久添加到Windows路径 [英] Adding the current directory to Windows path permanently

查看:92
本文介绍了将当前目录永久添加到Windows路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将当前目录(从命令行)永久添加到Windows路径,但是在实现此问题时遇到了严重的问题.

I am trying to add the current directory (from a command-line) to Windows path permanently, but I am having serious problems implementing this.

我最初的尝试是:

set PATH=%PATH%;%cd%

但是,这仅在当前会话中有效;关闭命令行窗口后,PATH环境变量将保留其先前的值.

However, this only works in the current session; as soon as I close the command-line window, the PATH environment variable retains its previous value.

接下来,我尝试了:

setx PATH=%PATH%;%cd%

根据我在这里找到的一些答案,这可能在Windows 7和8中有效,但是在Windows 10中,setx命令具有三种工作方式:

This might work in Windows 7 and 8 according to some of the answers that I found here, but in Windows 10, the setx command has three ways of working:

Syntax 1:
    SETX [/S system [/U [domain\]user [/P [password]]]] var value [/M]

Syntax 2:
    SETX [/S system [/U [domain\]user [/P [password]]]] var /K regpath [/M]

Syntax 3:
    SETX [/S system [/U [domain\]user [/P [password]]]]
         /F file {var {/A x,y | /R x,y string}[/M] | /X} [/D delimiters]

长话短说,我无法使其正常工作:

Long story short, I am unable to get it to work:

ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).

有人可以建议如何以最简单的方式完成我的目标吗?

Can somebody please suggest how to complete my goal the easiest way?

如果每个Windows版本都有不同的语法,那么我也很高兴获得此信息.

If there's a different syntax per Windows version, then I'd be happy to get this info as well.

非常感谢您!

推荐答案

答案中的详细说明,为什么其他文件夹路径也如此不仅通过指定的文件夹路径添加到使用SetX的系统PATH中?是否不能通过以下方式从批处理文件中修改 system user PATH只需使用 local PATH覆盖或将文件夹路径附加到存储在注册表中的PATH.

As described in detail in answer on Why are other folder paths also added to system PATH with SetX and not only the specified folder path? it is not good to modify system or user PATH from within a batch file by simply overwriting or appending a folder path to PATH stored in registry using the local PATH.

为该任务添加当前目录路径到用户 PATH的一种解决方案是在Windows Vista或更高版本的Windows版本上使用以下代码:

One solution for this task to add current directory path to user PATH is using this code on Windows Vista or later released versions of Windows:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "skip=2 tokens=1,2*" %%N in ('%SystemRoot%\System32\reg.exe query "HKEY_CURRENT_USER\Environment" /v "Path" 2^>nul') do (
    if /I "%%N" == "Path" (
        set "UserPath=%%P"
        if defined UserPath goto CheckPath
    )
)

set "UseSetx=1"
if not "%CD:~1024,1%" == "" set "UseSetx="
if not exist %SystemRoot%\System32\setx.exe set "UseSetx="
if defined UseSetx (
    %SystemRoot%\System32\setx.exe Path "%CD%" >nul
) else (
    %SystemRoot%\System32\reg.exe ADD "HKCU\Environment" /f /v Path /t REG_SZ /d "%CD%" >nul
)

endlocal
goto :EOF

:CheckPath
setlocal EnableDelayedExpansion
set "Separator="
if not "!UserPath:~-1!" == ";" set "Separator=;"
set "PathCheck=!UserPath!%Separator%"
if "!PathCheck:%CD%;=!" == "!PathCheck!" (
    set "PathToSet=!UserPath!%Separator%%CD%"
    set "UseSetx=1"
    if not "!PathToSet:~1024,1!" == "" set "UseSetx="
    if not exist %SystemRoot%\System32\setx.exe set "UseSetx="
    if defined UseSetx (
        %SystemRoot%\System32\setx.exe Path "!PathToSet!" >nul
    ) else (
        set "ValueType=REG_EXPAND_SZ"
        if "!PathToSet:%%=!" == "!PathToSet!" set "ValueType=REG_SZ"
        %SystemRoot%\System32\reg.exe ADD "HKCU\Environment" /f /v Path /t !ValueType! /d "!PathToSet!" >nul
    )
)
endlocal
endlocal

此解决方案的缺点是用户 PATH在当前目录为第一个C:\Temp时在下一次运行批处理文件C:\Temp\Other FolderC:\Temp;C:\Temp\Other Folder;C:\Temp\One More Folder >批处理文件的第三次执行.

The disadvantage of this solution is a user PATH being finally for example C:\Temp;C:\Temp\Other Folder;C:\Temp\One More Folder when current directory is first C:\Temp, on next run of the batch file C:\Temp\Other Folder and C:\Temp\One More Folder on third execution of the batch file.

避免这种情况的解决方案是定义在下一个批处理文件MyAppPath中调用的特定于应用程序的环境变量,该变量始终在执行批处理文件时被覆盖.如果用户 PATH中尚不存在对环境变量MyAppPath的引用,则会向用户添加PATH.

The solution to avoid this is a definition of an application specific environment variable called in next batch file MyAppPath which is always overwritten on execution of the batch file. To the user PATH is added only the reference to the environment variable MyAppPath if not already existing in user PATH.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "UseSetx=1"
if not "%CD:~1024,1%" == "" set "UseSetx="
if not exist %SystemRoot%\System32\setx.exe set "UseSetx="
if defined UseSetx (
    %SystemRoot%\System32\setx.exe MyAppPath "%CD%" >nul
) else (
    %SystemRoot%\System32\reg.exe ADD "HKCU\Environment" /f /v MyAppPath /t REG_SZ /d "%CD%" >nul
)

set "UserPath="
for /F "skip=2 tokens=1,2*" %%N in ('%SystemRoot%\System32\reg.exe query "HKEY_CURRENT_USER\Environment" /v "Path" 2^>nul') do (
    if /I "%%N" == "Path" (
        set "UserPath=%%P"
        if defined UserPath goto CheckPath
    )
)

if exist %SystemRoot%\System32\setx.exe (
    %SystemRoot%\System32\setx.exe Path "%%MyAppPath%%" >nul
) else (
    %SystemRoot%\System32\reg.exe ADD "HKCU\Environment" /f /v Path /t REG_EXPAND_SZ /d "%%MyAppPath%%" >nul
)
endlocal
goto :EOF

:CheckPath
setlocal EnableDelayedExpansion
set "Separator="
if not "!UserPath:~-1!" == ";" set "Separator=;"
if "!UserPath:%%MyAppPath%%=!" == "!UserPath!" (
    set "PathToSet=!UserPath!%Separator%%%MyAppPath%%"
    set "UseSetx=1"
    if not "!PathToSet:~1024,1!" == "" set "UseSetx="
    if not exist %SystemRoot%\System32\setx.exe set "UseSetx="
    if defined UseSetx (
        %SystemRoot%\System32\setx.exe Path "!PathToSet!" >nul
    ) else (
        %SystemRoot%\System32\reg.exe ADD "HKCU\Environment" /f /v Path /t REG_EXPAND_SZ /d "!PathToSet!" >nul
    )
)
endlocal
endlocal

在这种情况下,存储在注册表中的用户 PATH始终仅包含%MyAppPath%,并且注册表值的类型为 REG_EXPAND_SZ .环境变量MyAppPath的值也存储在注册表中,但类型为 REG_SZ . MyAppPath的值在每次执行批处理文件时更新为当前目录路径.因此,每次从不同文件夹执行批处理文件时,注册表中的用户 PATH都不会越来越长.

In this case user PATH as stored in registry contains always just %MyAppPath% and registry value is of type REG_EXPAND_SZ. The value of environment variable MyAppPath is also stored in registry, but is of type REG_SZ. The value of MyAppPath is updated to current directory path on each execution of the batch file. So the user PATH in registry does not get longer and longer on each execution of a batch file from a different folder than before.

通常,如果在执行应用程序或套件中的任何应用程序时,其应用程序文件夹或其子文件夹之一必须位于本地 PATH中,则该应用程序或应用程序套件的编码不正确完全正确.应用程序或应用程序套件可以将其安装路径也存储在注册表中的其他位置,例如App Paths%APPDATA%子文件夹(与用户帐户相关的标准应用程序数据路径)中的文件中,以便下次运行时可以从中读取它.仅当该应用程序最有可能主要由用户在命令提示符窗口中执行时,安装程​​序软件包才应修改用户系统 PATH.

In general an application or application suite is not good coded if its application folder or one of its subfolders must be in local PATH on execution of the application or any application from the suite to work properly at all. The application or application suite can store its installation path also somewhere else in registry like App Paths or in a file in a subfolder of %APPDATA% (user account related standard application data path) from which it can be read on next run. An installer package should modify user or system PATH only if this application is most likely executed mainly from within a command prompt window by a user.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • reg /?
  • reg add /?
  • reg query /?
  • set /?
  • setlocal /?
  • setx /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • reg /?
  • reg add /?
  • reg query /?
  • set /?
  • setlocal /?
  • setx /?

以下内容也应阅读:

  • Wikipedia article about Windows Environment Variables.
  • Microsoft article about Using command redirection operators explaining >nul.
  • Answer on Where is "START" searching for executables? with details about App Paths.
  • Answer on What is the reason for "X is not recognized as an internal or external command, operable program or batch file"? with details about local, system and user PATH.
  • Answer on How to set PATH environment variable in batch file only once on Windows?
  • Answer on How can I use a .bat file to remove specific tokens from the PATH environment variable?

这篇关于将当前目录永久添加到Windows路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆