+ Post New Thread
Results 1 to 6 of 6
Scripts Thread, Loop a batch file? in Coding and Web Development; In a batch file is it possible to set it to loop say 3 times only? Thanks...
  1. #1

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,216
    Thank Post
    893
    Thanked 1,773 Times in 1,529 Posts
    Blog Entries
    12
    Rep Power
    461

    Loop a batch file?

    In a batch file is it possible to set it to loop say 3 times only?

    Thanks

  2. #2

    mattx's Avatar
    Join Date
    Jan 2007
    Posts
    9,240
    Thank Post
    1,058
    Thanked 1,069 Times in 625 Posts
    Rep Power
    740
    Quote Originally Posted by FN-GM View Post
    In a batch file is it possible to set it to loop say 3 times only?

    Thanks
    Yes it is.

  3. #3

    mattx's Avatar
    Join Date
    Jan 2007
    Posts
    9,240
    Thank Post
    1,058
    Thanked 1,069 Times in 625 Posts
    Rep Power
    740
    Happy batch writing.

    Batch files - FOR loops

  4. #4

    Join Date
    Apr 2010
    Posts
    8
    Thank Post
    4
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Try this:

    Code:
    @echo off
    set max=.ooo
    set count=.
    :LOOP
    echo hi there
    set count=%count%o
    if not %count%==%max% goto LOOP
    set max=
    set count=
    echo complete
    set max=.ooo <-- 3 o's is your repeat.

  5. #5

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,730
    Thank Post
    344
    Thanked 520 Times in 488 Posts
    Rep Power
    180
    Other option "think it should work" is simply making a list and going through it like:

    for %a in (1,2,3) do echo "test"

    Think that should work, but can't run BATs locally

    Steve

  6. #6

    jinnantonnixx's Avatar
    Join Date
    Mar 2011
    Location
    In the Calamatorium.
    Posts
    2,017
    Thank Post
    118
    Thanked 512 Times in 346 Posts
    Blog Entries
    2
    Rep Power
    288
    The /L switch in 'FOR' is your best bet.

    FOR /L %variable IN (start,step,end) DO command [command-parameters]

    The set is a sequence of numbers from start to end, by step amount.
    So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would
    generate the sequence (5 4 3 2 1)
    example
    Code:
    @echo off
    for /L %%n in (1,1,3) do (
    	echo I'm in a loop!
    	echo This is loop number %%n
    	echo Oh yes.
    	if %%n EQU 3 (echo THIS IS THE LAST TIME I SHOULD RUN!!!)
    )
    will give you

    I'm in a loop!
    This is loop number 1
    Oh yes.
    I'm in a loop!
    This is loop number 2
    Oh yes.
    I'm in a loop!
    This is loop number 3
    Oh yes.
    THIS IS THE LAST TIME I SHOULD RUN!!!
    Last edited by jinnantonnixx; 4th August 2011 at 11:51 AM.

SHARE:
+ Post New Thread

Similar Threads

  1. For Next Loop in Batch File
    By barrystone in forum Scripts
    Replies: 2
    Last Post: 17th March 2011, 01:10 PM
  2. executing a random command in a batch file
    By Halfmad in forum Scripts
    Replies: 12
    Last Post: 6th November 2007, 09:25 PM
  3. batch file to shutdown PCs
    By macca in forum Windows
    Replies: 5
    Last Post: 3rd May 2007, 09:20 AM
  4. prezzie batch file to collect dps logs
    By russdev in forum ICT KS3 SATS Tests
    Replies: 4
    Last Post: 16th May 2006, 12:16 PM
  5. batch file help
    By beeswax in forum Scripts
    Replies: 9
    Last Post: 3rd March 2006, 07:48 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •