Commodore Disk Pattern Matching
Jim Butterfield, Associate Editor
The flexible Commodore DOS allows the user to LOAD, Scratch, and obtain a directory of files using the symbols * and ? as pattern matchers. The quirks of these two symbols can, however, cause problems. For one thing, you might accidentally erase an entire diskette.
Commodore disk drives are versatile; sometimes we don't realize how versatile they are. In this article, we'll discuss pattern matching: how it works, and how to use it to get rid of an annoying "comma" file that sometimes appears on your disk directory.
First, a recommendation: Unless you have 4.0 BASIC (in the PET/CBM series of computers), learn how to use the Wedge or DOS Wedge utility program. It's a great convenience. We'll refer to wedge commands within this article. The DOS Wedge has many handy features, but the two most important are these: You can find out about a disk error at any time by typing the @ key followed by a RETURN; and you can examine a disk directory without disturbing the program within your computer's memory by typing @$ followed by RETURN.
It's possible to identify one or more programs on disk without specifying their full names. Match the missing part of the filename by using a pattern. The two characters used for this are:
? – to match any single character;
* – to match any following characters.
If I have two files, one named DIG and the other, DOG, I can specify a name which matches both files with D?G—the question mark matches any character. If I have files named HOUSE, HO, HOTDOG, and HORRIBLE, I can match them all with HO*—the asterisk matches any group of characters, including no character.
This is good if you can't remember a filename exactly. If you have a file that might be called CATFOOD or might be called CAT FOOD, but you can't remember which, you can load it regardless of name with LOAD "CAT*",8. The first file whose name begins with CAT will be loaded. Unfortunately, you might discover that instead of the program you wanted, you have loaded something else, such as CATCH-MICE. The first name in the directory that matches will be the one loaded.
We can use pattern matching to get around this problem. If you load the directory using pattern matching, you'll see all programs that fit the pattern. To examine CAT programs, type:
or, with the wedge program:
You'll see a list of all programs (if any) whose names begin with the characters CAT, which allows you to select the one you want.
Note that LOAD picks the first program that matches, but the directory picks all programs that match.
It's probably obvious that SAVE must not allow pattern matching. You must save a real name, not an approximation. Thus, SAVE "CAT*",8 will produce a syntax error from the disk.
The Scratch command does accept pattern matching; all files that match will be removed from the disk. Use pattern matching with great care when using Scratch; you could remove more files than you planned.
To scratch all files from a disk that begin with the letter M, you would type the following:
OPEN 15, 8, 15 PRINT#15,"S0:M*"
or, using the wedge:
@S0 : M*
Be careful. There might be more files starting with M than you expected. Take a directory listing first (using pattern matching, of course).
Here's another example. Suppose you've been writing a BASIC program called DIS. As you write code, you save the program from time to time, creating DIS1 and DIS2. Then you start testing and correcting, saving new versions as you go, and create DIS3, DIS4, and DIS5. Finally, you're satisfied, and you save your final version as DISK/EDIT. How can you get rid of your five development programs, named DIS1 to DIS5? Easy. Scratch pattern DIS? and they will all go. DISK/EDIT will stay, since the ? character matches only a single character. Do not scratch pattern DIS* since that would definitely clobber DISK/EDIT.
But be careful. Just before you give the command to scratch pattern DIS?, take a directory with the same pattern. You might have other files called DISK or DISH that match the same pattern. So you might code:
LOAD "$0 : DIS?" LIST
or, with the wedge:
You'll see the programs that match the name pattern. If they are exactly the ones you want, type the Scratch command; or with the wedge, you can go back and type over the dollar sign with the letter S; pressing RETURN will scratch these files.
There are other patterns that are less well-known. For example, a filename is a pattern; it must be matched exactly. Thus, if I have a file named HOG and I want to see that it is in the directory, and perhaps check the number of blocks, I can type:
LOAD "$0:HOG",8 LIST
@$0 : HOG
The only item in the directory will be file HOG (if it exists).
Let's take this a step further. Suppose I don't want to see any file details. All I need is the title of the disk, its ID, and the number of blocks free. That's easy: Just specify a file that does not exist on the disk. The directory will then consist of the title line and the blocks free information. I often ask for a directory using a filename such as 0:#$&!%. This isn't an expletive; it's just a name that I know doesn't exist on the disk so that I'll get the blocks free count.
The Lone Asterisk
You would think that a pattern consisting of only a single asterisk would mean "any file." Thus, a command such as LOAD "*",8 would bring in the first file since anything will match. That's not quite correct: The asterisk often has a special meaning.
The single asterisk sometimes means "same name as last time." It may have been Commodore's intention to allow a user to load a program, and later save it with the same name with SAVE "*",8, the asterisk meaning "same name as before." This was never implemented fully, but you can see traces of this idea in the dual disk copy command. If you have a dual disk, type:
@C1 :*=0 : PROGNAME
We can see that this command asks to copy a file called PROGNAME to drive 1; but what name will the new file be given? The destination name is *—which in this case means "same name." Thus, the new file will be named PROGNAME, too. It seems that it was originally Commodore's intention to allow copying to take place with pattern matching, so that C1:* = O:RA* would copy all files whose names started with RA from drive 0 to drive 1 with the same name. If you have a dual drive, try it; it almost works correctly.
So it turns out that LOAD "*",8 does not always load the first file on the disk. Sometimes it loads the same file that was previously loaded.
You may specify a file type by adding an equals sign to the pattern followed by the file designation: S for Sequential, P for Program, U for User, and R for Relative types. You may also type the three-letter designation such as SEQ or PRG if you wish. Thus, 0 : * = S will reference all sequential files, 0 : B* = P will reference all programs whose names start with B, and 0 : ? = P will reference all programs with one-letter names.
Next month we'll look at a common disk error and a way to fix it.