How do you loop in a Windows batch file?

ID : 20210

viewed : 13

Tags : batch-filebatch-file

Top 5 Answer for How do you loop in a Windows batch file?

vote vote


If you want to do something x times, you can do this:

Example (x = 200):

FOR /L %%A IN (1,1,200) DO (   ECHO %%A ) 

1,1,200 means:

  • Start = 1
  • Increment per step = 1
  • End = 200
vote vote


FOR %%A IN (list) DO command parameters 

list is a list of any elements, separated by either spaces, commas or semicolons.

command can be any internal or external command, batch file or even - in OS/2 and NT - a list of commands

parameters contains the command line parameters for command. In this example, command will be executed once for every element in list, using parameters if specified.

A special type of parameter (or even command) is %%A, which will be substituted by each element from list consecutively.

From FOR loops

vote vote



for /? 

and you will get several pages of help text.

vote vote


Conditionally perform a command several times.

  • syntax-FOR-Files

    FOR %%parameter IN (set) DO command  
  • syntax-FOR-Files-Rooted at Path

    FOR /R [[drive:]path] %%parameter IN (set) DO command  
  • syntax-FOR-Folders

    FOR /D %%parameter IN (folder_set) DO command  
  • syntax-FOR-List of numbers

    FOR /L %%parameter IN (start,step,end) DO command  
  • syntax-FOR-File contents

    FOR /F ["options"] %%parameter IN (filenameset) DO command  


    FOR /F ["options"] %%parameter IN ("Text string to process") DO command 
  • syntax-FOR-Command Results

    FOR /F ["options"] %%parameter IN ('command to process') DO command 


  • Take a set of data
  • Make a FOR Parameter %%G equal to some part of that data
  • Perform a command (optionally using the parameter as part of the command).
  • --> Repeat for each item of data

If you are using the FOR command at the command line rather than in a batch program, use just one percent sign: %G instead of %%G.

FOR Parameters

  • The first parameter has to be defined using a single character, for example the letter G.

  • FOR %%G IN ...

    In each iteration of a FOR loop, the IN ( ....) clause is evaluated and %%G set to a different value

    If this clause results in a single value then %%G is set equal to that value and the command is performed.

    If the clause results in a multiple values then extra parameters are implicitly defined to hold each. These are automatically assigned in alphabetical order %%H %%I %%J ...(implicit parameter definition)

    If the parameter refers to a file, then enhanced variable reference can be used to extract the filename/path/date/size.

    You can of course pick any letter of the alphabet other than %%G. but it is a good choice because it does not conflict with any of the pathname format letters (a, d, f, n, p, s, t, x) and provides the longest run of non-conflicting letters for use as implicit parameters.

vote vote


FOR will give you any information you'll ever need to know about FOR loops, including examples on proper usage.

Top 3 video Explaining How do you loop in a Windows batch file?