Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
919 views
in Technique[技术] by (71.8m points)

batch file - 'Pretty print' windows %PATH% variable - how to split on ';' in CMD shell

I want to run a simple one-liner in the Windows CMD prompt to print my %PATH% variable, one entry per line.

I tried this: for /f "delims=;" %a in ("%path%") do echo %a but this only prints the first entry:

Z:>for /f "delims=;" %a in ("%path%") do echo %a

Z:>echo c:python25.
c:python25.

Also as you can see from the output above, this is also printing the echo %a command as well as the output. Is there any way to stop this?

If I try a similar command, I get all the entries, but still get the echo %a output spamming the results. I don't understand why the following prints all entries, but my attempt on %PATH% doesn't. I suspect I don't understand the /F switch.

Z:>for %a in (1 2 3) do echo %a

Z:>echo 1
1

Z:>echo 2
2

Z:>echo 3
3
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The simple way is to use

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

This works for all without ; in the path and without " around a single element
Tested with path=C:qt4.6.3in;C:Program Files;C:documents & Settings

But a "always" solution is a bit complicated
EDIT: Now a working variant

@echo off
setlocal DisableDelayedExpansion
set "var=foo & bar;baz<>gak;"semi;colons;^&embedded";foo again!;throw (in) some (parentheses);"unmatched ;-)";(too"

set "var=%var:"=""%"
set "var=%var:^=^^%"
set "var=%var:&=^&%"
set "var=%var:|=^|%"
set "var=%var:<=^<%"
set "var=%var:>=^>%"

set "var=%var:;=^;^;%"
rem ** This is the key line, the missing quote is intended
set var=%var:""="%
set "var=%var:"=""%"

set "var=%var:;;="";""%"
set "var=%var:^;^;=;%"
set "var=%var:""="%"
set "var=%var:"=""%"
set "var=%var:"";""=";"%"
set "var=%var:"""="%"

setlocal EnableDelayedExpansion
for %%a in ("!var!") do (
    endlocal
    echo %%~a
    setlocal EnableDelayedExpansion
)

What did I do there?
I tried to solve the main problem: that the semicolons inside of quotes should be ignored, and only the normal semicolons should be replaced with ";"

I used the batch interpreter itself to solve this for me.

  • First I have to make the string safe, escaping all special characters.
  • Then all ; are replaced with ^;^;
  • and then the trick begins with the line
    set var=%var:"=""%" (The missing quote is the key!).
    This expands in a way such that all escaped characters will lose their escape caret:
    var=foo & bar;;baz<>gak;;"semi^;^;colons^;^;^&embedded";;foo again!;;...
    But only outside of the quotes, so now there is a difference between semicolons outside of quotes ;; and inside ^;^;.
    Thats the key.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...