Classic Computer Magazine Archive COMPUTE! ISSUE 144 / SEPTEMBER 1992 / PAGE A16

Patterns of force. (specifying file names through pattern matching)
by Jim Butterfield

Pattern matching is a good way to specify filenames when you want to refer to several files or when you're not quite sure of a name. For example, typing DIR M#? will show you all files that start with the letter M; the #? part means "any number (#) of any character (?)."

AmigaDOS Release 2 adds new features to Amiga pattern matching and also makes a small change in how one feature's handled. The vertical bar character (1) is a shifted key located in the upper right area of your keyboard next to the Backspace key. It's used in pattern matching to signal an OR relationship. If you type DIR C:D#?IE#? on an Amiga running AmigaDOS 1.x, you'll get the names of all files in C: with names that start with D or E This feature still exists in Release 2, but with this version you need to put the expression in parentheses--you'd type DIR C:(D#?IE#?).

Even if you're not running Release 2 yet, start using those parentheses right away. They work fine on 1.x systems and also on earlier ones, and you might as well get into the habit of using the new syntax now since you'll need to use it when you upgrade.

That vertical bar can be handy, and the parentheses make an expression more readable. Here's an example that programmers might use. Suppose you have the files DOG.C, DOG.H, and DOG in RAM and want to move them to DFO:. You could type COPY RAM:DOG(%I.CI.H) DFO:.

Keep in mind that the percent symbol (%) means "nothing." That part of the pattern match would match the file called DOG. Here's another example: To move the commands LIST, TYPE, and CD into the RAM disk for faster access, you would type COPY C:(LISTITYPEICD) RAM:. The RESIDENT command is a better way to keep programs available in RAM, though, since executing multiple copies of each program won't use extra memory. The 1.3 version of RESIDENT doesn't do pattern matching, however, so you'd need to execute the command three times--once for each program you'd wish to make resident. Or you could use a script.

A few Shell commands won't accept pattern matching in a filename, at least in their 1.x versions. Sometimes the reason for this is obvious. Commands such as CD, PATH, and ASSIGN must specify a single file path; if pattern matching were allowed, more than one path might match the pattern, with confusing results.

Other commands, such as RESIDENT, PROTECT, TYPE, and RENAME, don't allow pattern matching, yet their functions cry out for it. For example: To move a group of files from one subdirectory on a disk to another, it would be convenient to be able to type RENAME CAT/#? MOUSE This command, if it worked, would move all files from subdirectory CAT into subdirectory MOUSE on the same disk. The Release 2 version of RENAME will do the job that way, but earlier versions won't.

We're not stuck, however. The hard way would be to repeat the RENAME command, moving the files one at a time. There's an easier way--to use SPAT (Single PATtern match) or DPAT (Double PATtern match). These are scripts that reside in the S: directory. With care, you can get them to pattern-match filenames for almost any command.

Let's take on the RENAME task above. First, let's move some files to the RAM disk for our experiment. Issue the following commands.

CD RAM: MAKEDIR RAM:CAT MAKEDIR RAM:MOUSE XCOPY C:(D#? IE#? IF#?)RAM:CAT

So far, we've copied all the commands in the C: directory that start with the letters D, E, or F RENAME wouldn't be appropriate for the above action for two reasons: We're copying from one disk to another, and we don't want to move the files from their original location in C:. I use XCOPY instead of COPY as a matter of habit; XCOPY causes dates and protection bits to be preserved in the copies. XCOPY is a system alias.

We're ready for the main task. We'll use the RENAME command to move all files within RAM:CAT that start with the letter D to the directory RAM:MOUSE. They'll disappear from RAM:CAT, since the filenames are relocated to a new directory; the files themselves aren't actually moved. To do the move, type DPAT RENAME RAM:CAT/D#? RAM:MOUSE/. Note the slash at the end of the line. It tells DPAT that MOUSE is a directory rather than a file.

Why use DPAT rather than SPAT? At first glance, we seem to have only one pattern in the command line, and DPAT is a two-pattern command. Well, DPAT needs to insert the filename in two places in the RENAME command.

Don't forget to confirm that DPAT RENAME has done its job correctly; get directory listings of CAT and MOUSE. Release 2 saves you the extra work by allowing you to type the command as RENAME RAM:CAT/D#? RAM:MOUSE, with or without a slash behind MOUSE The system will figure out what you want.