FOR IN() DO

The FOR IN() DO command of MS-DOS opens up a whole avenue of batch file enhancements.

For example, suppose you wanted to create a list of all the files in the hidden C:\_SYS directory. Of course, you could enter DIR C:\_SYS from the C:\ prompt. That would give you all the file names, the file extensions, the bytes, the date and time of creation. However, suppose you only wanted the file names and their extensions without all the extra information.

You can use the built-in MEMO application to create a small batch file. Be sure to press <ENTER> after the second line.

echo off for %%f in ( *.* ) do echo %%f

[Note: Ed indicates below that *.* (or any characters inside the parentheses) must be separated from open and close parentheses by an empty space {e.g. ( *.* ), not (*.*)}. The batch file above worked on my 512K 95LX with or without the empty spaces --Rich.]

Save the file as FLIST.BAT and run it from FILER by highlighting FLIST.BAT and pressing F4.

If you create FLIST.BAT exactly as shown you'll be rewarded with a list of the file names in the C:\_SYS directory. If, instead, you get "Syntax error," you may be experiencing what I believe to be a rare bug in some versions of the 95LX's operating system. In every other version of DOS I've worked with, the batch command "FOR %%M IN(*.*) DO ECHO %%M" works as the MS-DOS manual says it should. However, it doesn't work on my HP 95LX.

I lost a couple hours of sleep before I somehow surmised that if I put spaces on either side of "*.*" inside IN( ), the FOR IN() DO command would work. Hopefully, the fact that someone is watching over DOS for you will let you sleep tonight.

What the FOR IN() DO command does is to convert the wild cards "*.*" to the actual file names.

You can modify this FLIST.BAT file to make it create another batch file. For example, modify the second line in FLIST.BAT above like so:

for %%f in ( *.* ) do >> do.bat echo type %%f

[Again, make sure there are spaces between the parentheses and asterisks.]

Be sure to use the ">>" (append) operator rather than the DOS ">" (put to) operator.

Now run FLIST.BAT, and after a slight pause, check your file listing. You should find DO.BAT on your disk. The file will contain lines like "TYPE filename.ext." When you run DO.BAT, text files will successfully be listed on the screen. However, your DO.BAT file might contain commands like type cost.wk1, type command.com or type clock.exm. These commands will usually produce garbage on the screen when they are "Typed." The technique of using one batch file to create another can be useful in other situations.

Suppose you wanted to edit all the *.TXT files in C:\_DAT. You can modify FLIST.BAT so that it looks like the following:

echo off

if exist do.bat del do.bat

for %%f in ( c:\_dat\*.txt ) do echo vde %%f >> do.bat

vde do.bat

pause

do

[Again, make sure there is an empty space between the parentheses and the phrase c:\_dat\*.txt. ]

Your FLIST.BAT file may differ somewhat. The above FLIST.BAT assumes that you have *.TXT files in C:\_DAT. It also assumes that you have VDE.EXE * on your HP 95LX, and that VDE is somewhere on the DOS PATH. (VDE - Video Display Editor is an alternative text editor described on page 27 of the Jan/Feb 92 issue and page 41 of the Fall 91 issue of The HP Palmtop Paper.)

The second line in FLIST.BAT erases any existing DO.BAT file. The append operator (">>") adds lines to an existing DO.BAT file. Deleting the DO.BAT file lets the ">>" operator start appending to an empty file. The fourth line in the batch file uses VDE to edit DO.BAT before running the newly created batch file. The PAUSE command lets you press <CTRL>-C to "Terminate batch job" before FLIST.BAT jumps to DO.BAT.

You can name FLIST.BAT anything you want. But wait: Why stop there? Consider the following batch file, MKBAT.BAT * ("MKBAT" stands for Make Batch).

echo off

if (%1)==() goto error

if (%2)==() goto error

if (%3)==() goto error

if exist %3.Bat del %3.Bat

for %%f in ( %2 ) do echo %1 %4 %%f >> %3.bat

rem add %5 thru %9 where needed

vde %3.Bat

pause

%3

:error

echo off

rem %1 %2

echo Usage: MKBAT DOS_Command FileMask

echo NewBatFilename [parameter_n]

rem %3 %4...%9

[Again, make sure there is an empty space between the parentheses and the phrase %2 on line 6.]

This elaborate file uses command line parameters. Command line parameters are represented inside MKBAT.BAT as %1, %2, etc.

Say, for example, you wanted to use the ATTR.COM program to turn on the Read-Only attribute for several files in the A:\BIN directory. You could use the command:

MKBAT attr A:\BIN\*.* do +R

In this case, MKBAT will check to see that you have used the first three command line parameters. (The fourth through the ninth parameters are optional.) MKBAT will then create a DO.BAT file with several lines like this:

attr +R A:\BIN\filename.ext

MKBAT calls VDE to let you examine the DO.BAT file and, if desired, delete lines. Upon exiting from VDE, you can press any key to jump to the DO.BAT program or press <CTRL>--C to terminate the batch file.

By now, you've either seen how the technique works and have started writing your own version of MKBAT.BAT, or you're shaking your head in wonderment. If you're among the latter, don't be too discouraged. You should have been here when I was agonizing over the MKBAT.BAT file. Maybe then you'd believe me when I say, "I understand how you feel." Do as I did. Take a nap. Put the article aside and come back to it after reading a good novel. If you want to see what the MKBAT.BAT file does, try keying it in, but omit the ECHO OFF command and insert PAUSE commands every other line. Then run MKBAT.BAT and watch what happens.

[Ed Keefe is an author, programmer, computer science instructor, and long-time contributor to support publications for HP computers. He is the president of the FastAid Company, 314 S.W. Logan, Ankeny, IA 50021. Ed's CompuServe ID# is [73277-1064].]