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

Author Topic: help with looping  (Read 6728 times)

0 Members and 1 Guest are viewing this topic.

valahrim

    Topic Starter


    Rookie

    help with looping
    « on: July 07, 2008, 01:14:37 AM »
    i have a problem with batch file loop commands in MS dos  and need a little help.

    below is a similar type of batch file i am currently using.

    Code: [Select]
    @echo off
    :a
    start
    sleep 2
    echo1
    echo2
    echo3
    goto :a
    echo end
     

    i would like to terminate the loop created have the batch file continue so i can use multiple loop commands. is there a way i can do this.

    thank you
    « Last Edit: July 07, 2008, 01:31:11 AM by valahrim »

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: help with looping
    « Reply #1 on: July 07, 2008, 04:59:21 AM »
    What condition has to be met to terminate the loop? Once you determine the condition, you can use an if statement in combination with a goto statement to branch out of the loop.

     8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    valahrim

      Topic Starter


      Rookie

      Re: help with looping
      « Reply #2 on: July 07, 2008, 07:53:52 AM »
      the condition im after is to run thru the loop, say for ecample 5 times before it stops the loop and continues to run

      Dias de verano

      • Guest
      Re: help with looping
      « Reply #3 on: July 07, 2008, 08:01:11 AM »
      @echo off
      set /a limit=5
      set /a loops=0
      :a
      start
      sleep 2
      echo1
      echo2
      echo3
      set /a loops=%loops%+1
      if %loops% EQU %limit% goto end
      goto :a
      :end


      valahrim

        Topic Starter


        Rookie

        Re: help with looping
        « Reply #4 on: July 07, 2008, 08:38:53 AM »
        ok thx for that ill try it out, would u be able to explain the parts of the code?

        Carbon Dudeoxide

        • Global Moderator

        • Mastermind
        • Thanked: 169
          • Yes
          • Yes
          • Yes
        • Certifications: List
        • Experience: Guru
        • OS: Mac OS
        Re: help with looping
        « Reply #5 on: July 07, 2008, 09:08:47 AM »
        set /a limit=5
        set /a loops=0

        Sets %limit% = 5 and %loops% = 0

        set /a loops=%loops%+1
        Adds 1 to %loops%. If this is the first loop, it would be 0+1.

        if %loops% EQU %limit% goto end

        If the number %loops% = 5 goto end.

        This means in every loop, %loops% will add 1 to itself and when %loops% equals 5, it will terminate. If not, it goes to :a

        Carbon Dudeoxide

        • Global Moderator

        • Mastermind
        • Thanked: 169
          • Yes
          • Yes
          • Yes
        • Certifications: List
        • Experience: Guru
        • OS: Mac OS
        Re: help with looping
        « Reply #6 on: July 07, 2008, 09:09:45 AM »
        I just notice you don't need to set limit=5

        Code: [Select]
        @echo off
        set /a loops=0
        :a
        start
        sleep 2
        echo1
        echo2
        echo3
        set /a loops=%loops%+1
        if %loops% EQU 5 goto end
        goto :a
        :end

        That should work.

        valahrim

          Topic Starter


          Rookie

          Re: help with looping
          « Reply #7 on: July 07, 2008, 09:13:31 AM »
          ok tyvm it all works fine. i no its not what i originally asked for but how does the call command work?

          Carbon Dudeoxide

          • Global Moderator

          • Mastermind
          • Thanked: 169
            • Yes
            • Yes
            • Yes
          • Certifications: List
          • Experience: Guru
          • OS: Mac OS
          Re: help with looping
          « Reply #8 on: July 07, 2008, 09:23:21 AM »
          It calls another Batch File. For example:

          You could do this with the call command

          start.bat
          Code: [Select]
          @echo off
          call settings.bat
          echo %a% %b% %c%
          pause
          exit

          settings.bat

          Code: [Select]
          set a=one
          set b=two
          set c=three

          If the two files aren't in the same directory, you will need to use call C:\path\to\file.bat

          valahrim

            Topic Starter


            Rookie

            Re: help with looping
            « Reply #9 on: July 07, 2008, 09:27:46 AM »
            what is the echo %a% %b% %c% ued for? i see the relevance to the settings.bat but i dont understand why u need it and how you would run a command from it

            Carbon Dudeoxide

            • Global Moderator

            • Mastermind
            • Thanked: 169
              • Yes
              • Yes
              • Yes
            • Certifications: List
            • Experience: Guru
            • OS: Mac OS
            Re: help with looping
            « Reply #10 on: July 07, 2008, 09:33:03 AM »
            That was an example.   :P :P

            http://www.computerhope.com/call.htm

            valahrim

              Topic Starter


              Rookie

              Re: help with looping
              « Reply #11 on: July 07, 2008, 09:36:57 AM »
              woo it works ^^ awesum tyvm :D

              Carbon Dudeoxide

              • Global Moderator

              • Mastermind
              • Thanked: 169
                • Yes
                • Yes
                • Yes
              • Certifications: List
              • Experience: Guru
              • OS: Mac OS
              Re: help with looping
              « Reply #12 on: July 07, 2008, 09:53:18 AM »
               ;)

              Dias de verano

              • Guest
              Re: help with looping
              « Reply #13 on: July 07, 2008, 10:01:53 AM »
              I just notice you don't need to set limit=5

              Code: [Select]
              if %loops% EQU 5 goto end

              Thanks for worsening my code!  :(

              Yes, I knew when I posted that you could hard-code the loop limit into the IF test, but that is what I was taught was sloppy programming practice, that is, code with "magic numbers" is deplored. Better to put it in a variable.
              « Last Edit: July 07, 2008, 10:30:55 AM by Dias de verano »

              valahrim

                Topic Starter


                Rookie

                Re: help with looping
                « Reply #14 on: July 07, 2008, 10:04:14 AM »
                well i kept the  set limit so its easier for me to find my way around the code