将当前目录永久添加到 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).
我怎样才能以最简单的方式完成我的目标?
How can I 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而不只是指定的文件夹路径?修改系统或用户 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
eg.exe query "HKEY_CURRENT_USEREnvironment" /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%System32setx.exe set "UseSetx="
if defined UseSetx (
%SystemRoot%System32setx.exe Path "%CD%" >nul
) else (
%SystemRoot%System32
eg.exe ADD "HKCUEnvironment" /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%System32setx.exe set "UseSetx="
if defined UseSetx (
%SystemRoot%System32setx.exe Path "!PathToSet!" >nul
) else (
set "ValueType=REG_EXPAND_SZ"
if "!PathToSet:%%=!" == "!PathToSet!" set "ValueType=REG_SZ"
%SystemRoot%System32
eg.exe ADD "HKCUEnvironment" /f /v Path /t !ValueType! /d "!PathToSet!" >nul
)
)
endlocal
endlocal
此解决方案的缺点是 user PATH
最终例如 C:Temp;C:TempOther Folder;C:TempOne More Folder
当当前目录是第一个 C:Temp
时,在批处理文件的下一次运行 C:TempOther Folder
和 C:TempOne More Folder
批处理文件的第三次执行.
The disadvantage of this solution is a user PATH
being finally for example C:Temp;C:TempOther Folder;C:TempOne More Folder
when current directory is first C:Temp
, on next run of the batch file C:TempOther Folder
and C:TempOne More Folder
on third execution of the batch file.
避免这种情况的解决方案是定义在下一个批处理文件 MyAppPath
中调用的特定于应用程序的环境变量,它总是在批处理文件执行时被覆盖.用户 PATH
仅添加对环境变量 MyAppPath
的引用,如果 user 中尚不存在 >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%System32setx.exe set "UseSetx="
if defined UseSetx (
%SystemRoot%System32setx.exe MyAppPath "%CD%" >nul
) else (
%SystemRoot%System32
eg.exe ADD "HKCUEnvironment" /f /v MyAppPath /t REG_SZ /d "%CD%" >nul
)
set "UserPath="
for /F "skip=2 tokens=1,2*" %%N in ('%SystemRoot%System32
eg.exe query "HKEY_CURRENT_USEREnvironment" /v "Path" 2^>nul') do (
if /I "%%N" == "Path" (
set "UserPath=%%P"
if defined UserPath goto CheckPath
)
)
if exist %SystemRoot%System32setx.exe (
%SystemRoot%System32setx.exe Path "%%MyAppPath%%" >nul
) else (
%SystemRoot%System32
eg.exe ADD "HKCUEnvironment" /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%System32setx.exe set "UseSetx="
if defined UseSetx (
%SystemRoot%System32setx.exe Path "!PathToSet!" >nul
) else (
%SystemRoot%System32
eg.exe ADD "HKCUEnvironment" /f /v Path /t REG_EXPAND_SZ /d "!PathToSet!" >nul
)
)
endlocal
endlocal
在这种情况下,user PATH
存储在注册表中总是只包含 %MyAppPath%
并且注册表值的类型为 REG_EXPAND_SZ强>.环境变量 MyAppPath
的值也存储在注册表中,但类型为 REG_SZ.每次执行批处理文件时,MyAppPath
的值都会更新为当前目录路径.因此,每次执行来自不同文件夹的批处理文件时,注册表中的 user 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.
通常,如果在应用程序或任何应用程序执行时其应用程序文件夹或其子文件夹之一必须位于 local 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/?
转到/?
if/?
reg/?
reg add/?
reg 查询/?
set/?
setlocal/?
setx/?
还应阅读以下内容:
- 关于Windows 环境变量的维基百科文章.
- 关于 使用命令重定向运算符解释
>nul
. - 回答关于START"在哪里搜索可执行文件?以及有关
应用路径
的详细信息. - 回答X 未被识别为内部或外部命令、可运行的程序或批处理文件"的原因是什么? 包含有关本地、系统和用户
PATH
的详细信息. - 如何在 Windows 上只在批处理文件中设置一次 PATH 环境变量?
- 回答 如何使用 .bat 文件从 PATH 环境变量中删除特定标记?
- 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?