2

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!

Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125
  • 1
    That would be Phase 2. [Read here](https://stackoverflow.com/a/4095133/2152082). Get a cup of coffee, it's really hard stuff. – Stephan Jul 27 '19 at 19:26
  • 1
    @Stephan I'm all out of coffee, I'm scared ;). Thanks for the link! – Ghoul Fool Jul 27 '19 at 19:41
  • 1
    Some explanations, see [Unexpected “The system cannot find the drive specified.” in batch file](https://stackoverflow.com/a/19847135/463115) – jeb Jul 28 '19 at 13:48

1 Answers1

3

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

Magoo
  • 77,302
  • 8
  • 62
  • 84