Understanding the Command Line For Loop

Written by Barry Dysert (last updated December 20, 2021)

As you get more comfortable typing at the command prompt, you may want to explore what the For loop can do for you. Fundamentally, the For loop lets you execute commands over a set of items like files or directories. So, for example, if you want to perform the same activity over a set of files, using a For loop can provide tremendous time savings.

The general form of a For loop is:

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

Where "%variable" can be anything from "%a" through "%z"; the "set" is the list of space-separated file names on which you want to perform the "command"; and the "command-parameters" are an optional list of parameters to accompany the "command".

As an example, if you wanted to display to the screen the contents of files a.txt, b.txt, and c.txt you could do so using the following For loop:

FOR %i IN (a.txt b.txt c.txt) DO TYPE %i

If you have command extensions enabled, there are some additional forms of the For loop:

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

In this form, if "set" contains a wildcard character then it represents directories instead of files.

FOR /R [[drive:]path] %variable IN (set) DO command [parameters]

In this form, the loop walks the directory tree specified by "[[drive:]path]", executing the FOR statement in each directory along the path.

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

In this form, the set is a sequence of numbers from "start" to "end", by "step" amount.

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

In this form, "set" is one or more file names. Each file is opened, read, and processed before going on to the next file in "set". This particular form is quite powerful when "options" are specified. Details and examples can be found by typing "FOR /?" at the command line.

 This tip (13118) 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." ...

MORE FROM BARRY

Restoring Your System from a Restore Point

Restore Points let you go "back in time" to a point before you made system changes that could prove harmful to your ...

Discover More

M8 Free Clipboard

If you've ever found it tedious to do copy/paste with one piece of information at a time, you'll really like M8 Free ...

Discover More

Renaming Folders Pinned to Quick Access

The Quick Access folder in File Explorer has been around for a while now. In fact, I use it to begin almost all of my ...

Discover More
More WindowsTips

Using the Sort Command

Sorting data is a common task even of end users. Fortunately, The Windows command line provides us with a Sort utility so ...

Discover More

Setting Time Limits for Windows 10 Users

If your computer is set up in crowded environment, it may be helpful to establish times that an account can be used. You ...

Discover More

Switching Between Command Line and File Explorer

Sometimes you can be more effective in a command window, and other times you can be more effective using File Explorer. ...

Discover More
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}] (all 7 characters, in the sequence shown) 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 two less than 9?

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


Newest Tips