Thursday, March 31, 2011

Deleting files & directories recursively

use strict;
use warnings;
cleanup("d:/tempo");
sub cleanup {
            my $dir = shift;
        local *DIR;
        opendir DIR, $dir or die "opendir $dir: $!";
            for (readdir DIR) {
                next if /^\.{1,2}$/;
                my $path = "$dir/$_";
            unlink $path if -f $path;
            cleanup($path) if -d $path;
            print "deleting $path\n";   
            }
        closedir DIR;
            print "Deleting $dir";
        rmdir $dir or print "error - $!";

No comments: