9.8. Removing a Directory and Its ContentsProblem
You want to remove a directory tree recursively without using Solution
Use the Example 9.3: rmtree1#!/usr/bin/perl # rmtree1 - remove whole directory trees like rm -r use File::Find qw(finddepth); die "usage: $0 dir ..\n" unless @ARGV; *name = *File::Find::name; finddepth \&zap, @ARGV; sub zap { if (!-l && -d _) { print "rmdir $name\n"; rmdir($name) or warn "couldn't rmdir $name: $!"; } else { print "unlink $name"; unlink($name) or warn "couldn't unlink $name: $!"; } }
Or use Example 9.4: rmtree2#!/usr/bin/perl # rmtree2 - remove whole directory trees like rm -r use File::Path; die "usage: $0 dir ..\n" unless @ARGV; foreach $dir (@ARGV) { rmtree($dir); }
Discussion
The File::Find module exports both a
We have to use two different functions,
Check first that the file isn't a symbolic link before determining if it's a directory. See Also
The Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|