Ghoul Fool
Ghoul Fool

Reputation: 6967

What is a broken label?

Further to my question here, I need to ask why is it bad (as in results and practice) to have :: comment instead of REM within code blocks:

for %%a in (*.pdf) do (
:: This bad, but why?
)

Can someone explain this to me? And thereby restore my faith in batch files!

Upvotes: 2

Views: 199

Answers (1)

Magoo
Magoo

Reputation: 80211

The reason that it's a broken label can be demonstrated by this code:

@ECHO On
SETLOCAL
GOTO fred
ECHO miss this 1
:fred
GOTO :bill
ECHO miss this 2
:bill
GOTO ::charlie
echo miss this 3
::charlie
ECHO all done!
GOTO :EOF

That is, a leading : indicates a label, but the label-name itself may not start :. Now whether :label:with:colons is a valid label, I'll leave to the enthusiastic experimenter. I'd prefer to keep to a simple rule - no colons in label-names.

As for the no-labels-in-code-blocks rule, remember cmd's history. At one time, code-blocks were not allowed. It's a simple-minded processor which may have learned a lot since its inception. It wants to maintain backward-compatibility, so suppose we come across some common constructions within a block:

goto label, goto :label, call label, call :label

this leaves us with a nightmare of possible return-paths and interpretation possibilities. Does goto outofblock terminate the block? Does goto inblock terminate the iteration?

Hence, the simple-minded rule, no labels in blocks. I remember jeb worked out a set of rules where labels can be used, which was interesting. Here's a video

Upvotes: 3

Related Questions