Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

WAD Not quite a bug, but Is there a reason we can't use labels in parenthesis?

Jul
254
6
I think this may be why I refused to use parenthesis in if/fore statements from 1988 until 2023...... Suddenly not being able to do things I normally do.

But i see now that technically, the documentation says this is invalid.

But i feel like this kinda defeats the purpose of using parethesis for large blocks of code?

I get why this would make sense for a (command group of one line)

but i don't get why this would make sense for a (
command
group
of
several
lines
)

Correctly outputs 'foo bar baz':
Code:
@Echo off
for %value in (foo bar baz) do (
    REM :label
    echo %value
)

Outputs nothing but having unreachable lines of BAT doesn't feel right:

Code:
@Echo off
for %value in (foo bar baz) do (
    :label
    echo %value
)


The only difference is me remarking the label out. That makes the output happen.

But use a label? Everything past it is unreachable code.

Basically means we can't use labels inside a for loop.

Am I crazy in that i think this should be allowed?



1686438239934.png
 
WAD.

A FOR command is *NOT* a multiline command; it's a single-line command. The parser assembles it into a single line before passing it to the FOR internal command for execution. A label embedded in a single line makes no sense.

FOR is restricted in what it can do in order to maintain compatibility with CMD. Your batch file won't work in CMD either, though CMD throws an error instead of ignoring the invalid syntax.

So yes, you are crazy to think this should be allowed. Or at least Microsoft thinks you are.

All is not lost though - dump FOR and use DO instead. It's vastly more flexible and powerful than FOR, and you can put labels inside your DO loops (except for the special case single-line DO's).
 
I can deal with Microsoft thinking I'm crazy ;) :D

It's also avoidable if you factor out the multi-line code into a gosub, so in a way, this microsoft-compatibility issue foces me to factor my code a bit better.

Thank you for your excellent insight!
 

Similar threads

Back
Top