Problem with a label in parentheses

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Problem with a label in parentheses

#1 Post by foxidrive » 09 Jan 2013 14:10

I had believed that labels posed problems in loops and that the batch execution failed.

After some testing I find that a label only fails when the label is the last line of the do loop

Code: Select all

@echo off
for /f "delims=" %%a in ("text") do (
set var=%%a
goto :label
:label
)


) was unexpected at this time.



But this works to get the first line of the file and keeping the code within the parentheses.

Code: Select all

@echo off
for /f "delims=" %%a in (file.txt) do (
set var=%%a
goto :label
:label
rem
)
echo %var%


This works too, with a double full-colon:

Code: Select all

@echo off
for /f "delims=" %%a in (file.txt) do (
set var=%%a
goto :label
:label
::
)
echo %var%



The same thing holds true with casual parentheses - this generates an error but adding a statement after the label allows it to work.

Code: Select all

(
set var=text
goto :label
:label
)


I had read someone say once that you can't have a label in a for in do loop and I didn't actually test it, but remembered what I had read.
It goes to show how dearly held beliefs are not always in fact totally true. :D

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Problem with a label in parentheses

#2 Post by abc0502 » 09 Jan 2013 14:27

i thought that too after having the same error and didn't use the comment in the for command until short time, but there is another error happen when you add two comment lines after each other

Ex 1
@echo off
for /L %%a in (1 1 5) Do (
:: test comment 1
echo ok
)
pause
This will work fine and display the ok five times, But

Code: Select all

@echo off
for /L %%a in (1 1 5) Do (
     :: test comment 1
     :: test comment 2
     echo ok
     )
pause

Adding another comment after the first will cause this error but will show the ok after each error
The system cannot find the drive specified.
ok
The system cannot find the drive specified.
ok
The system cannot find the drive specified.
ok
The system cannot find the drive specified.
ok
The system cannot find the drive specified.
ok

i have no idea why, but when using the REM command instead of :: it work fine

Edit,
just tested the code Ed Dyreen posted for multi line comment, it work under the for loop, if some one insist on using the :: to comment on multi lines but the same apply here can't use two sets of them:lol:

Code: Select all

@echo off
for /L %%a in (1 1 5) Do (
     ::%= ^
     Multiline comments are supported. ^
     Special characters work.<>;:%=* :) ^
     Be carefull with double quotes though, they need to be even ! "" ^
     Apart from a trailing caret, empty lines need at least one char to not break the multiline. ^
     =%

     echo ok
     )
pause


Important Note, when using the commant like that it seems that you can't leave empty line after it so
the last =% must be followed by the echo ok not like the example above "it won't work"
I don't know if that what Ed Dyreen meant in his comment or not
Apart from a trailing caret, empty lines need at least one char to not break the multiline.
Last edited by abc0502 on 09 Jan 2013 14:58, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Problem with a label in parentheses

#3 Post by foxidrive » 09 Jan 2013 14:43

That's interesting behaviour. It's as if it is parsing :: like a: or c: drive letter.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Problem with a label in parentheses

#4 Post by abc0502 » 09 Jan 2013 14:47

you are actually right, i didn't notice that :o
but Ed Dyreen code solved the problem of the only one line without the need of typing rem each line i don't like the way it looks :P

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

Re: Problem with a label in parentheses

#5 Post by Ed Dyreen » 09 Jan 2013 14:59

foxidrive wrote:It goes to show how dearly held beliefs are not always in fact totally true. :D
Apart from the special rules that apply inside brackets it works :D

Code: Select all

@echo off

call :function
echo.after outer function call

for /f "delims=" %%a in ("text") do (

   echo. &echo.goto inner label1
   goto :label1

   :label
   echo.this is skipped

   :label1
   echo.inside inner label1

   call :function1
   echo.after call inner function1

   echo. &echo.goto outer label2
   goto :label2

   :function1
   echo. &echo.inside inner function1
   exit /b
)

:label2
echo.inside outer label2

call :function
echo.after outer function call

call :function1
echo.after inner function call


echo.
pause
exit

Code: Select all


inside function
after outer function call

goto inner label1
inside inner label1

inside inner function1
after call inner function1

goto outer label2
inside outer label2

inside function
after outer function call

inside inner function1
after inner function call

Druk op een toets om door te gaan. . .
foxidrive wrote:That's interesting behaviour. It's as if it is parsing :: like a: or c: drive letter.
:: is a command that is executed. That's why it sometime fail, it need to be valid :twisted:

Code: Select all

::%=   This comment works though %~* :) =%
echo.test1
rem.%= This comment works though %~* :) =%
echo.test2
::     this comment crash my batch %~* :(
rem    this comment crash my batch %~* :(
%=     this comment crash my batch %~* :( =%

pause
exit

Code: Select all

test1
test2
Het volgende gebruik van de padoperator bij het vervangen
van batchparameters is ongeldig: %~*


Typ CALL /? of FOR /? voor geldige gebruiksmogelijkheden.

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

Re: Problem with a label in parentheses

#6 Post by Ed Dyreen » 09 Jan 2013 16:03

abc0502 wrote:Edit,
just tested the code Ed Dyreen posted for multi line comment, it work under the for loop, if some one insist on using the :: to comment on multi lines but the same apply here can't use two sets of them:lol:

Code: Select all

@echo off
for /L %%a in (1 1 5) Do (
     ::%= ^
     Multiline comments are supported. ^
     Special characters work.<>;:%=* :) ^
     Be carefull with double quotes though, they need to be even ! "" ^
     Apart from a trailing caret, empty lines need at least one char to not break the multiline. ^
     =%

     echo ok
     )
pause


Important Note, when using the commant like that it seems that you can't leave empty line after it so
the last =% must be followed by the echo ok not like the example above "it won't work"
I don't know if that what Ed Dyreen meant in his comment or not
Apart from a trailing caret, empty lines need at least one char to not break the multiline.

You cannot leave an empty line after a double colon inside brackets, this can be solved by replacing the double colon by REM.

Code: Select all

@echo off
(
     rem.%= ^
     Multiline comments are supported. ^
     Special characters work if they are either escaped or quoted "<>;:=* :)" ^
     Be carefull with double quotes though, they need to be even ! "" ^
     Apart from a trailing caret, empty lines need at least one char to not break the multiline. ^
     _^
     =%

     rem.%= Multiline comments are supported.=% ^
     %= Special characters work.<>;:=* :) =% ^
     %= Double quotes" do not pose a problem. =% ^
     %= Apart from a trailing caret, empty lines need at least one char to not break the multiline. =%
)
pause
exit
Inside brackets, it's more difficult to comment, the entire block needs to be valid.

ed

Tags: comment, comments, multiline, multi line, multi-line, inside brackets

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

Re: Problem with a label in parentheses

#7 Post by jeb » 10 Jan 2013 02:08

From Stackoverflow: windows batch file with goto command not working

In parenthesis lables are "two line" oriented! I experimented with labels and this are some results for parenthesis.

If a label occours, the next line has to been the correct format for a "secondary" line.
The secondary line has to be a "normal" label or a command, but not an empty line nor a double lable "::"
The secondary line is parsed a bit different, so special characters will can take effects like & and |.

Some samples

Code: Select all

(
:this label fails with a syntax error
)

(
:this works
:because this line is a "legal" secondary line
)

(
:: The remark style
:: fails, because it's not "legal" to use a double colon, because it's not a legal path
)

(
:and now it got courious & echo This will not echo'd
:but & echo You can see this !
)


There is one more strange effect with labels in parenthesis, they can consume the first token of a line.
Removing a token with an obscure trick

Code: Select all

(
:label space ^

The_first_token_wil_be_removed ECHO This works
)


jeb

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Problem with a label in parentheses

#8 Post by foxidrive » 10 Jan 2013 02:17

jeb wrote:

Code: Select all

(
:this label fails with a syntax error
)

(
:this works
:because this line is a "legal" secondary line
)

(
:: The remark style
:: fails, because it's not "legal" to use a double colon, because it's not a legal path
)





and this fails as you noted:

Code: Select all

(
rem this fails
:because
)


and this works.

Code: Select all

(
:: The remark style
rem
)

Post Reply