baz was unexpected at this time.
What happened here?Well, look at the following code:
ECHOcommand to end? Right, just before the closing paren. Let's rewrite that line:
IFblock?A straightforward idea of writing this would be
ECHOtells us that it prints the whole line, unless interrupted by things like
>. And of course
ECHO :-)works just fine from the command line. Put into an
IFblock, however, yields an error:
IFblock, kinda like the first
*/will end a comment in C. Cmd's behavior here makes sense for single-line
IFs but in my opinion it is a bit annoying when going multi-line.Now we know what to look for, it'd be nice to know how to prevent this in future. The first idea would be to move the interior of the block in a subroutine:
^, so the code would look the following:
Note, that if you have the parentheses in a variable (such as the 32-bit program files directory on x64 systems), then you need to either use delayed expansion or a subroutine (to avoid the parentheses).
Things to learn from this: The escape character is applicable anywhere in batch files. It causes every character to be recognized as not being part of the syntax.