TOOLKIT\APPLY.DOC  ·  DOC  ·  4.4 KB  ·  1988-10-01  ·  from PersonalComputing_Oct-1988_Disk-2
APPLY (c) 1987  Michael M Rubenstein

These programs may be freely distributed provided no fee is charged.


                                  DESCRIPTION
                                  -----------

APPLY executes a command for each of a set of arguments (typically, but not 
necessarily, generated by wild card specification of file names). APPLY is more 
versatile and easier to use than the DOS FOR command.

XAPPLY executes a command in each directory of a tree.


                                  USING APPLY
                                  -----------

APPLY is executed with the syntax

   APPLY [<options>] <command> [<arg>...]

The arguments may contain wild cards (? and *) which are expanded to match 
normal files (not hidden or system files and not directories). For example, to 
print every file with extension C or H, one would use the command

   APPLY print *.c *.h

In many cases, however, one the command will contain fixed arguments (i.e., 
arguments used in each invocation) as well as the variable ones to which it is 
being applied. For example, to copy all files with extension C or H to the 
directory \CFILES, commands of the form

   COPY foo.c \cfiles
   COPY bar.c \cfiles

would have to be generated. This can be done with the command

   APPLY "copy $1 \cfiles" *.c *.h

Note that the command is enclosed in quotes, to make it a single argument. This 
is necessary whenever the command contains space or tab characters (apostrophes 
can also be used in this case).

The command may contain pipes or redirection, e.g.,

   APPLY "more <$1" *.c

In this case quotes (NOT apostrophes) must be used.

The list of arguments may be taken from standard input (usually redirected from 
a file), by using the -i switch, e.g.,

   APPLY -i "cc" <files

The input must contain one line for each argument.

More than one argument may be used in the command simply by referring to 
additional arguments as $2, $3, ..., $9. For example, the command

   APPLY "foo $1 $2 test" a b c d

would generate the commands

   foo a b test
   foo c d test

Alternatively, if all arguments are to be placed at the end of the command, the 
number of arguments may be specified as - <number>. For example,

   APPLY -2 bar a b c d

would generate

   bar a b
   bar c d

Multiple arguments are rarely useful when wild card expansion is used.

In both of the above cases, the number of arguments must be a multiple of the 
number used in each generated command. The command

   APPLY -2 bar a b c

will generate the command

   bar a b

but will then give an error message and terminate because only one argument is 
left. If the -v switch is included

   APPLY -v -2 bar a b c

the list is expanded by empty arguments to the required length and the commands

   bar a b
   bar c

are generated.

If the number of arguments is given as -0, all arguments are included in a 
single command. For example

   APPLY -0 foo a b c

would generate

   foo a b c

Obviously, this isn't very useful except when the arguments are wild card 
expansions or come from a file. Care must be taken that the expansion does not 
exceed the maximum (usually at least 110 characters are permitted for the 
command and arguments, depending on the COMSPEC environment variable. In no 
case are more than 123 characters permitted.)

Two switches, -d and -e, may be used to modify the arguments. The -e switch 
ignores file extensions. This can be useful for mass renames, e.g,

   APPLY -e "rename $1.c $1.x" *.c

or when the command generates a new version, e.g.,

   APPLY -e "sed -f fixup $1.c >$1.new" *.c

The -d switch ignores the drive specifier and any higher directories. To delete 
all files from the current directory which have the same name as a file in 
\TEST, the command

   APPLY -d del \test\*.*

could be used.

Normally, APPLY echoes each command to stdout and then executes the command. 
The -q switch prevents echoing to stdout. The -x switch prevents execution 
(stdout can be redirected to a file which may be edited or used by another 
program).

Finally, in some special situations $ may not be suitable as a command 
character. It may be changed with the -a switch, e.g.,

   APPLY -a& "foo &1 test" *.c

If apply is invoked with no arguments or if the -h switch is given, a brief 
command summary is displayed.