On 12/19/05, May, Robert <> wrote:
> gao perlone wrote:
> > how to delete a directory and its
> > subdirectory using Perl under Windows?
>
> See the rmtree() function in the File::Path module.
>
> Rob.
>
> =========================================================
> >
> _______________________________________________
> Perl-Win32-Users mailing list
> Perl-Win32-Users@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
I cobbled together the attached monstrosity. File::Path would have been great.
I just included the sub routine that does the deleting. Just give it
a directory name and it will start wiping them out.
-Jason
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 "..");
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
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs