I use something like this to delete temporary directories... it deletes
files in the dir before deleting it though... 

foreach $Dir (@TempDirs) {
        if (-e $Dir and -d $Dir) {
                print "\n-> Looking in directory $Dir\n";
                &ScanDir("$Dir");
        } # end of if
} # end of foreach

sub ScanDir {
        my ($workdir) = shift;
        my ($startdir) = &cwd; # keep track of where we began

        chdir($workdir) or die "\nUnable to enter dir $workdir:$!\n";
        opendir(DIR, ".") or die "\nUnable to open $workdir:$!\n";
        my @names = readdir(DIR) or die "\nUnable to read
$workdir:$!\n";
        closedir(DIR);

OUTER:  foreach my $name (@names) {
                next if ($name eq ".");
                next if ($name eq "..");
                foreach $entry (@exclude) {
                        if ($name eq "$entry") {
                                print "\n$entry is in the exclusion
list.";
                                print "\nSkipping.\n";
                                next OUTER;
                        }# end of if excluded
                } # end of foreach excluded list
                if (-d $name) { # is this a directory?
                        print "\n-> Looking in directory $name";
                        &ScanDir($name);
                        print "\nAttemtping to delete directory $name";
                        rmdir($name) or warn "\nUnable to remove
directory $name: $!\n";
                        next;
                } # end of if
                if ($name ne "") {
                        print "\nAttemtping to delete file $name";
                        unlink($name) or warn "\nUnable to delete file
$name: $!\n";
                } # end of if
        } # end of foreach
        chdir($startdir) or die "\nUnable to change to dir $startdir:
$!\n";
} # end of sub ScanDir

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On 
> Behalf Of John Deretich
> Sent: Tuesday, September 30, 2003 4:05 PM
> To: Perl-Win32-Admin-Request (E-mail)
> Subject: removing empty directories
> 
> 
> 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.
> 
> thanks,
> 
> John
> _______________________________________________
> Perl-Win32-Admin mailing list
> [EMAIL PROTECTED]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> 

_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to