1

In Windows, when each of the PATH variables need to be listed on a single line then the command to be used is for %a in ("%path:;=";"%") do @echo %~a can be executed.

I don't understand, what does "%path:;=";"%" do?

Simon Kissane
  • 4,373
  • 3
  • 34
  • 59
ACD
  • 69
  • 1
  • 8
  • basically it splits your path variable by using the delimiter ";", but it is interesting why `%path` does not take a second `%` like here `%path%` – Marged Oct 18 '15 at 09:58

2 Answers2

2

It takes the value of the %path% variable and substitutes each occurrence of ; character with ";". For more details see the output of set /? in command prompt.

MBu
  • 2,880
  • 2
  • 19
  • 25
2

In windows batch files, you can do string substitution in variables using the syntax

%var:search=replace%

In your case, the variable is path, the search is ; and the replacement is ";"

      v ......... search 
%path:;=";"%
        ^^^ ..... replace

That way each semicolon in the variable contents is replaced by the same semicolon, but with a double quote in each side. Including an starting and ending quotes around the expression

"%path:;=";"%"

every semicolon delimited element in the path variable is now quoted

 c:\windows ; c:\windows\system32 ; c:\windows\system32\wbem ; c:\other folder
v          vvv                   vvv                        vvv               v
"c:\windows";"c:\windows\system32";"c:\windows\system32\wbem";"c:\other folder"

Now there is a list of quoted strings separated by a semicolon delimiter.

This string is processed by a for command that will process each delimited element storing it inside the %a replaceable parameter and executing the code inside the do clause for each value, echoing to console the content of the replaceable parameter without quotes (%~a)

All this to be able to enumerate the different directories in the path variable because it probably will contain other characters (ex. spaces) that are handled as delimiters by the for command.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • This works as long as there are no quotes in the path itself and when no single directory of the path contains a semicolon (it's a legal character for a directory). ['Pretty print' windows %PATH% variable - how to split on ';' in CMD shell](http://stackoverflow.com/q/5471556/463115) – jeb Oct 18 '15 at 19:13
  • @jeb, Maybe I should have included a warning in the answer, but as the question is about the meaning of the expression I left it out. Thank you anyway. I already know your post, well, I borrowed the code for [another answer](http://stackoverflow.com/a/31358421/2861476). – MC ND Oct 18 '15 at 19:23