21

I have a task: print all entries of %PATH% variable on new line. For example:

C:\Program Files\
C:\Windows
C:\Windows\System32

and so on...

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Groosha
  • 2,897
  • 2
  • 22
  • 38
  • 1
    It sounds like an interesting task. What have you tried so far to actually make it happen? – Ken White Apr 08 '13 at 17:28
  • 2
    possible duplicate of ['Pretty print' windows %PATH% variable - how to split on ';' in CMD shell](http://stackoverflow.com/questions/5471556/pretty-print-windows-path-variable-how-to-split-on-in-cmd-shell) – dbenham Apr 08 '13 at 17:35

2 Answers2

72

Solved: I used echo %path:;=&echo(%

Source: How do I view/see the PATH in a windows environment? (the original link is dead, now linking to WebArchive)

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
Groosha
  • 2,897
  • 2
  • 22
  • 38
  • 2
    @SteveValliere Sorry for VERY slow answer, but here's the source (for history: http://geekswithblogs.net/AskPaula/archive/2008/12/18/128035.aspx#548528) – Groosha Feb 03 '16 at 20:15
  • 2
    Great answer! I would love an explanation on how and why it works :) – Nom1fan Nov 21 '17 at 08:58
  • 2
    @Nom1fan it's explained how it works here: https://superuser.com/questions/1369142/how-this-windows-cmd-command-works/1369153#1369153 – yekanchi Oct 23 '18 at 08:51
  • 1
    The initial `echo` should also be `echo.` for fully consistent behaviour across all elements of the path including the first. – JdeBP Oct 23 '18 at 11:32
  • 2
    Great answer. Sorting the output can be handy, e.g,: ```cmd /c "echo %path:;=&echo.%" | sort``` – Lars Dec 05 '21 at 09:33
  • I use this command through a macro. From .bat file: `DOSKEY pathlist = echo %%PATH:;=^&echo.%%` – Ivan_a_bit_Ukrainivan Apr 26 '22 at 15:20
  • Re `echo.` (with dot) see [_SS64 – Echo a blank line_](https://ss64.com/nt/echo.html#blank): "_Other [than ':'] command delimiters are available but have buggy side effects, for example `Echo.` will search for a file named "echo" (or echo.exe) in the current directory, if such a file is found that will raise an error. If the 'echo' file does not exist then the command does work, but this still makes `Echo.` slightly slower than `echo:` (In recent builds of WIndows 10 `Echo.` will always throw an error.)_" – Gerold Broser Feb 20 '23 at 18:29
  • 1
    You should use `echo(` instead, even `echo:` could fail with some arguments, like in `PATH=C:\windows;\..\..\..\Windows\System32\calc.exe` [ECHO. fails ...](https://www.dostips.com/forum/viewtopic.php?f=3&t=774&start=0#p4554) – jeb Feb 20 '23 at 22:00
1
@ECHO OFF
SETLOCAL 
SET count=1
:loop
FOR /f "tokens=%count%delims=;" %%i IN ("%path%") DO ECHO %%i&SET /a count+=1&GOTO loop
ECHO %count% entries found

Not hard - simply use TOKENS to select the token number until they run out. May want to echo %%~i to strip quoted paths if you want. count displayed because it's there.

Magoo
  • 77,302
  • 8
  • 62
  • 84