Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Breaking FOR Loop  (Read 2233 times)

0 Members and 1 Guest are viewing this topic.

CameronY

    Topic Starter


    Intermediate

    Breaking FOR Loop
    « on: May 29, 2008, 05:48:47 PM »
    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

    CameronY

      Topic Starter


      Intermediate

      Re: Breaking FOR Loop
      « Reply #1 on: May 29, 2008, 06:15:53 PM »

      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.
       

      Dias de verano

      • Guest
      Re: Breaking FOR Loop
      « Reply #2 on: May 30, 2008, 12:43:33 AM »
      Why don't you just set MonitorLoops=4 in the first place? Or was that just to demomstrate the question?


      CameronY

        Topic Starter


        Intermediate

        Re: Breaking FOR Loop
        « Reply #3 on: May 30, 2008, 12:53:54 AM »
        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
         

        Dias de verano

        • Guest
        Re: Breaking FOR Loop
        « Reply #4 on: May 30, 2008, 03:02:21 AM »
        That code will ***never*** get to 5

        CameronY

          Topic Starter


          Intermediate

          Re: Breaking FOR Loop
          « Reply #5 on: May 30, 2008, 03:30:56 AM »
          Hi Dias,

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

          Cheers,
          Cameron