[comp.os.vms] Recursive Deletion of Directory Trees

whitfill@hc.DSPO.GOV (Jim Whitfill) (02/29/88)

 
The following is a program (recursive in DCL) and an associated sub-
program that will recursively delete directories and subdirectories,
including the root directory.
 
 
 
 
$	save_verify := 'f$verify(0)
$	set message/notext/noid/noseverity/nofacility
$ ! SYS$UTILS:[COMMANDS]DELTREE.COM
$ !
$ ! This command file (used recursively) will delete the contents of a root
$ ! directory, including the root directory and all subdirectories and files.
$ ! 
$ !
$ ! 		P1 = device:[directory] location of root directory
$ !		P2 = special delete switches, i.e. /BEFORE=YESTERDAY
$ !
$	set noon
$	batch = "NO"
$	if f$mode() .nes. "INTERACTIVE" then batch = "YES"
$	start_directory = f$logical("sys$disk") + f$directory()
$	wo := write sys$output
$	root = p1
$	if p1 .nes. "" then goto have_root
$ get_root:
$	if batch then wo "Directory not given in batch mode"
$	if batch then goto delete_done
$	inquire root "Enter DEVICE:[ROOT]"
$	if root .eqs. "" then goto delete_done
$ have_root:
$	colon = f$locate(":",root)
$	if colon .ne. f$length(root) then goto get_dir
$	wo "You must enter a DEVICE:"
$	goto get_root
$ get_dir:
$	device = f$extract(0,colon,root)
$	deldir = f$extract(colon+2,f$length(root)-colon-3,root)
$	define/user sys$output nl:
$	define/user sys$error nl:
$	directory 'root'
$	if $status .eq. %X00000001 .or. $status .eq. %X10018290 -
						then goto root_ok
$	wo "No such directory path ''root'"
$	if batch then goto delete_done
$	goto get_root
$ root_ok:
$	if batch then goto dont_check
$       wo ""
$       wo "              !!! NODE  ''f$logical("node")' !!!"
$       wo ""
$	wo "DELETE root dir, all subdirs and all files of ''root'"
$	inquire really "Are You SURE? [NO]"
$	if .not. really then goto delete_done
$ dont_check:
$	set def 'root'
$ !
$ !		Invoke recursive procedure to delete the directory tree
$ !		subject to any selection criteria in P2.
$ !
$	@sys$utils:[commands]deldirsub "''p2'"
$ !
$ !		Now get rid of top level directory
$ !
$	if f$locate(".",root) .ne. f$length(root) then goto sub_delete
$	set prot=sy:rwed 'device':[000000]'deldir'.dir;1
$	delete'p2' 'device':[000000]'deldir'.dir;1
$	if p2 .nes. "" then -
	wo "''device':[000000]''deldir'.dir;1 deleted, if created ''p2'"
$	if p2 .eqs. "" then -
	wo "''device':[000000]''deldir'.dir;1 deleted"
$	goto delete_done
$ sub_delete:
$	set default [-]
$	parent = f$directory()
$	parent = parent - "[" - "]"
$	deldir = deldir - parent - "."
$	set prot=sy:rwed 'device':'deldir'.dir;1
$	delete'p2' 'device':'deldir'.dir;1	
$	dot = f$locate(".",root)
$	root = f$extract(0,dot,root) + "]"
$	if p2 .nes. "" then -
	wo "''root'''deldir'.dir;1 deleted, if created ''p2'"
$	if p2 .eqs. "" then -
	wo "''root'''deldir'.dir;1 deleted"
$ delete_done:
$	set message/facility/text/severity/id
$	set def 'start_directory'
$	reset = f$verify(save_verify)
$ !		SYS$UTILS:[COMMANDS]DELDIRSUB.COM
$ !
$ !		This command file will recursively delete files in a dir tree
$ !		starting at the current directory.
$	set noon
$ !
$	dirtodelete = f$directory()
$ !
$ !		See if subdirectories exist and if so, go delete them.
$ !
$ sub_loop:
$	subdir = f$search("*.DIR;1")
	if subdir .eqs. "" then goto do_one
	subname = f$parse(subdir,,,"NAME")
	subdirect = dirtodelete - "]" + "." + subname + "]"
	set def 'subdirect'
	@sys$utils:[commands]deldirsub "''p1'"
	set def [-]
	goto sub_loop
$ do_one:
$ !
$ !		Delete all files subject to P1 qualifier (P2 may be null)
$ !
$	set proc/priv=bypass
$	delete'p1' *.*;*
$	set proc/priv=nobypass
$	write sys$output "All files deleted in ''dirtodelete' ''p1'"
$	exit
 

                   =======================================
                               Jim A. Whitfill
                    Los Alamos National Laboratory (LANL)
                            Group MEE-10, MS J580
                             Los Alamos, NM 87545
                                (505) 667-9282

                 (ARPAnet ==> whitfill%meediv.xnet@lanl.gov)
                   =======================================