Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'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 
like image 504
sam Avatar asked Mar 29 '11 11:03

sam


People also ask

How do you separate paths in Environment Variables?

In the Environment Variables window (pictured below), highlight the Path variable in the System variables section and click the Edit button. Add or modify the path lines with the paths you want the computer to access. Each directory path is separated with a semicolon, as shown below.

How do I find my path variable in CMD?

Select Start select Control Panel. double click System and select the Advanced tab. Click Environment Variables. In the section System Variables find the PATH environment variable and select it.

How do I echo a variable in Windows command prompt?

To reference a variable in Windows, use %varname% (with prefix and suffix of '%' ). For example, you can use the echo command to print the value of a variable in the form " echo %varname% ".

How do I view a variable in CMD?

On Windows Select Start > All Programs > Accessories > Command Prompt. In the command window that opens, enter set. A list of all the environment variables that are set is displayed in the command window.


2 Answers

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:\qt\4.6.3\bin;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.
like image 136
jeb Avatar answered Oct 01 '22 05:10

jeb


A simple one liner to prettying printing the PATH environment variable:

ECHO.%PATH:;= & ECHO.% 

If your PATH was equal to A;B;C the above string substitution will change this to ECHO.A & ECHO.B & ECHO.C and execute it all in one go. The full stop prevents the "ECHO is on" messages from appearing.

like image 45
Stephen Quan Avatar answered Oct 01 '22 07:10

Stephen Quan