Windows% PATH% variable - how to split into ';' in the cmd shell again

I just checked out https://stackoverflow.com/a/212960/ which was very useful and worked great on Windows XP. But using Windows 7 it does not work for some unclear reason.

The PATH variable looks like this:

 C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\QuickTime\QTSystem\ 

It obviously contains \, as well as the semicolons that I use to separate in the package that contains this FOR loop:

  FOR /F "delims=;" %%A IN ("%PATH%") DO ( echo %%A ) 

Execution does not cause an error, but provides only one (first) token

 C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common 

I had no idea why FOR completes and plays with several options that were offered on the network, but no one did the job.

Any help would be greatly appreciated.

Christian

+7
source share
2 answers

You could do it that way.

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

( Source )

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

+14
source

Combining everything that has been learned on this and various other stackoverflow pages, OP can be expanded to:

How to ensure that the PATH variable has unique values?

What 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 variable REM 3: set the VARNAME to this newly unique-ified collection. REM REM From: various StackOverflow pages: REM http://stackoverflow.com/questions/5471556/pretty-print-windows-path-variable-how-to-split-on-in-cmd-shell REM http://stackoverflow.com/questions/3262287/make-an-environment-variable-survive-endlocal REM http://stackoverflow.com/questions/14879105/windows-path-variable-how-to-split-on-in-cmd-shell-again REM :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:

  • to split the PATH loop into ';' and proper quotes management
    • for a loop looking through all previously saved paths
    • extends the array only if it is a new path to add
  • glue the array package together, clearing the array variable when we go
  • replace path and clear temporary variables
  • Return from function without errors.

called of course with

 set PATH=%PATH%;%MY_PATH_ADDITIONS% call :make_path_unique PATH "%PATH%" 
+1
source

All Articles