[Toc][Index]

DO - Create loops in batch files

 
 Purpose:    Create loops in batch files. 
             
 Format:     DO [n | FOREVER] 
                 or 
             DO varname = start TO end [BY n ] 
                 or 
             DO [WHILE | UNTIL] condition 
             DO varname IN [@]set 
                 commands 
             [ITERATE] 
             [LEAVE] 
                 commands 
             ENDDO 
             
             varname :  The environment variable that will hold the loop 
             counter, filename, or line from a file. 
             n, start, end :  Integers between 0 and 2,147,483,647 
             inclusive, or an internal variables or variable functions 
             that evaluate to such a value. 
             condition :  A test to determine if the loop should be 
             executed. 
             set :  A set of values for the variable. 
             commands :  One or more commands to execute each time through 
             the loop.  If you use multiple commands, they must be 
             separated by command separators or be placed on separate 
             lines. 
 
 File Selection 
 Supports extended wildcards, ranges, and include lists for the set. 
 Usage 
 DO can only be used in batch files.  It cannot be used in aliases. 
 DO can be used to create 4 different kinds of loops.  The first, 
 introduced by DO n, is a counted loop.  The batch file lines between DO 
 and ENDDO are repeated n times.  For example: 

 
         do 5
            beep
         enddo
 
 
 You can also specify "forever" for n if you wish to create an endless 
 loop (you can use LEAVE or GOTO to exit such a loop; see below for 
 details). 
 The second type of loop is similar to a "for loop" in programming 
 languages like BASIC.  DO creates an environment variable, varname, and 
 sets it equal to the value start (if varname already exists in the 
 environment, it will be overwritten).  DO then begins the loop process by 
 comparing the value of varname with the value of end. If varname is less 
 than or equal to end, DO executes the batch file lines up to the ENDDO. 
  Next, DO adds 1 to the value of varname, or adds the value n if BY n is 
 specified, and repeats the compare and execute process until varname is 
 greater than end.  This example displays the even numbers from 2 through 
 20: 

 
         do i = 2 to 20 by 2
            echo %i
         enddo
 
 
 DO can also count down, rather than up.  If n is negative, varname will 
 decrease by n with each loop, and the loop will stop when varname is less 
 than end.  For example, to display the even numbers from 2 through 20 in 
 reverse order, replace the first line of the example above with: 

 
         do i = 20 to 2 by -2
 
 
 The third type of loop is called a "while loop" or "until loop." DO 
 evaluates the condition, which can be any of the tests supported by the 
 IF command, and executes the lines between DO and ENDDO as long as the 
 condition is true.  The loop ends when the condition becomes false. 
 WHILE tests the condition at the start of the loop.  Therefore, if the 
 condition is false when the loop starts, the statements within the loop 
 will never be executed, and the batch file will continue with the 
 statement after the ENDDO. 
 UNTIL tests the condition at the end of the loop.  Therefore, if the 
 condition is false when the loop starts, the statements within the loop 
 will still be executed at least once. 
 The fourth type of loop executes the lines between DO and ENDDO once for 
 every member of a set (this is similar to the set used in the FOR 
 command).  Normally, the set is a list of files specified with wildcards. 
  For example: 

 
         do x in *.txt
 
 
 will execute the loop once for every .TXT file in the current directory; 
 each time through the loop the variable x will be set to the name of the 
 next file that matches the file specification. 
 If, between DO and ENDDO, you create a new file that could be included in 
 the list of files, it may or may not appear in an iteration of the DO 
 loop.  Whether the new file appears depends on its physical location in 
 the directory structure, a condition over which CMD.EXE has no control. 
 You can also execute the loop once for each line of text in a file by 
 placing an [@] in front of the file name.  If you have a file called 
 DRIVES.TXT that contains a list of drives on your computer, one drive 
 name per line, you can execute the loop once for each drive this way: 

 
         do x in @drives.txt
 
 
 To execute the loop once for each line of text in the clipboard, use 
 CLIP: as the file name (e.g. DO X IN @CLIP:).  CLIP: will not return any 
 data unless the clipboard contains text.  See Redirection for additional 
 information on CLIP:. 
 Two special commands, ITERATE and LEAVE, can only be used inside a DO / 
 ENDDO loop.  ITERATE ignores the remaining lines inside the loop and 
 returns to the beginning of loop for another iteration (unless DO 
 determines that the loop is finished).  LEAVE exits from the current DO 
 loop and continues with the line following ENDDO. Both ITERATE and LEAVE 
 are most often used in an IF or IFF command: 

 
         do while "%var" != "%var1"
            ...
            if "%var" == "%val2" leave
         enddo
 
 
 You can nest DO loops up to 15 levels deep. 
 The DO and ENDDO commands must be on separate lines, and cannot be placed 
 within a command group, or on the same line as other commands (this is 
 the reason DO cannot be used in aliases).  However, commands within the 
 DO loop can use command groups or the command separator in the normal 
 way. 
 You can exit from all DO / ENDDO loops by using GOTO to a line past the 
 last ENDDO.  However, be sure to read the cautionary notes about GOTO and 
 DO under the GOTO command before using a GOTO in any other way inside any 
 DO loop. 

Created using Inf-PHP v.2 (c) 2003 Yuri Prokushev
Created using Inf-HTML v.0.9b (c) 1995 Peter Childs