如何以持久的方式在命令提示符中的系统变量或用户变量中明确设置 %PATH%?

如何以持久的方式在命令提示符中的系统变量或用户变量中明确设置 %PATH%?

当您打开 sysdm.cpl -> 高级 -> 环境变量时,您可以看到环境分为两部分:

一个是User Variables,另一个是System Variables,它们都有一个变量PATH

现在,如果您在提示符set内运行cmd,它只会显示一个名为的变量名PATH,它包含上述的并集。

如何以持久的方式明确设置System Variable或在命令提示符中?User Variable

答案1

阅读并关注这是 rojo 的详尽回答StackOverflow 上的类似问题:如何在 Windows 上仅用批处理文件设置一次 PATH 环境变量?

下面是我以前编写的批处理/cmd 脚本查看目录中%PATH%以及 和 下的相应注册表值中HKLMHKCU该脚本不会在注册表中写入任何内容;它只是展示了完成此类任务的另一种可能方法。在纯批处理/cmd中并表明它的解并不简单。

用法:

  • testpath.bat(无参数):检查 %PATH%变量语法;奖金: 显示冗余%PATH%变量重复
  • testpath.bat dir(笔记文字参数): 显示每个特定目录的DIR可执行文件列表(由变量确定) ;PATHEXT%PATH%奖金:显示冗余和%PATH%变量中的无价值目录
@ECHO OFF
SETLOCAL enableextensions enabledelayedexpansion
echo --- %date% %time% %~nx0 %*  
if /I "%~1" EQU "dir" (
    set path
    Call :printPath path "" dir
    ENDLOCAL
    goto :eof
)

set pathext

Call :duplicity pathext

Call :printPath path ""

rem debugging set "wrongpath=%path%;%SystemRoot%\\"
rem debugging Call :duplicity wrongpath

Call :duplicity path

set "HKCU_type="
set "HKCU_path="
for /F "tokens=2*" %%G in (
  'reg query HKCU\Environment /v Path 2^>NUL ^|findstr /I "path"'
  ) do (
    set "HKCU_type=%%G"
    set "HKCU_path=%%H"
  ) 
Call :printPath HKCU_path %HKCU_type%
if /I "%HKCU_type%"=="REG_EXPAND_SZ" Call :printPath HKCU_path %HKCU_type% Expanded

set "HKLM_type="
set "HKLM_path="
set "qqqq=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
for /F "tokens=2*" %%G in ('reg query "%qqqq%" /v Path^|findstr /I "path"'
  ) do (
    set "HKLM_type=%%G" 
    set "HKLM_path=%%H" 
  ) 
Call :printPath HKLM_path %HKLM_type%
if /I "%HKLM_type%"=="REG_EXPAND_SZ" Call :printPath HKLM_path %HKLM_type% Expanded

:dirOnly
Call :deflatePath HKLM_path %HKLM_type%
    rem echo "!NewPath:%%=%%%%!"

pause
set HK
ENDLOCAL&call set "NewPath=%NewPath%"
goto :eof

:printPath
echo(
echo %~0 %~1 %~2 %~3
if "!%~1!" NEQ "" (
  set "ggg="!%~1:;=" "!""
  rem set "ggg=!ggg:\"="!"
  for %%G in (!ggg!) do (
    if /I "%~3" NEQ "expanded" (
        if /I "%~3" EQU "dir" (
            echo(
            echo %~0 %~1 %~2 %%~G
            set "_partpath=%%~G"
            if /I "!_partpath:%SystemRoot%=!" EQU "%%~G" (
                dir /B /A:-D "%%~G" | findstr /I "%pathext:;=$ %$"
                rem timeout /T 3 /NOBREAK >NUL
            ) else (
                echo %~0 %~1 %~2 System default 
            )
        ) else (
            echo %%~G
        ) 
    ) else (
        call echo %%~G
    )
  )
  echo(
  echo tested using next findstr regex: 
  echo "%pathext:;=$ %$"
)
goto :eof

:duplicity
echo(
echo %~0 %~1 %~2
set /A "ii=0"
set "ggg="!%~1:;=" "!""
set "ggg=!ggg:\"="!"
for %%G in (!ggg!) do (
  set /A "ii+=1"
  set /A "jj=0"
  for %%g in (!ggg!) do (
    set /A "jj+=1"
    if /I "%%~G"=="%%~g" if !ii! LSS !jj! echo !ii!, !jj!: %%~g 
  )
)
goto :eof

:deflatePath
echo(
echo %~0 %~1 %~2
set "ggg="!%~1:;=" "!""
rem set "ggg=!ggg:\"="!"
set "NewPath="
for %%G in (!ggg!) do (
  set "item=%%~G"
  set "meti="
  call :deflateItem "ProgramFiles(x86)"
  if defined meti (
    rem echo # !item!
  ) else (
    call :deflateItem "ProgramFiles"
    if defined meti (
      rem echo # !item!
    ) else (
      call :deflateItem "SystemRoot"
      if defined meti (
        rem echo # !item!
      ) else (
        rem echo = !item:%%=%%%%!
      )
    )
  )
  if defined NewPath (
    set "NewPath=!NewPath!;!item!"
  ) else (
    set "NewPath=!item!"
  )
)
echo !NewPath!
rem reg delete HKCU\Environment /v NewPath /f
rem setx NewPath "!NewPath!"
rem WARNING: The data being saved is truncated to 1024 characters.
rem reg query HKCU\Environment /v NewPath|findstr /I "NewPath"
goto :eof

:deflateItem
  set "meti=!%~1!"
  if "!meti!"=="!item!" (
    set "item=%%%~1%%"
  ) else (
    set "meti=!item:%meti%\=!"
    if "!meti!" == "!item!" (
      set "meti="
    ) else (
      set "item=%%%~1%%\!meti!"
    )
  )
goto :eof

相关内容