Search code examples
windowscmdechodoskey

How to create a DOSKEY alias command to print Windows %PATH% with every entry on new line?


I have the same task as Print Windows %PATH% with every entry on new line

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...

which is achievable by echo %path:;=&echo.% as explained here

And I want to assign it to a command alias using DOSKEY

But the following script is failing for me

DOSKEY list=echo %path:;=&echo.%

Which just prints all of the paths while I'm defining the DOSKEY, and after that, when I try to invoke list command, it just doesn't work.


Solution

  • As mentioned by @eryksun you'll need to escape special chars.

    Update: And to prevent a premature expanding - will use delayed expansion.

    doskey list=cmd /v:on /c "for %p in ("!path:;=" "!") do @echo %~p"
    

    An explanation of how this work: we can't use directly solution with echo ^%path:;=^&echo.^% because it's stored in macros already expanded. To prevent this I've tried to use delayed expansion (the part with cmd /v:on /c). It seemed almost working but without splitting on new lines (adding & for some reason doesn't work with delayed expansion).

    Then I started to search a way to split !path! and found a neat trick with replacing ; to " " (double quoted space) ("!path:;=" "!"). This splitting was good enough for a plain for (without /f option). The rest is obvious.