0

I'm just starting with batch files, and I'm trying to loop over my Path environment variable with this script:

for %%A in %PATH% do echo %%A 

ant I'm getting the error in the title.

While running set I see that path has a value, and the "c:\ant-1.8.4\bin" is what I'm looking for.

I tried playing around with it, and even this:

for %%A in %PATH% do echo TEST

fails with the same error.

Thanks!

edit - clarification In the end, I want to do a small manipulation on each entry, so Magoo's answer doesn't really solve the problem for me.

orirab
  • 2,915
  • 1
  • 24
  • 48
  • possible duplicate of ['Pretty print' windows %PATH% variable - how to split on ';' in CMD shell](http://stackoverflow.com/questions/5471556/pretty-print-windows-path-variable-how-to-split-on-in-cmd-shell) – MC ND Jul 01 '14 at 09:23
  • I took a look at his question and the answers, but the thing that troubled him is that the command itself printed ("echo something"). My problem is that it doesn't cycle through the entire path. – orirab Jul 01 '14 at 09:31
  • Problems: 1: You forget the parenthesis for the `in (...) do`. 2: `for` will split the line in the spaces, commas, semicolons, ... so you will probably not have the output you search without using some of the indications of the pointed question. Sorry. Conceptually it seems a simple problem, but really it is not as simple as it seems. – MC ND Jul 01 '14 at 09:41
  • problem1 - I thought the parenthesis were for when there are multiple things to be done, and you want to take care of them all? problem2 - So you're saying the problem is the spaces (and similar things)? Is this reflected in the error message? anyway, thanks a lot. I'll try looking more extensively into that question. – orirab Jul 01 '14 at 10:00
  • There are two different sets of parenthesis. `for %%x in (set) do (commands)`. The first set of parenthesis delimit the set of elements being processed and are needed always. The second set of parenthesis are needed only if the list of commands to execute for each of the iterations of the `for` extends over several lines. And yes, spaces, `&<>!;:,"` and some more need to be taken into consideration when dealing with `for` command and file/folder paths. – MC ND Jul 01 '14 at 10:06
  • Thanks, after playing around with it I managed. Is there a way for me to "accept" your comment as an answer? – orirab Jul 01 '14 at 13:11

2 Answers2

1
@ECHO OFF
SETLOCAL
ECHO %path:;=&ECHO(%

This should show the elements of your path one to a line. Your question isn't clear - what do you actually want to do?

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • In the end, I want to do a small check against every entry in the Path, so your suggestion won't work for me. – orirab Jul 01 '14 at 12:32
  • Try specifying what you need to make it "work" for you. Sorry - my crystal ball is still in the shop. – Magoo Jul 01 '14 at 12:47
0

Adapting answer from comments in original question

There are two different sets of parenthesis in a for command:

for %%x in (set) do (commands)

The first set of parenthesis delimit the set of elements being processed and are needed always.

The second set of parenthesis are needed only if the list of commands to execute for each of the iterations of the for command extends over several lines.

And yes, spaces, &<>!;:," and some more special characters need to be taken into consideration when dealing with for command and file/folder paths.

MC ND
  • 69,615
  • 8
  • 84
  • 126