@Echo off
REM #Doc: 24.09.2024/J.Fes see PathaddEnv_Help.txt
REM #LUp: 15.11.2024/J.Fes Echo to Echo2
REM #LUp: 26.10.2025/J.Fes Info print Moved to begin deactivate Print result at end of list

set PathaddEnv$tmp=
set PathaddEnv$No=
set ResVar=%~1
set ResInsertAtBeginOrEnd=@X
if /I "%~2" equ "@B" set ResInsertAtBeginOrEnd=%~2& shift
if /I "%~2" equ "@E" set ResInsertAtBeginOrEnd=%~2& shift

Call Echo2 "Path values set to Environvar" "%ResVar%"

:Start
 shift
 if "%~1" equ "" goto :Done
 call :PathaddEnv "%~1"
 goto :Start

:Done
 setlocal enableDelayedExpansion
 if /I "%ResInsertAtBeginOrEnd%" equ "@B" set Res=!%ResVar%!;%PathaddEnv$tmp%
 if /I "%ResInsertAtBeginOrEnd%" equ "@E" set Res=%PathaddEnv$tmp%;!%ResVar%!
 if /I "%ResInsertAtBeginOrEnd%" equ "@X" set Res=%PathaddEnv$tmp%
 set PathaddEnv$tmp=
 set PathaddEnv$No=

REM  Call Echo2 "Path values" "%Res%"
 endlocal& set %ResVar%=%Res%
goto :eof

rem ------------------------------------------------------
:PathaddEnv
if "%~1" equ "" goto :eof
set /a PathaddEnv$No=%PathaddEnv$No% + 1
set p$_DP=%~1
if "%p$_DP:~-1%" equ ";" set p$_DP=%p$_DP:~0,-1%
if "%p$_DP%" equ "" goto :eof
if exist "%p$_DP%" (
 Call Echo2 "add Path-%PathaddEnv$No%:OK" "%p$_DP%"
 if defined     PathaddEnv$tmp set PathaddEnv$tmp=%PathaddEnv$tmp%;%p$_DP%
 if not defined PathaddEnv$tmp set PathaddEnv$tmp=%p$_DP%
) else (
 Call Echo2 "add Path-%PathaddEnv$No%:ERR path not exist" "%p$_DP%"
)
set %p$_DP%=
goto :eof
