View unanswered posts | View active topics It is currently 26 May 2013 00:48



Post new topic Reply to topic  [ 8 posts ] 
Problem with a label in parentheses 
Author Message

Joined: 10 Feb 2012 02:20
Posts: 2485
Post Problem with a label in parentheses
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:
@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:
@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:
@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:
(
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


09 Jan 2013 14:10
Profile

Joined: 26 Oct 2011 22:38
Posts: 1006
Location: Egypt
Post Re: Problem with a label in parentheses
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
Quote:
@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:
@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
Quote:
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:
@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
Quote:
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.



09 Jan 2013 14:27
Profile

Joined: 10 Feb 2012 02:20
Posts: 2485
Post Re: Problem with a label in parentheses
That's interesting behaviour. It's as if it is parsing :: like a: or c: drive letter.


09 Jan 2013 14:43
Profile

Joined: 26 Oct 2011 22:38
Posts: 1006
Location: Egypt
Post Re: Problem with a label in parentheses
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


09 Jan 2013 14:47
Profile
Expert

Joined: 16 May 2011 08:21
Posts: 1162
Location: Flanders_(Belgium)
Post Re: Problem with a label in parentheses
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:
@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:

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:
::%=   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:
test1
test2
Het volgende gebruik van de padoperator bij het vervangen
van batchparameters is ongeldig: %~*


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


09 Jan 2013 14:59
Profile WWW
Expert

Joined: 16 May 2011 08:21
Posts: 1162
Location: Flanders_(Belgium)
Post Re: Problem with a label in parentheses
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:
@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
Quote:
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:
@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


09 Jan 2013 16:03
Profile WWW
Expert

Joined: 30 Aug 2007 08:05
Posts: 567
Location: Germany
Post Re: Problem with a label in parentheses
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:
(
: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:
(
:label space ^

The_first_token_wil_be_removed ECHO This works
)


jeb


10 Jan 2013 02:08
Profile

Joined: 10 Feb 2012 02:20
Posts: 2485
Post Re: Problem with a label in parentheses
jeb wrote:
Code:
(
: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:
(
rem this fails
:because
)


and this works.

Code:
(
:: The remark style
rem
)


10 Jan 2013 02:17
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.