Using the FOR Statement

by Barry Dysert
(last updated October 23, 2017)

The FOR statement is a command that allows you to "loop" or repeat commands multiple times. It is very similar to the FOR statement provided in several programming languages. The FOR statement is often used in batch files, but it can also be used directly on the command line.

As a reminder, the general form of the FOR loop is:

FOR %variable IN (set) DO command [command-parameters]

Where 'variable' is a single letter, '(set)' specifies a set of one or more files, 'command' is the command-prompt command to be executed for each file in the set, and 'command-parameters' are optional parameters to be passed to 'command'.

So let's say you have been given several COM .dll files that you need to register within Windows' Registry. You could put them all in one folder and can then run the following command from within that folder:

C:\DLLs> FOR %i IN (*.dll) DO regsvr32 /s %i

What this does, for each .dll file it finds in the current directory, is to run the regsvr32 program on it. (The "/s" switch simply tells regsvr32 to do its work silently.) The variable used ("%i") is case sensitive, and it assumes the value of each .dll file in turn. The name of the .dll file is then passed to the regsvr32 program using the same "%i" variable.

A variant of the general FOR loop uses the /L switch and has this general form:

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

This executes a loop where %variable is first set to 'start' and is incremented by 'step' until it hits the value 'end'. (This usage should be very familiar to programmers.) So let's say you need to display the list of numbers from 1 to 5. You could do it this way:

C:\> FOR /L %i IN (1,1,5) DO @echo %i

Another variant of the FOR loop uses the /D switch and has this general form:

FOR /D %variable IN (set) DO command [command-parameters]

With this variant, if 'set' contains wildcards, then the command works on directory names instead of file names. This would be useful, for example, if you wanted to copy a set of files from a directory tree and put all the files in one directory.

For example, I have a directory called \Temp. It contains subdirectories called \Temp\Temp2 and \Temp\Barry. The \Temp\Barry and the \Temp\Temp2 directories contain files I want to copy. If I wanted to copy all of the files (without the directory structure) in the \Temp tree to a directory called \Files, I could do it like this:

C:\Temp> FOR /D %i IN (*) DO Copy %i\*.* \Files

I have left to the end what I think is the most esoteric form of the FOR loop. It seems that using the /F switch will allow you to do almost anything—if you can only figure out how to use it. The general form is:

FOR /F "options" %variable IN (`command`) DO command [command-parameters]

(Sorry it doesn't all fit on one line.) There are several 'options you can use, and the first 'command' (the one surrounded by the backwards apostrophes) can be any command-prompt command. So here's one example of how to use this:

FOR /F "Usebackq Delims==" %i IN (`dir/b c:\temp\a*.tmp`) DO @echo c:\temp\%i | Findstr /f:/ "a b c"

(Sorry it doesn't all fit on one line.) This command performs a DIR/B command on C:\Temp\A*.tmp. For each file it finds, it echoes the file name and pipes it to the Findstr command, which looks for all lines containing an "a", "b", or "c". A pretty odd thing to do, granted, but at least it shows how you can use /F. I suggest you do FOR /? to get a complete list of what all the options are, and then you're basically bound only by your imagination on figuring out how to use it.

You can also use the FOR command within a batch file. The only thing that's different is that you double the %-signs, whereas at the command line you only use one %-sign. For example, if the second example above were to be placed in a batch file, it would look like this:

FOR /L %%i IN (1,1,5) DO @echo %%i

 This tip (13196) applies to Windows 7, 8, and 10.

Author Bio

Barry Dysert

Barry has been a computer professional for over 35 years, working in different positions such as technical team leader, project manager, and software developer. He is currently a software engineer with an emphasis on developing custom applications under Microsoft Windows. When not working with Windows or writing Tips, Barry is an amateur writer. His first non-fiction book is titled "A Chronological Commentary of Revelation." ...


Searching From the Start Menu

You can be more efficient if you get used to using the search functionality from the Start menu. This tip discusses that.

Discover More

Creating a Media Player Playlist

Windows Media Player lets you manager all sorts of media. One way to do this is by creating playlists. This tip describes ...

Discover More

Understanding the Command Line For Loop

A niche command that sometimes comes in handy is the FOR loop. It has several forms and therefore can serve several ...

Discover More
More WindowsTips

Using Batch Files, Part 1

This tip is part of a series that shows you how to create and use Windows batch files. It introduces a few commands and ...

Discover More

Creating a Simple Batch File

One of the powerful capabilities built into Windows is the ability to create and use batch files to perform a wide ...

Discover More

Using Batch Files, Part 3: The IF Command

This tip is part of a series that talks about Windows batch files. It introduces a few more commands you can use in your ...

Discover More

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 1 + 7?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)

Newest Tips

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.