Classic Computer Magazine Archive COMPUTE! ISSUE 148 / JANUARY 1993 / PAGE 58

For's forte is repetition. (DOS command) (Column)
by Tony Roberts

One of my favorite DOS commands is also a power command. The For command lets you perform the same operation several times, or on several files. It's documented in the batch programming section of your DOS manual, so you may think it can only be used in batch files, but that's not so.

Although the syntax is slightly different in each application, For can be used both in batch files and at the command line.

Here's how it works: Let's say you want to list the contents of a set of files--say, your batch files--to the screen. Type for %f in (* .bat) do type %f. The %f is a placeholder. When the line is executed, it's replaced in turn by each member of the series indicated by the data in the parentheses. In this case, that series includes every file with a bat extension in the current subdirectory.

You most often see %f used as a placeholder, but in practice, any letter will do. When a For command is used in a batch file, the placeholder designation includes two percent signs (% %f). When For is used on the command line, only one percent sign (%f) is necessary.

For isn't a command you'll use every day, but it's a good option to consider when you're facing a big task. For example, if you want to place a copyright notice at the end of each of 100 text files, you could open each file and type in the new line, but that would get old quickly. The For command can have the job finished in a flash.

First, create a file that contains the copyright notice. Save it with the name notice. Then, type the following: for %f in (*.txt) do copy %f+notice.

This example combines the For and Copy commands to append the text in the notice file to every file with the txt extension in the current subdirectory. The plus sign between the %f and the filename notice activates Copy's append feature.

Before you use this or any For command to modify your important files, make a set of backups of those files. Also, test the syntax on one or two files first. Just as For is powerful enough to get a lot of work done in a hurry, it can also do heavy damage just as quickly if your command isn't constructed just right.

The For command can be used with programs as well as with other DOS commands. I frequently use the For command to update archive files I've created with the PKZIP utility. For. instance, you might type for %f in (*.zip) do pkzip -f%f.

This invokes the PKZIP utility for each of the zip files in the current subdirectory. The -f switch tells PKZIP to freshen the zip files--to replace any file contained in the archive with a newer file of the same name if such a file exists.

Although most uses of the For command involve a series of filenames, there are other ways the command can be used. For example, the following short batch file might come in handy if you're hooked up to a network and want to know which drive designations are in use.

@echo off for % of in (C D

E F G H I J K L M N O P)

do if exist %%f:/** echo

Drive %%f is online.

This batch file cycles through a list of drive names and checks for the existence of files there to determine whether the drive is available. If the drive is available, a message indicating the fact will be printed onscreen.

You can even use the For command to compare text strings entered by the user. This might allow you to set up a simple password system for running various programs. Here's an example of a batch file that requires a password to run the program Fungame.

@echo off

for %%f in (cat

dog pig) do if %1==%%f goto ok echo Sorry, you didn't

supply a valid password. goto end :OK fungame :end

This batch file fragment requires the user to enter a valid password (in this case cat, dog, or pig) as a parameter when running the batch file. The for line compares the user's input (the % 1) with each of the three words listed in the series. If there's a match, the program jumps to the OK label and runs Fungame. If the password doesn't match up, the batch file prints a message and then ends.

Although this security system won't fool anyone savvy with computers, your kids might get a kick out of having a secret password.

If you have a job for the For command, give it a try, but don't give up if things don't go right the first time. Building a sucessful For command usually takes experimentation, but if it's set up correctly, it can save you a lot of time.

After you've put in the effort to create a good For command, don't let it go to waste. Document the command--what what it does, how to use it, and so on-with rem statements in a batch file, and store it where you can refer to it next time an industrial-strength project corps up.