Using the FOR Statement

by Barry Dysert
(last updated August 11, 2014)

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 and 8.

Author Bio

Barry Dysert

Barry has been a computer professional for over 30 years, working in different positions such as technical team leader, project manager, and software developer.  He is currently a senior software engineer with an emphasis on developing custom applications under Microsoft Windows. ...

MORE FROM BARRY

Allowing Remote Desktop

Remote Desktop allows you to access other systems and operate them from your own system. If you want to use this capability ...

Discover More

Performing Complex Calculations Using the Scientific Calculator

The next time you need to perform calculations at your computer, there's no need to fire up a big spreadsheet program or even ...

Discover More

Understanding Data Execution Prevention

Windows has many malware-prevention features. One of these is Data Execution Prevention. This tip provides an explanation of ...

Discover More
More WindowsTips

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

Using Batch Files, Part 2

This tip is part of a series that talks about Windows batch files. It introduces a few more commands and special characters ...

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 variety of ...

Discover More
Subscribe

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

View most recent newsletter.

Comments

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 7 - 0?

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


Newest Tips
Subscribe

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.