I looked through a few solutions here and found an almost working script to append to the user Environment Variable PATH without overwriting the existing path and not clutter it with system’s EV’s PATH. I wanted to add 4 paths, 2 from Python and 2 from Anaconda. The first 3 locations work perfectly (Python311, Python312, conda.exe), but it keeps duplicating the 4th path (_conda.exe) even though the script sequence is the same. Thoughts?
@echo off
REM usage: append_user_path "path"
SET Key="HKCUEnvironment"
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
echo Add %appdata%PythonPython311Scripts to PATH
@echo off
set "newPath=%appdata%PythonPython311Scripts"
echo %CurrPath% | findstr /i /c:"%newPath%" >nul
if errorlevel 1 (
setx PATH "%CurrPath%;%newPath%"
echo Path updated successfully.
) else (
echo Path already contains the directory.
)
REM usage: append_user_path "path"
SET Key="HKCUEnvironment"
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
echo Add %appdata%PythonPython312Scripts to PATH
@echo off
set "newPath=%appdata%PythonPython312Scripts"
echo %CurrPath% | findstr /i /c:"%newPath%" >nul
if errorlevel 1 (
setx PATH "%CurrPath%;%newPath%"
echo Path updated successfully.
) else (
echo Path already contains the directory.
)
REM usage: append_user_path "path"
SET Key="HKCUEnvironment"
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
echo Add %ProgramFiles%ContinuumAnaconda3conda.exe to PATH
@echo off
set "newPath=%ProgramFiles%ContinuumAnaconda3conda.exe"
echo %CurrPath% | findstr /i /c:"%newPath%" >nul
if errorlevel 1 (
setx PATH "%CurrPath%;%newPath%"
echo Path updated successfully.
) else (
echo Path already contains the directory.
)
REM usage: append_user_path "path"
SET Key="HKCUEnvironment"
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
echo Add %ProgramFiles%ContinuumAnaconda3_conda.exe to PATH
@echo off
set "newPath=%ProgramFiles%ContinuumAnaconda3_conda.exe"
echo %CurrPath% | findstr /i /c:"%newPath%" >nul
if errorlevel 1 (
setx PATH "%CurrPath%;%newPath%"
echo Path updated successfully.
) else (
echo Path already contains the directory.
)
Pause