1.

Solve : Breaking FOR Loop?

Answer» HI Again Everyone,

In batch code, is it possible to force a break out of runnng FOR loop ?

I've not been able to figure this out (If it is at all possible)Code: [Select]
@echo off
set MonitorLoops=9
set MonitorInterval=2

for /l %%i in (1,1,%MonitorLoops%) do (
echo Loop %%i of %MonitorLoops% .
ping -n %MonitorInterval% 127.0.0.1 > nul

if %%i EQU 4 exit
)

Any ideas ??

Cheers,
Cameron
Solved ... just needed to use the GOTO statement.
Code: [Select]
@echo off
set MonitorLoops=9
set MonitorInterval=2

for /l %%i in (1,1,%MonitorLoops%) do (
@echo Loop %%i of %MonitorLoops% .
ping -n %MonitorInterval% 127.0.0.1 > nul

if %%i EQU 4 goto :Break1
)
:Break1
@echo End-of-Script.
Why don't you just set MonitorLoops=4 in the first place? Or was that just to demomstrate the question?

Hi Dias,

The eventual code won't be checking MonitorLoop.
It'll be actually checking for the existance of a 'trigger' file.
If it exists, then I need to exit the loop.

The example code (excluding the "if %%i EQU 4 goto :Break1" ) would run for 18 seconds.

Nine(9) loops with two(2) second intervals between loops. This will eventually be set to have an interval of 300 seconds when it goes into production.

Unfortunately I rearely have a chance to create Batch scripts, hence I'm quite rusty. Very grateful for fellas like Sidewinder for their assistance.
I HAPPEN to do more of my scripting on HP-UX systems.

But thanks for the question anyways.

Cheers,
Cameron
That code will ***never*** get to 5
Hi Dias,

Correct - I was deliberately generating a TRUE condition for testing.

Cheers,
Cameron


Discussion

No Comment Found