Break Loop infinite

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ngusaomanoi
Posts: 8
Joined: 13 Nov 2012 20:27

Break Loop infinite

#1 Post by ngusaomanoi » 12 Mar 2013 02:20

Hi All,
I have a Loop Infinite.

for /L %%n in (1,0,10) do (
echo "infinite\n"
if exist "%~dp0\Parts\file.xml"
(
//I want break here
)
)
anybody give me syntax break in batch file.

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

Re: Break Loop infinite

#2 Post by foxidrive » 12 Mar 2013 03:53

What do you mean break? Control C?

Explain what you are trying to do...

ngusaomanoi
Posts: 8
Joined: 13 Nov 2012 20:27

Re: Break Loop infinite

#3 Post by ngusaomanoi » 12 Mar 2013 04:28

I use batch file no use console.
Thanks.

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

Re: Break Loop infinite

#4 Post by foxidrive » 12 Mar 2013 04:31

foxidrive wrote:Explain what you are trying to do...


See above.

For example you can put in a statement and check for the existence of another file, and branch out of the loop.

It depends on what you want to do.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Break Loop infinite

#5 Post by Squashman » 12 Mar 2013 05:49

We had a thread a few months back about breaking out of FOR /L loops. Gotta dig that up.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Break Loop infinite

#6 Post by Aacini » 12 Mar 2013 09:13

This question was extensively discussed at infinite loop with break condition topic, and the last solution is here

Antonio

tomato
Posts: 2
Joined: 14 Mar 2013 19:33

Re: Break Loop infinite

#7 Post by tomato » 14 Mar 2013 19:46

To break in for-loop, use "goto ~ label" statement as followings.

for /L %%n in (1,0,10) do (
echo "infinite\n"
if exist "%~dp0\Parts\file.xml" (
goto :break
)
)
:: label
:break

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Break Loop infinite

#8 Post by Aacini » 14 Mar 2013 22:11

@tomato: Please, test the previous program by yourself: it never ends and requires a Ctrl-C cancellation!

(perhaps you should read the referred topic so you know what we are talking about)

Antonio

tomato
Posts: 2
Joined: 14 Mar 2013 19:33

Re: Break Loop infinite

#9 Post by tomato » 15 Mar 2013 01:08

@Antonio
sorry for my mistake. I did not watch the code carefully.

@ngusaomanoi
In your for-loop,

(1, 0, 10) -> (start, step, end)

because the step is 0, the start(=1) cannot reach the end(=10).
Therefore it causes an infinite loop.

Replace step to any other value greater than 0.
eg. (1, 1, 10)

:)

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Break Loop infinite

#10 Post by Liviu » 17 Mar 2013 11:44

tomato wrote:(1, 0, 10) -> (start, step, end)

because the step is 0, the start(=1) cannot reach the end(=10).
Therefore it causes an infinite loop.

Right. That's probably why the title of this thread is "Break Loop infinite" ;-)

FWIW "goto :break" doesn't really work for finite loops, either, even though it may appear to. What happens is that the body of the for loop stops being executed after the goto, but the loop itself unrolls all the way to the end before jumping to the label. For a simple example, save the following as for123.cmd

Code: Select all

@for /l %%i in (1,1,3) do (
  echo %%i
  goto :break
)

:break
@echo done
then run it at a command prompt.

Code: Select all

C:\tmp>for123

C:\tmp>(
echo 1
 goto :break
)
1

C:\tmp>(
echo 2
 goto :break
)

C:\tmp>(
echo 3
 goto :break
)
done

C:\tmp>
Note that the body of the loop is still parsed 3 times, regardless of the goto :break. The actual output in this case is "1" then "done" as probably expected, but if you replace (1,1,3) with (0,0,0) or simply () then the loop becomes infinite, and the "done" line is never reached - which was the topic in point here, and discussed in detail at the links Aacini provided.

Liviu

Post Reply