Join Them Files

See all articles from CATS v10 n7
Author(s): Duane Parker
Publication: CATS Newsletter
Pub Details: CATS v10 n7 (Volume 10, Issue 7)
Date: December 1992
Pages: 4-5

One of our members wanted to join two or more files together into one big one and couldn’t find an easy QDOS command that would do that. I couldn’t either. (See the CATS N/L, Jan 1989, p. 4 for an earlier discussion.) MS-DOS allows one to append one file to another (concatenate) just by putting “+”‘s between names:

COPY FLA.ASC + FLB.ASC FLC.ASC

This statement causes file “FLA.ASC” to be copied to “FLC.ASC” and then “FLB.ASC” to be appended to it. There are a number of “switches”, and wildcard file descriptions that may be used – check with a good MS-DOS manual. Since QDOS doesn’t work that way, what can we do?

For one thing we could open a new file into which we want to join the others, open the source files in turn, and repeatedly INPUT strings from source and PRINT strings to the receiving file. We repeat this process until each file “PRINT”ed to the end, and then CLOSE all the files. For example, if we had two files with lists of names, Relatives_lis and Friends_lis that we wanted to combine into All_lis, on a second microdrive, the following should work:

10 OPEN_NEW #8,MDV2_All_lis
20 OPEN_IN #5,MDV1_Relatives_lis
30 OPEN_IN #6,MDV1_Friends_lis
40 REPeat loop_a
50 INPUT #5,x$
60 PRINT #8,x$
70 IF EOF(#5) THEN EXIT loop_a
80 END REPeat loop_a
90 REPeat loop_b
100 INPUT #6,x$
110 PRINT #8,x$
120 IF EOF(#6) THEN EXIT loop_b
130 END REPeat loop_b
140 CLOSE #5: CLOSE #6: CLOSE #8

This program segment will work properly if the lis files have lines, that is, if the line-feed character is present, CHR$(10). The line length must be shorter than the QL allows, and the file must end with a line-feed. Without a line-feed at the file-end, some bytes may not be copied, and the program stops with an error message. This can be corrected if we copy byte by byte, i. e., replace 50 & 60 and 100 & 110, each with a single line.

50  PRINT #8,INKEY$(#5)
100 PRINT #8,INKEY$(#6)

Is there an easier way? Yes, there is, if you have Tebby‘s Toolkit II. The procedure uses the background spooler that he provides, started with the SPL command, entered as:

SPL name_1 TO name_2

The typical use would be to spool a file to a printer, e. g.:

SPL Friends_lis, ser1

Note that the comma may be used instead of TO. The spooler is then started as a separate job, and copies the file to ser1 – it is suspended if the device is in use. In other words, you may have the QL print files while you multitask another program. But, you must be careful to avoid closing or opening ser1 at the same time – odd things may occur. If name_2 is a filename, e. g. flp2_All (or just All, for the default drive), the name_1 is copied over the file “All” and the old file lost. If no “All” is on the drive the spooler creates it. Tebby provides a SPL_USE command to set-up a default destination, e. g., first:

SPL_USE ser1

then

SPL Relative_lis

will copy file “Relative_lis” to ser1 and stop when it is finished.

Under one method, SPL may be used to join files, although Tebby doesn’t make that clear in his manual. The commands are:

SPL #channel_x to #channel_z

then

SPL #channel_y to #channel_z

Channels x and y must be from files opened for input and z must be for a file opened for output. If all these files are closed (“CLOSE”), the file opened as z will be as long as the sum of those from x and y. To illustrate this the following program was written to concatenate 5 files named, tst1, tst2, etc.

100 b=FOP_NEW('trial')
110 FOR i= 1 TO 5
120 a=FOP_IN('tst'&i)
130 tflen = FPOS(#b)+FLEN(#a)-1
140 SPL #a TO #b
150 REPeat check:IF FPOS(#b)>tflen:EXIT check:REMark This loop prevents closing channel #a before spool is done.
160 PAUSE 20:REMark This prevents 0-length files from stopping the program.
170 CLOSE #a
180 END FOR i
190 CLOSE #b

I hope that all of this will let you join those files, if not, I have written a SBasic program, all Babbage’d up (Herb Schaaf‘s term) that will do the joining for you. Just ask, I can give you a copy.

Finally, for those interested enough to get this far, I discovered some quirks and bugs in the SPL and SPL_USE commands. First, these apply only to Tebby’ TK2, other toolkits may have the same commands, that might work differently. In no particular order, I list some quirks or bugs that I found. To illustrate, channels 3 and 4 represent files opened for input, while 5 is one for output.

1. Joining files also works as follows:

SPL tst1 TO #5

then

SPL tst2 TO #5

But, SPL #3 TO All, will just overwrite “All”.

2. The command:

SPL_USE #5

does not result in an error message until you try SPL tst1, then QDOS reports: “Bad Name”.

3. If the receiving file is opened, e. g.: OPEN #5, All, then

SPL tst1 TO All

gives an error of “In use”, but if you first, SPL_USE All, then OPEN #5,All, and finally,

SPL tst1

no error is reported and the spooler writes over whatever is at the beginning of the file, but leaves the remainder intact!

4. If #5 is closed during the spooling(SPL #3 TO #5), an error results, but some garbage is also written to the channel (maybe 40 bytes or so).

If you find anymore quirks, or tricks to avoid the problems, let me know.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.