Saturday, August 7, 2010

How to Write Batch Files to Process Multiple Files and Looping

Batch file programming is a very useful way to automate small, repetitive, tasks. The Windows Command Line Programming tutorial is a good starting place to read about the basics of batch file programming, for those who are new to creating batch files.

It is also a good idea to have a debugger handy, such as the Running Steps batch file IDE and integrated debugger.


The FOR command has 2 uses in batch file programming:
  • Processing lists of files;
  • Looping through variable values.
In command line batch file programming, FOR works rather like other looping constructs common to all procedural programming languages. The difference between traditional FOR commands for counted loops is that, in MS-DOS/Windows Command Line Batch File programming, it can be used to process a list of files.

Basic Generic Form

The basic form for the FOR command is:
FOR %file_name IN (criteria) DO command
The %file_name is a variable that will contain the name of the current file being processed. These follow usual command line batch programming variable naming conventions; they start with a % (if used in a batch file), and can be any alpha value. They can not be the numbers 0 to 9, because these are reserved for the parameters fed to the batch file on the command line.

The criteria can be any wildcard-enabled search that can be issued using the DIR command in the command line environment. The following are all valid:
  • *.*
  • *.html
  • 00_??.*
The command is any valid command including call with a batch file name to execute an external batch file, or an executable file name. So, to concatenate a list of text files, the following could be used:
echo > %2

for %%A in (%1) do type %%A >> %2
The above assumes that it is called with a command such as:
  • concat_files *.txt output_file.txt
There are a few more options that can be used with file processing and the FOR command.

Using FOR to Process Lists of Files

The /D switch may be used between the FOR keyword and the variable name to limit the selection of files to folders. This might be useful in performing selective backups, for example. The /R switch can be used to walk the current directory tree. This will cause the command to recurse into each folder that it finds, and look for files that match the criteria.
If a path to a folder is supplied after the /R switch, then

Using FOR to as a Looping Construct

With the /L switch, it is possible to execute a command a given number of times based on a variable set that has a start, step and end value. The command will loop through the values, starting at start adding step at each iteration, and then stopping when the current value is equal to, or greater than end. To loop forwards:
for /L %%A in (0, 1, 10) do echo %%A
It is also possible to loop backwards:
for /L %%A in (10,-1, 0) do echo %%A
And also, step by a different amount:
for /L %%A in (0, 2, 8) do echo %%A
Note that all that has happened in this usage is that the file set has been replaced by a numerical set of values.
Debugging FOR loops can be quite tricky, and so a good debugger such as the Running Steps IDE is useful for stepping through the code.

No comments:

Post a Comment