Using FOR To Process Data Lines


FOR looks like just another way to process lines. But it has one thing going for it under Windows 95. If the data line you happen to be processing is a list of file masks (like *.xls or 1999*.doc), the LFNFOR command can be used to modify how FOR will read the file names -- as long or short names.  

Let's jump straight into a command-line example of how to use FOR:

E:\>for %x in (one two three) do echo %x

E:\>echo one
one

E:\>echo two
two

E:\>echo three
three
As you can see, the FOR command caused echo to be executed three times -- once for each word in it's "set" of arguments. If we were able to replace the "set" with an entire data line, we could do some real processing without the crazy antics other methods need. Obviously, we'll have to replace the echo command with something a bit more useful, but you already figured that out. Now, the only reasonable way I can see to get an entire data line into the middle of our FOR line is to put that line in the environment. So we'll go to the next example, this one a batch file assuming the prior existence of an environment variable named "DATALINE":
@echo off
echo echo %%1> process.bat
for %%x in (%dataline%) do call process.bat %%x
del process.bat
Assuming our environment variable DATALINE contains the same one two three text, the example above would generate this output:
one
two
three
Admittedly, our PROCESS.BAT was rather simple. The trick was the assumption of the prior existence of our data in an environment variable. And that's where the trouble is. Putting a line in the environment requires the antics I'd hoped to avoid. Check here for tips on how to put data lines in the environment.

Now lets discuss file masks and the LFNFOR command.

C:\Temp>dir

 Volume in drive C is WINDOWS95
 Volume Serial Number is 1251-1BED
 Directory of C:\Temp

.              <DIR>        04-14-97  8:23a .
..             <DIR>        04-14-97  8:23a ..
LONGFI~1 TXT            16  06-12-98  8:52a long file name.txt
         1 file(s)             16 bytes
         2 dir(s)      55,918,592 bytes free
C:\Temp>lfnfor on

C:\Temp>for %x in (*.*) do echo %x

C:\Temp>echo long file name.txt
long file name.txt
C:\Temp>lfnfor off

C:\Temp>for %x in (*.*) do echo %x

C:\Temp>echo LONGFI~1.TXT
LONGFI~1.TXT
So let's put it all together. Suppose you want to print out all your basic and batch files. Notepad can do this under batch control with the /p option. But it prints out the title at the top of each page, and it will print the long or short name depending on how it opened the file. So this line in a batch file:
for %%x in (*.bas *.bat) do start /w /m notepad.exe /p "%%x"
would give you long file names at the top of each page if you preceded it with lfnfor on, or short names at the top of each page if you preceded it with lfnfor off.

Lost? Look at the site map.

Bad links? Questions? Send me mail.

Google
Yahoo
Ask Jeeves