[comp.sys.apple] Reading the CATALOG

mdavis@hp-sdd.UUCP (System Administrator) (05/21/87)

Yes, AmperWorks would be overkill if all you need was to read a catalog into
memory -- and had no need for all the other features in AmperWorks.

So...here's the method of reading a catalog from BASIC.SYSTEM into a string
array.  This is documented in the "BASIC Programming with ProDOS" manual (or
whatever its called) that's been out by Apple for eons.  Note that the strings
that are returned are about 80 characters -- each string is worth one line of
information that you see when you do a full "Catalog" command from immediate
mode.

        10 DIM F$(255): REM The number of files -- should be plenty
        20 D$ = CHR$(4)
        30 INPUT "Pathname to a directory: ";DIR$
        40 PRINT D$"OPEN"DIR$",TDIR"
        50 PRINT D$"READ"DIR$
        60 INPUT NAME$: INPUT HEADER$: INPUT NULL$
        70 INPUT A$: IF A$ = "" GOTO 90
        80 F = F + 1: F$(F) = A$: GOTO 70
        90 INPUT BLOCKINFO$: PRINT D$"CLOSE"
        100 PRINT F" files in "DIR$
        110 PRINT NAME$: PRINT HEADER$: PRINT NULL$
        120 FOR I = 1 TO F: PRINT F$(I): NEXT
        120 PRINT NULL$: PRINT BLOCKINFO$

The operations here should be obvious.  The directory is opened, like a text
file, but since its a DIR type file, you have to give the ",TDIR" parameter.
Each line is read in and stored into the appropriate variable: header
variables are NAME$ (the name of the directory), HEADER$
("Name...Type...Blocks...etc."), and NULL$ (just a blank entry before the file
names start).  Then each file entry is read into F$() until a blank ("") one is
taken.  Finally, the last line containing the block usage information is read
into BLOCKINFO$.

[Note that a bug in BASIC.SYSTEM will cause an END OF FILE error to occur when
this is used on the /RAM volume of 128k systems.  With error trapping, you can
get around this.  Sort of.]

The drawbacks to this are that you will be reading all the directory
information even though you might not want it.  If you require just the
filename, you'll have to use the MID$() function -- and even then, you'll have
to run a loop which lops off extraneous space characters.  All this can be
really slow if your program requires any kind of speed performance.

That's why I put the &FILES command into AmperWorks so that it would just read
the filenames from a directory into an array with no extra spaces and no other
information.  Because it works with ProDOS (and not through BASIC.SYSTEM) the
following one-liner is many times faster than the above BASIC listing:

                & FILES (DIR$, F$), F

..and as you would guess, this reads all the filenames from pathname
contained in DIR$ into the F$() array.  The number of filenames read are
returned in the F numeric variable.  (And there are no errors when reading the
/RAM disk).  The only other advantage of this worth mentioning is that your
programs can tell &FILES to read in only the filenames that match certain
filetypes.  For example:

                & FILES (DIR$, F$, 4), F

This reads only the filenames with a numeric type of 4 ("TXT" file) into the
F$() array.  Conversely, if you wanted to read all filenames *except* those
which are TXT files, you would use:

                & FILES (DIR$, F$, -4), F

The negative 4 means, "ignore all filetypes of 4".

When I wrote "Learning Microsoft BASIC 2.0 for the Macintosh" (1984, CompuSoft
Publishing), I learned about the FILES command and realized that you often
encounter situations where filetype exception cases are important, especially
under ProDOS.

--Morgan Davis

UUCP: [ ihnp4 akgua hplabs!hp-sdd sdcsvax nosc ] !crash!pnet01!pro-sol!mdavis
ARPA: crash!pnet01!pro-sol!mdavis@nosc
INET: mdavis@pro-sol.CTS.COM