More Powerful Batch Files Part II - Iterating with "For"


The very useful "for...in...do" statement is discussed

Computers are very good at doing the same thing over and over. The command line contains a powerful and versatile method for carrying out this type of operation. With this method, you can automate many time-consuming tasks. The basic statement is of the form: for {each item} in {a collection of items} do {command} (For those who persist in calling the command line DOS, note that the 32-bit version of the "For" statement is much more powerful than the old 16-bit DOS version.)

A single-letter replaceable variable is used to represent each item as the command steps through the the collection (called a "set"). Note that, unlike most of Windows, variables are case-dependent. Thus "a" and "A" are two different variables. The variable has no significance outside the "For" statement. I will be using X throughout the discussion but any letter will do. (In principle, certain non-alphanumeric characters can also be used but that seems like a bad idea to me.) The variable letter is preceded with a single percent sign when using the command line directly or double percent signs in a batch file. Thus the statement in a batch file looks like this: for %%X in (set) do (command) What makes the "For" statement so powerful is the variety of objects that can be put in the set of things that the command iterates through, the availability of wildcards, and the capability for parsing files and command output. A number of switches or modifiers are available to help define the type of items in the set. Table I lists the switches. They are listed in upper case for clarity but are not case-sensitive.

Switch Function
/D Indicates that the set contains directories.
/R Causes the command to be executed recursively through the sub-directories of an indicated parent directory
/L Loops through a command using starting, stepping, and ending parameters indicated in the set.
/F Parses files or command output in a variety of ways

I will consider a number of examples that illustrate the use of "For" and its switches.

Simple iteration through a list

The set of things that are to used can be listed explicitly. For example, the set could be a list of files: for %%X in (file1 file2 file3) do command (Care must be taken to use correct paths when doing file operations.) A different example where the set items are strings is: For %%X in (eenie meenie miney moe) do (echo %%X)Wildcards can be also be used to denote a file set. For example: for %%X in (*.jpg) do commandThis will carry out the command on all files in the working directory with extension "jpg". This process can be carried further by using several members in the set. For example to carry out a command on more than one file type use: for %%X in (*.jpg *.gif *.png *.bmp) do command

As always, keep in mind that the command line may choke on file names with spaces unless the name is enclosed correctly in quotes. Therefore, you might want to use "%%X" in the "command" section.

Looping through a series of values

The well known action of stepping through a series of values that was discussed in connection with "if" and "Goto" statements is succinctly done with the switch /l (This switch is an "ell", not a "one") . The statement has the form: for /l %%X in (start, step, end) do command The set consists of integers defining the initial value of X, the amount to increment (or decrement) X in each step, and the final value for X when the process will stop. On the previous page, I gave an example batch file that listed all the numbers from 1 to 99. If we use a "For" statement, that task can be accomplished with one line:for /l %%X in (1,1,99) do (echo %%X >> E:\numbers.txt)The numbers in the set mean that the initial value of X is 1, X is then increased by 1 in each iteration, and the final value of X is 99.

Working with directories

If you wish to use directories in the variable set, use the switch /d. The form of the command is for /d %%X in (directorySet) do commandAn example that would list all the directories (but not sub-directories) on the C: drive is for /d %%X in (C:\*) do echo %%X

Recursing through sub-directories

If you want a command to apply to the sub-directories as well as a parent directory, use the switch /r. Then the command has the form: for /r [parent directory] %%X in (set) do command Note that you can designate the top directory in the tree that you want to work with. This gets around the often cumbersome problem of taking into account which is the working directory for the command shell. For example the statement: for /r C:\pictures %%X in (*.jpg) do (echo %%X >> E:\listjpg.txt) will list all the jpg files in the directory C:\pictures and its sub-directories. Of course, a "dir" command can do the same thing but this example illustrates this particular command.

Parsing text files, strings, and command output

Now we come to a truly powerful switch that was not even dreamed of back in the DOS days of yore. The switch /f takes us into advanced territory so I can only indicate the many aspects of its application. Things become rather complex so those who are interested should consult programming books or the Microsoft documentation. However, here is a brief sketch of what's involved.

This version of the "For" command allows you to examine and parse text from files, strings, and command output. It has the form for /f [options] %%X in (source) do command "Options" are the text matching criteria and "source" is where the text is to be found. One of the interesting applications is to analyze the output of a command or commands and to take further action based on what the initial output was.

Back to top

{ezoic-ad-1}
{ez_footer_ads}