Using :: syntax for comment in a FOR loop can break things

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Using :: syntax for comment in a FOR loop can break things

#1 Post by Acy Forsythe » 21 Jul 2011 11:56

I thought this had been covered somewhere here but I cannot seem to find it, and I wanted to know why it doesn't work.

I don't beleive I have ever run across this before, but I am testing a script so I find myself commenting out sections and lines. I commented out the last line of a FOR loop just a few minutes ago and tested the replacement string above it... Here is a simple example of the problem:

Code: Select all

@Echo Off

FOR /F "tokens=1-2 delims= " %%a IN ("Test This") DO (

    Echo.a = %%a
    Echo.b = %%b

    Echo.This Works
REM This Works
)

FOR /F "tokens=1-2 delims= " %%a IN ("Test This2") DO (

    Echo a = %%a
    Echo b = %%b

    Echo This Works
:: This does NOT work
)


For the record I exclusively use :: for my comments as opposed to REM. With the exception of an interesting technique I picked up here to use %== Comment ==% for inline comments.

You can move that ) down a few lines and get an "incorrect Syntax" error instead of the error that this generates...

The :: comment syntax can be used anywhere else in the FOR loop, but it cannot be the last unblank line in the command block without generating an error.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Using :: syntax for comment in a FOR loop can break thin

#2 Post by dbenham » 21 Jul 2011 13:05

Here are some posts that address the issue in one way or another:
viewtopic.php?f=3&t=962&p=7350&hilit=rem#p7350
viewtopic.php?f=3&t=1003&p=3456&hilit=rem#p3456

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Using :: syntax for comment in a FOR loop can break thin

#3 Post by dbenham » 21 Jul 2011 13:06


Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: Using :: syntax for comment in a FOR loop can break thin

#4 Post by Acy Forsythe » 21 Jul 2011 14:28

Thanks Dave (And Jeb) :) that second one was the one I was after.

I apologize for not searching, but I have learned that searching for strings under 4 characters usually gives an error on forums so I just don't do it.

I usually run across threads like that because I am searching for something else and they are one-off hits. I always browse the unrelevant search results just to see what I am missing :) And since I don't remember what I was searching for when I found that and I didn't search for REM because it's 3 characters, I had to post :oops:

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Using :: syntax for comment in a FOR loop can break thin

#5 Post by Ed Dyreen » 21 Jul 2011 14:32

'
Hi Acy, why does this work ?

Code: Select all

@Echo Off

FOR /F "tokens=1-2 delims= " %%a IN ("Test This") DO (

    Echo.a = %%a
    Echo.b = %%b

    Echo.This Works
REM This Works
)

FOR /F "tokens=1-2 delims= " %%a IN ("Test This2") DO (

    Echo a = %%a
    Echo b = %%b

    Echo This Works

    :: This Works
    echo.>nul

    ::^( This Works
    echo.>nul
    ::^) This Works
    echo.>nul
)

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: Using :: syntax for comment in a FOR loop can break thin

#6 Post by Acy Forsythe » 21 Jul 2011 15:12

Well the link Jeb posted in the first link Dave posted does not work. So all I had to go by were the 2nd and 3rd links that Dave posted.

The 2nd link I messed around with Jebs remarks.bat and it does not run on my Win7 machine, it generates a syntax error after the first example. :cry:

So in answer to your question, No. I don't know why it works, but I do know that the echo.>nul is what is making it work.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Using :: syntax for comment in a FOR loop can break thin

#7 Post by Ed Dyreen » 21 Jul 2011 15:25

'
That's ok, I don't know why either, as long we know it works.
One of the rules of :: inside brackets is that it always has to be followed by a valid command.

That link to http://www.lingubender.com/forum/viewto ... 8698#p5290 is down and I hope temporarily.
I've seen it already, jeb posted an example there about the peculiarities of rem and ::
It will blow your mind when you get to see it, and you should see it, it's worth it.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Using :: syntax for comment in a FOR loop can break thin

#8 Post by dbenham » 21 Jul 2011 15:38

Yes - I hope jeb's link gets ressurected. It was very informative.

The following is from my memory of jeb's post, coupled with some testing of my own based on that memory.

In order for a ::comment to work within a () block, the next line must be a valid non blank, non ::comment line.

These all work:

Code: Select all

(
  ::This works because there is a valid non :: / non blank line below it
  echo Line1

  :: This works because Line continuation ^
  :: causes this line to be part of the above line and next line is valid
  echo Line2

  ::It doesn't matter what the next line is as long as it is a valid line
  rem
)


And all of these cases fail and/or give error messages:

Code: Select all

(
  echo this won't print because of fatal failed ::coment below
  ::This fails because no line after
)

Code: Select all

(
  :: This generates an error because next line starts with ::
  ::
  echo This prints
)

Code: Select all

(
  :: This fails because next line is blank

  echo This won't print because of fatal failed ::comment above
)

Code: Select all

(
  ::This fails because line continuation makes next line part of this one^
  echo and there is not a valid line afterward
)


Dave Benham

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: Using :: syntax for comment in a FOR loop can break thin

#9 Post by Acy Forsythe » 21 Jul 2011 15:51

One of the rules of :: inside brackets is that it always has to be followed by a valid command.


Nevermind, I know why it works... a label is not valid as the last command in a block which is specified by ( and ). So as long as you have something even if it is echo.>null it's still a valid command.

In other examples, you're commenting out the () by using ::( and ::) so they are not "Really" command blocks so any commenting inside them is just fine, even this:

Code: Select all

::(
::This is a comment
::)


In youre example above, you're creating a new line with the ^ so the comment-block rules apply again. I find that odd though, that the :: doesn't stop the ^ from being parsed, but I guess even labels can use the multiline char.

EDIT:

I was writing my reply while Dave was posting. And I got jeb's example to work... Unfortinately I fell once again into the situation Dave found where setlocal exists beyond the grave.

Alright, so I know why :: fails in the middle somewhere in brackets. It's because the second character is being picked up as colon by the parser and mistakes :: for the drive ::\ but why only when in brackets? That makes no sense for the parser to decide that : is a drive letter only when it's inclosed in brackets...

I can't think of any examples where someone would type (C:) like that in a batch file :?

EDIT2:

So I guess the rules for :: are:

1. Can't be the first line in a block
2. Can't be the last line in a block
3. Can't be on a line by itself within a block

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Using :: syntax for comment in a FOR loop can break thin

#10 Post by jeb » 22 Jul 2011 00:08

Hi Acy,

Acy Forsythe wrote:So I guess the rules for :: are:

1. Can't be the first line in a block
2. Can't be the last line in a block
3. Can't be on a line by itself within a block

Nice guess, but wrong :wink:

Each label requires a secondary line, the secondary line can be
a normal command or also a label, illegal are empty lines or lines beginning with ::
The first part of a secondary label have to be a legal filname/command,
like

Code: Select all

(
:FirstLine - This works
:C:
)
(
:FirstLine - This fails
::
)
(
:FirstLine - This fails
:<
)


And an ampersand works in a the secondary label line!

That's the cause why it seems, that a label is not allowed at the end
of a block.

Code: Select all

(
echo hello
:this Fails
)


But only the secondary line is the problem, in this case the ")"

Code: Select all

(
echo hello
:first label
:secondary label
)


Code: Select all

(
:label1 & echo this doesn't work
:label2 & echo But this
)


jeb

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Using :: syntax for comment in a FOR loop can break thin

#11 Post by dbenham » 22 Jul 2011 02:01

Thanks jeb

dbenham wrote:In order for a ::comment to work within a () block, the next line must be a valid non blank, non ::comment line.
:D My memory served me well.
But I forgot that ::comment can be followed by a valid label

Code: Select all

(
  ::This works because following line is a valid label (thanks jeb)
  :Valid label that probably never gets called
  echo There is no problem with this block
)


jeb wrote:That's the cause why it seems, that a label is not allowed at the end
of a block.
Not quite true jeb :!: :wink:

This works:

Code: Select all

(
  echo There is no problem with this block
  ::This works because following line is a valid label
  :label is valid at end of block only if previous line begins with ::
)


Dave Benham

Post Reply