Computing.Net > Forums > Programming > attn: IVO * goto :label in FOR

attn: IVO * goto :label in FOR

Reply to Message Icon

Original Message
Name: Mechanix2Go
Date: May 19, 2008 at 11:33:16 Pacific
Subject: attn: IVO * goto :label in FOR
OS: 2K SP3
CPU/Ram: PIII/512
Model/Manufacturer: clone
Comment:

Hi gang,

Why doesn't this keep going?

::==================================

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/s/b/a-d') do (
echo %%a | find /i "bat" > nul
if errorlevel 1 goto :skip
echo found bat in %%a
:skip
echo.
)


=====================================
If at first you don't succeed, you're about average.

M2


Report Offensive Message For Removal


Response Number 1
Name: IVO
Date: May 19, 2008 at 14:37:42 Pacific
Reply: (edit)

I don't know the theory behind that, but I experienced you can't code GOTO inside a block/compound statement , i.e. one embraced by (...), as the nominal flow of execution is broken and unexpected results stem out.

You have to follow strictly the constructs of structured programming replacing GOTOs with Call If (Then) Else and so on...

Hard but challenging.

Ciao
Ivo


Report Offensive Follow Up For Removal

Response Number 2
Name: Mechanix2Go
Date: May 19, 2008 at 17:52:24 Pacific
Reply: (edit)

ciao IVO

got it


=====================================
If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 3
Name: klint
Date: May 20, 2008 at 02:04:44 Pacific
Reply: (edit)

I'm not sure but the Command Processor seems to treat command blocks (inside brackets) as if they were on a single line. I.e. this:

if ... (
command1
command2
command3
)

behaves as if it was like this:

if ... (command1 & command2 & command3)

which also explains why you can't use %var% and have to use !var! instead.

Since labels have to be at the start of a line, it doesn't make sense to say

(command1 & :label1 & command2)


Report Offensive Follow Up For Removal

Response Number 4
Name: Mechanix2Go
Date: May 20, 2008 at 04:13:23 Pacific
Reply: (edit)

klint says:

Since labels have to be at the start of a line, it doesn't make sense to say

(command1 & :label1 & command2)

========================================
Maybe not, but how about:

(command1 & goto :label1 )


=====================================
If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 5
Name: klint
Date: May 20, 2008 at 04:51:13 Pacific
Reply: (edit)

M2:

> Maybe not, but how about:
>
> (command1 & goto :label1 )

Yes, that's perfectly fine, as long as label1 is defined outside the brackets.


Report Offensive Follow Up For Removal


Response Number 6
Name: Mechanix2Go
Date: May 21, 2008 at 01:31:12 Pacific
Reply: (edit)

There's plenty of odd stuff in the command interpreter. Same gos for command.com


=====================================
If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 7
Name: tuk
Date: May 27, 2008 at 12:27:44 Pacific
Reply: (edit)

I'v had this same problem until IVO helped me out with the 'choice' command:

I'v noticed when using @pause in () to track down the source of errors, that the error is flagged before execution reaches the pause..the only thing I can think is that all the code inside the brackets is checked/compiled before the first com is executed

(
-execution stops here and flags error in com4
com1 @pause
com2
com3
com4 error
)

Warning Batch Newbie:
Thanks For Being Patient!
Any Help Is Appreciated!


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: attn: IVO * goto :label in FOR

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge