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
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.
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.
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% ".
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.
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.
;
are replaced with ^;^;
set var=%var:"=""%"
(The missing quote is the key!).var=foo & bar;;baz<>gak;;"semi^;^;colons^;^;^&embedded";;foo again!;;
...;;
and inside ^;^;
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With