Changeset 10211


Ignore:
Timestamp:
2005-07-05T13:48:35+12:00 (19 years ago)
Author:
davidb
Message:

Introduction of filtered_rm_r(). This function is similar to rm_r() execpt it can take
two extra parameters: the first a regular expression for files to accept, the second a
regular expression to reject. If both of these are undef, then the function works like
rm_r(), consequently rm_r() is now a small wrapper to filtered_rm_r().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/perllib/util.pm

    r10146 r10211  
    5757
    5858
     59
     60# recursive removal
     61sub filtered_rm_r {
     62    my ($files,$file_accept_re,$file_reject_re) = @_;
     63
     64    my @files_array = (ref $files eq "ARRAY") ? @$files : ($files);
     65
     66    # recursively remove the files
     67    foreach my $file (@files_array) {
     68    $file =~ s/[\/\\]+$//; # remove trailing slashes
     69   
     70    if (!-e $file) {
     71        print STDERR "util::filtered_rm_r $file does not exist\n";
     72
     73    } elsif ((-d $file) && (!-l $file)) { # don't recurse down symbolic link
     74        # get the contents of this directory
     75        if (!opendir (INDIR, $file)) {
     76        print STDERR "util::filtered_rm_r could not open directory $file\n";
     77        } else {
     78        my @filedir = grep (!/^\.\.?$/, readdir (INDIR));
     79        closedir (INDIR);
     80               
     81        # remove all the files in this directory
     82        map {$_="$file/$_";} @filedir;
     83        &filtered_rm_r (\@filedir,$file_accept_re,$file_reject_re);
     84
     85        if (!defined $file_accept_re && !defined $file_reject_re) {
     86            # remove this directory
     87            if (!rmdir $file) {
     88            print STDERR "util::filtered_rm_r couldn't remove directory $file\n";
     89            }
     90        }
     91        }
     92    } else {
     93        next if (defined $file_reject_re && ($file =~ m/$file_reject_re/));
     94
     95        if ((!defined $file_accept_re) || ($file =~ m/$file_accept_re/)) {
     96        # remove this file 
     97        &rm ($file);
     98        }
     99    }
     100    }
     101}
     102
     103
    59104# recursive removal
    60105sub rm_r {
    61106    my (@files) = @_;
    62 
    63     # recursively remove the files
    64     foreach my $file (@files) {
    65     $file =~ s/[\/\\]+$//; # remove trailing slashes
    66    
    67     if (!-e $file) {
    68         print STDERR "util::rm_r $file does not exist\n";
    69 
    70     } elsif ((-d $file) && (!-l $file)) { # don't recurse down symbolic link
    71         # get the contents of this directory
    72         if (!opendir (INDIR, $file)) {
    73         print STDERR "util::rm_r could not open directory $file\n";
    74         } else {
    75         my @filedir = grep (!/^\.\.?$/, readdir (INDIR));
    76         closedir (INDIR);
    77 
    78         # remove all the files in this directory
    79         &rm_r (map {$_="$file/$_";} @filedir);
    80 
    81         # remove this directory
    82         if (!rmdir $file) {
    83             print STDERR "util::rm_r couldn't remove directory $file\n";
    84         }
    85         }
    86 
    87     } else {
    88         # remove this file
    89         &rm ($file);
    90     }
    91     }
    92 }
     107   
     108    # use the more general (but reterospectively written function
     109    # filtered_rm_r function()
     110
     111    filtered_rm_r(\@files,undef,undef); # no accept or reject expressions
     112}
     113
     114
     115
    93116
    94117# moves a file or a group of files
Note: See TracChangeset for help on using the changeset viewer.