#1 24 Apr 2012 14:42

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 323

Can I use labels inside IF?

Because it seems I cant:

if exist c:\ (
	goto :myLabel
	:mylabel
)

prints:

) was unexpected at this time.

I can workaround this with subroutine but it's strange.
I have no problems with going to labels outside the IF.


http://www.facebook.com/npocmaka

Offline

#2 24 Apr 2012 14:57

jeb
Member
From: Germany
Registered: 19 Nov 2010
Posts: 99

Re: Can I use labels inside IF?

There are two different problems here.

1) A GOTO inside of brackets breaks the bracket context (and stops a FOR-Loop)

if 1==1 (
  goto :next
  echo never reached
:next
  echo After the label
) ELSE (
  echo Why I can see this?
)

Outputs

After the label
Why I can see this?

As you can see the ELSE isn't effective anymore after invoking the GOTO

2) Labels are a bit strange inside of brackets
Labels in brackets use always two lines
- The primary line is the label, here is also a :: Label allowed
- The secondary line (after a label) must be a VALID statement or and label (but not a :: comment), an empty line or a closing bracket isn't valid here

In the secondary line are also the special characters active like "&|>"

(
:NormalLabel & echo this will not be executed
:SecondaryLine & echo This will be executed
)

As you can see, it's all obvious smile

jeb

Offline

#3 24 Apr 2012 15:23

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 323

Re: Can I use labels inside IF?

IT'S ALIVE!

..I mean it works with two labels.Thanks a lot :-) .

And it's not so obvious.I still do not understand why with two labels the bracket context works.But anyway it works .


http://www.facebook.com/npocmaka

Offline

#4 04 Nov 2012 23:08

carlos
Member
From: Chile
Registered: 04 Nov 2008
Posts: 231
Website

Re: Can I use labels inside IF?

Mix the character & with labels are confusing:

(
:Label1 & echo line1
:Label2 & echo line2
)

prints:

line2

But:

(
:Label1
echo line1
:Label2
echo line2
)

prints:

line1
line2

Offline

#5 05 Nov 2012 18:53

dbenham
Member
From: U.S. east coast
Registered: 15 Apr 2012
Posts: 100

Re: Can I use labels inside IF?

@Carlos - Oooh, that is pure evil. I like it! :twisted:

Offline

#6 06 Nov 2012 09:35

jeb
Member
From: Germany
Registered: 19 Nov 2010
Posts: 99

Re: Can I use labels inside IF?

Long time ago there was a discussion about labels inside barckets.
Nested for loop recursing directories
and here
SO: windows batch file with goto command not working

And there exists a more evil construct.

for %%a in (1 2 3) DO (
:label1 ^
::label2
:label3
::Label4 ^

NeverEver^
(^ AlwaysWorks echo %%a)
)

It will show the numbers 1 to 3 (obviously) smile

Ok the code it's a bit obfuscated, but a little bit fun must be. smile


Btw. I'm happy that I recover my lost sample, as it's the only known way (for me),
to remove a single token form the beginning of a line.

jeb

Last edited by jeb (06 Nov 2012 09:38)

Offline

Board footer

Powered by FluxBB