windows %PATH% variable - how to split on ';' in CMD shell again [duplicate] windows %PATH% variable - how to split on ';' in CMD shell again [duplicate] windows windows

windows %PATH% variable - how to split on ';' in CMD shell again [duplicate]


You could do it this way.

for %%A in ("%path:;=";"%") do (    echo %%~A)

(Source)

The problem with the way you have it is that, using the for /F switch, %%A only specifies the first token. You would have to do for /f "tokens=1-9 delims=;" %%A in ("%PATH%") and read in %%A through %%I that way.


Combining things learned on this and various other stackoverflow pages, the OP can be extended to:

How to ensure the PATH variable has unique values?

Which can be done this way, using array variables:

REM usage: call :make_path_unique VARNAME "%VARNAME%"REM 1: splits VARNAME on ';' and builds an array of unique values (case insensitive)REM 2: glues the array back into a single variableREM 3: set the VARNAME to this newly unique-ified collection.REMREM From: various StackOverflow pages:REM     http://stackoverflow.com/questions/5471556/pretty-print-windows-path-variable-how-to-split-on-in-cmd-shellREM     http://stackoverflow.com/questions/3262287/make-an-environment-variable-survive-endlocalREM     http://stackoverflow.com/questions/14879105/windows-path-variable-how-to-split-on-in-cmd-shell-againREM:make_path_unique  setlocal EnableDelayedExpansion  set VNAME=%~1  set VPATH=%~2  set I=0  for %%A in ("%VPATH:;=";"%") do (    set FOUND=NO    for /L %%B in (1,1,!I!) do (        if /I "%%~A"=="!L[%%B]!" set FOUND=YES    )    if NO==!FOUND! (        set /A I+=1        set L[!I!]=%%~A    )  )  set FINAL=!L[1]!  for /L %%n in (2,1,!I!) do (    set FINAL=!FINAL!;!L[%%n]!    set L[%%n]=  )  for %%P in ("!FINAL!") do (    endlocal    set %VNAME%=%%~P  )  exit /b 0

Summary of steps:

  1. for loop splitting PATH at ';' and properly managing quotes
    1. for loop looking at all previously stored paths
    2. only extend the array if this is a new path to be added
  2. glue the array pack together, clearing the array variable as we go
  3. replace the path and clear the temporary variables
  4. return from function with no errors.

invoked, of course, with:

set PATH=%PATH%;%MY_PATH_ADDITIONS%call :make_path_unique PATH "%PATH%"