0

When you open sysdm.cpl -> Advanced -> Environment Variables. you can see that envs are divided into two section:

one is User Variables, another is System Variables, they both have a variable PATH in them.

Now if you run set inside the cmd prompt, it only shows one variable name called PATH and it contains the union of the above.

How do I explicitly set System Variable or User Variable in command prompt in a persistent manner?

Sajuuk
  • 121
  • 6

1 Answers1

0

Read and follow this exhaustive rojo's answer to similar question at StackOverflow: How to set PATH environment variable in batch file only once on Windows?.

Below is a batch/cmd script I formerly wrote to check directories in my %PATH% as well as in appropriate registry values under HKLM and HKCU. The script does not write anything in registry; it merely shows another possible approach to such task in pure batch/cmd and shows that its solution can't be trivial.

Usage:

  • testpath.bat (no parameter): checks %PATH% variable syntax; a bonus: shows redundant duplicities in %PATH% variable;
  • testpath.bat dir (note literal parameter DIR): displays a list of executables (as determined in PATHEXT variable) for each particular directory in %PATH%; a bonus: shows redundant and worthless directories in %PATH% variable:
@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
JosefZ
  • 1,564
  • 1
  • 10
  • 18
  • very rich source of information, but the link you provided seems to only set environment variable for once? – Sajuuk Feb 01 '18 at 04:05