John Deretich wrote:
> Hello,
>
> I was wondering if anyone has a script that
> will remove empty directories and subdirectories.
> The code that I am using will delete the entries
> at the first sublevel but not at multiple sublevels.
>
> Here's my code:
>
> opendir(EMPTYDIR, $searchdrive1) ;
> while ($directoryempty = readdir(EMPTYDIR)) {
> if ($directoryempty ne "." && $directoryempty ne "..") {
> $directoryempty = $searchdrive1 . "\\" . $directoryempty;
> if (-d $directoryempty) {
> opendir(EMPTY, $directoryempty);
> while ($subdirectoryempty = readdir(EMPTY)) {
> if ($subdirectoryempty ne "." && $subdirectoryempty
> ne "..") {
> $subdirectoryempty = $directoryempty . "\\" .
> $subdirectoryempty;
> system ("rmdir \"$subdirectoryempty\" ") || warn
> "Cannot remove $subdirectoryempty: $! \n";
> }
> }
> system ("rmdir \"$directoryempty\" ") || warn "Cannot remove
> $directoryempty: $! \n";
> }
> }
> }
>
> I have File::Path but when I run it, it will remove more than what I need.
Don't follow the last line - what does it do wrong ?
Here's a recursive version you can try (actual rmdir commented out till
you have it well tested).
use strict;
rm_empty_dirs (shift || 'c:');
exit;
sub rm_empty_dirs {
my $dir = shift;
local *DIR;
opendir DIR, $dir or die "opendir $dir: $!";
my $found = 0;
while ($_ = readdir DIR) {
next if /^\.{1,2}/;
my $path = "$dir/$_";
rmemptytree ($path) if -d $path;
$found++ if -e $path;
}
closedir DIR;
if (not $found) { # no files/dirs found in this dir
print "Would rmdir ($dir)\n";
# rmdir ($dir) or die "rmdir $dir: $!"; # remove # after verifying code
}
}
__END__
--
,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED]
(_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED]
/ ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs