APPLY.DOC 26 February 87 Page 1
Copyright 1987 Michael M Rubenstein
These programs may be freely distributed provided no fee is
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.
APPLY is executed with the syntax
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
APPLY.DOC 26 February 87 Page 2
redirected from a file), by using the -i switch, e.g.,
apply -i "cc"
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 -
apply -2 bar a b c d
bar a b
bar c d
Multiple arguments are rarely useful when wild card expansion is
In both of the above cases, the number of arguments must be a
multiple of the number used in each generated command. The
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
If the number of arguments is given as -0, all arguments are
APPLY.DOC 26 February 87 Page 3
included in a single command. For example
apply -0 foo a b c
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.
XAPPLY is executed with the syntax
The command is executed in the specified directory (the current
APPLY.DOC 26 February 87 Page 4
directory if none is specified) and in all subdirectories. The
command must be enclosed in quotes or apostrophes if it contains
space or tab characters. It must be enclosed in quotes if it
contains redirection or pipes.
The directories may be specified with wild cards. Nondirectories
matching the wild card specification will be ignored.
Usually it does not matter in what order the subdirectories are
visited. Normally, XAPPLY executes the command in each
subdirectory before executing it in a given directory. For cases
in which it does matter and this is not correct, the -r switch
may be used to execute the command in a directory before
executing it in subdirectories.
If no arguments are given or if the -h switch is given, XAPPLY
displays a command summary.
For the Hacker.
APPLY and XAPPLY are written in AZTEC C with a modified run time
system. Most of the code is very standard and it should be easy
to modify it for other C compilers. The major difficulty will
probably be in emulating the command line handling functions of
the modified run time system.
Argument parsing (including handling quoted arguments and wild
card expansion) is done by the modified run time system.
Normally only normal files are matched. The declaration
int _wildattr = ST_DIRECT;
in XAPPLY.C informs the run time system that directories are also
to be matched.