[comp.os.vms] Apply a command to a random filespec

PKARP@SRI-ROBOTX.ARPA (Peter Karp) (04/27/88)

Many VMS programs (DCL and otherwise) don't expand wildcards in
filenames.  The command procedure below makes it easy to execute
an arbitrary program over an arbitrary filespec.  Enjoy...


$!  Invocation:		@FORFILES  FILESPEC  CMD
$!
$!  Written by Peter D. Karp
$!
$!  This command procedure executes the DCL command CMD once for every
$!  file indicated by FILESPEC.  On each execution, a different filename
$!  specified by FILESPEC is substituted for each occurrence of the string
$!  "FILE" in CMD, e.g.,
$!
$!	$ @FORFILES  *.COM  "DIFF FILE OTHER_DIR:FILE"
$!
$!
$ on control_y  then goto eof
$!
$! Get the list of specified files.  Default to latest version only.
$!
$ filespec = f$parse(p1,";0")
$ dir/output=sys$login:filelist.tmp/nohead/notrail/col=1  'filespec'
$!
$! For each file...
$!
$ open/read  fileF  sys$login:filelist.tmp
$!
$FILE_LOOP:
$!
$! Read its name and type.
$!
$	read/end_of_file=EOF  fileF  full_filename
$	name = f$parse(full_filename,,,"NAME")
$	type = f$parse(full_filename,,,"TYPE")
$	filename = name + type
$!
$! Now substitute 'filename' for every occurrence of "FILE" in CMD.
$!
$	cmd = p2
$!
$	SUBST_LOOP:
$		pos = f$locate("file", cmd)
$		if  pos .ne. f$length(cmd)  Then Goto SUBST_MATCH
$		pos = f$locate("FILE", cmd)
$		if  pos .eq. f$length(cmd)  Then Goto DONE_SUBST
$	SUBST_MATCH:
$		cmd = f$extract(0,pos,cmd) + filename -
		      + f$extract(pos+4,f$length(cmd)-pos-4,cmd)
$		goto SUBST_LOOP
$DONE_SUBST:
$!
$! Execute the command!
$!
$ write sys$output "$ ''cmd'"
$ 'cmd'
$!
$ goto FILE_LOOP
$!
$EOF:
$
$ close fileF
$ delete sys$login:filelist.tmp;*
-------