在Windows中将目录追加到PATH环境变量 [英] Append a directory to PATH Environment Variable in Windows

查看:670
本文介绍了在Windows中将目录追加到PATH环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有这个批处理文件,据说该文件将我的脚本附加到path变量

@echo OFF

setx path "%path%;%cd%\script.py"

但是我遇到一些问题.

  1. 附加我的script.py将导致路径大于1024个字符.因此输出警告.

警告:保存的数据被截断为1024个字符.

成功:指定的值已保存.

  1. 使用上面的代码将PATH从系统 PATH复制到用户 PATH,然后尝试附加script.py. (我在 user PATH中有其他目录,这些目录不在 system PATH中,并且使用脚本覆盖了这些目录.)

我的问题是,如何克服1024个字符的限制?

并且如何在不复制系统 PATH中的脚本的情况下,将script.py正确地附加到用户?

示例:

原始

用户路径:C:\ dirA; ...; C:\ dirB;

系统路径:C:\ dirC; ...; C:\ dirD;

运行脚本后...

期望

用户路径:C:\ dirA; ...; C:\ dirB; C:\ something \ script.py

系统路径:C:\ dirC; ...; C:\ dirD;

实际

用户路径:C:\ dirC; ...; C:\ dirD; C:\ somet(截断为1024个字符)

系统路径:C:\ dirC; ...; C:\ dirD;

解决方案

下一个脚本显示了一种可能的方法.

@ECHO OFF >NUL
SETLOCAL enableextensions
rem  enabledelayedexpansion
echo adding "%~1" to the user level HKCU\Environment /v Path
call :showReg old
call set "expanded=%~1"
if "%expanded%"=="" goto :usage
if not exist "%expanded%\" goto :usage
set "HKCU_type=REG_EXPAND_SZ"
set "HKCU_path="
for /F "tokens=1,2*" %%F in ('
  reg query HKCU\Environment /v Path 2^>NUL ^|findstr /I "path"
  ') do (
    set "HKCU_path=%%H"
    REG ADD HKCU\Environment /v Path /t %HKCU_type% /d %%H;%~1 /f >NUL
  ) 
if not defined HKCU_path (
    REG ADD HKCU\Environment /v Path /t %HKCU_type% /d %~1 /f >NUL
)
:endlocal
call :showReg new
ENDLOCAL
goto :eof

:usage
  echo      directory "%~1" ^("%expanded%"^) not found
goto :endlocal

:showReg
<NUL set /P "=%~1: "
reg query HKCU\Environment /v Path 2>NUL|findstr /I "path"|findstr /V /R "^$"
if errorlevel 1 echo not defined
goto :eof

提供的示例显示了添加尝试

  • 不存在的目录d:\FooBar(已拒绝);
  • 现有目录d:\bat(硬编码参考);
  • 现有目录%SystemRoot%(变量引用,在注册表中硬编码);
  • 现有目录^%windir^%(变量引用在注册表中保持可扩展状态).

输出:

==>31602391.bat d:\FooBar
adding "d:\FooBar" to the user level HKCU\Environment /v Path
old: not defined
     directory "d:\FooBar" ("d:\FooBar") not found
new: not defined

==>31602391.bat d:\test
adding "d:\test" to the user level HKCU\Environment /v Path
old: not defined
new:     Path    REG_EXPAND_SZ    d:\test

==>31602391.bat %SystemRoot%
adding "C:\Windows" to the user level HKCU\Environment /v Path
old:     Path    REG_EXPAND_SZ    d:\test
new:     Path    REG_EXPAND_SZ    d:\test;C:\Windows

==>31602391.bat ^%windir^%
adding "%windir%" to the user level HKCU\Environment /v Path
old:     Path    REG_EXPAND_SZ    d:\test;C:\Windows
new:     Path    REG_EXPAND_SZ    d:\test;C:\Windows;%windir%

==>

So, I have this batch file that supposedly appends my script to the path variable

@echo OFF

setx path "%path%;%cd%\script.py"

But I encounter a few problems.

  1. Appending my script.py would cause the path to be greater than 1024 characters. Thus outputting a warning.

WARNING: The data being saved is truncated to 1024 characters.

SUCCESS: Specified value was saved.

  1. Using the code above copies the PATH from system PATH to the user PATH then tries to append script.py. (I have other directories in the user PATH that are not in the system PATH and using the script overwrites those.)

My question is, how do I overcome the 1024 character limitation?

And how can I properly append script.py to the user PATH without copying the ones from the system PATH?

Example:

Original

USER PATH: C:\dirA;...;C:\dirB;

SYSTEM PATH: C:\dirC;...;C:\dirD;

After running the script...

Expected

USER PATH: C:\dirA;...;C:\dirB;C:\something\script.py

SYSTEM PATH: C:\dirC;...;C:\dirD;

Actual

USER PATH: C:\dirC;...;C:\dirD;C:\somet (truncated to 1024 characters)

SYSTEM PATH: C:\dirC;...;C:\dirD;

解决方案

Next script shows a possible approach.

@ECHO OFF >NUL
SETLOCAL enableextensions
rem  enabledelayedexpansion
echo adding "%~1" to the user level HKCU\Environment /v Path
call :showReg old
call set "expanded=%~1"
if "%expanded%"=="" goto :usage
if not exist "%expanded%\" goto :usage
set "HKCU_type=REG_EXPAND_SZ"
set "HKCU_path="
for /F "tokens=1,2*" %%F in ('
  reg query HKCU\Environment /v Path 2^>NUL ^|findstr /I "path"
  ') do (
    set "HKCU_path=%%H"
    REG ADD HKCU\Environment /v Path /t %HKCU_type% /d %%H;%~1 /f >NUL
  ) 
if not defined HKCU_path (
    REG ADD HKCU\Environment /v Path /t %HKCU_type% /d %~1 /f >NUL
)
:endlocal
call :showReg new
ENDLOCAL
goto :eof

:usage
  echo      directory "%~1" ^("%expanded%"^) not found
goto :endlocal

:showReg
<NUL set /P "=%~1: "
reg query HKCU\Environment /v Path 2>NUL|findstr /I "path"|findstr /V /R "^$"
if errorlevel 1 echo not defined
goto :eof

Provided examples show attempts to add

  • a non existent directory d:\FooBar (rejected);
  • an existent directory d:\bat (hard-coded reference);
  • an existent directory %SystemRoot% (variable reference, hard-coded in the registry);
  • an existent directory ^%windir^% (variable reference keeps expandable in the registry).

Output:

==>31602391.bat d:\FooBar
adding "d:\FooBar" to the user level HKCU\Environment /v Path
old: not defined
     directory "d:\FooBar" ("d:\FooBar") not found
new: not defined

==>31602391.bat d:\test
adding "d:\test" to the user level HKCU\Environment /v Path
old: not defined
new:     Path    REG_EXPAND_SZ    d:\test

==>31602391.bat %SystemRoot%
adding "C:\Windows" to the user level HKCU\Environment /v Path
old:     Path    REG_EXPAND_SZ    d:\test
new:     Path    REG_EXPAND_SZ    d:\test;C:\Windows

==>31602391.bat ^%windir^%
adding "%windir%" to the user level HKCU\Environment /v Path
old:     Path    REG_EXPAND_SZ    d:\test;C:\Windows
new:     Path    REG_EXPAND_SZ    d:\test;C:\Windows;%windir%

==>

这篇关于在Windows中将目录追加到PATH环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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