Split The Output of PATH Environment Variable

batchbatch filecmd.exe

In CMD, when i type PATH, the output:

Path=C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\;C:\Windows\system32;C:\Windows;

I need to split each path with new line, then the output to become:

C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\
C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\
C:\Windows\system32
C:\Windows

I can do this in PowerShell with $Env:Path.Split(';'), or calling it from CMD powershell -c "$Env:Path.Split(';')".

How I do this in CMD it self?

I Tried:

For /F "Tokens=1* Delims=;" %A in ('%PATH%') do @Echo %A %B
For /F "Tokens=1* Delims=;" %A in (%PATH%) do @Echo %A %B

giving error: \Intel\Intel(R) was unexpected at this time.


Thanks to T3RR0R.

@For %G in ("%PATH:;=" "%")Do @Echo(%~G

Best Answer

You can use a FOR loop. Save this into BATCH-file (eg. split_path.bat)

@echo off
rem Get the PATH variable
set "PATH_STR=%PATH%"

rem Use FOR loop to split and print each path on a new line
for %%a in ("%PATH_STR:;=" "%") do (
    echo %%~a
)

It should split the PATH variable based on the semicolon delimiter and print each path on a new line.

Related Question