#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0

    Help : Trying to use Find::File::Rule ???


    Hi

    Below is a simple test program. The end use of this program is to produce a list of sub directories sorted by age. The age of the sub directories is identifed by the names which have the format:

    YYYYMMDD-HHMM

    There is only one layer of sub directories.

    I want to produce the list to allow me to sequencially delete the sub directories starting with the oldest. This is part of a script to purge older directories and files in a system where there is a stream of image files being produced. The incoming images will be dropped into new sub directories created on each day.

    There are other sub directories in the base directory that are not named by the date format. I don't want to delete these. The regex filter needs to work.

    The problem I have is that I can't get the Find::File::Rule call to filter on the sub directory naming format. I have tried different things to get the regex string to work but it doesn't. I am stuck.

    Code:
    #!/usr/bin/perl 
    # =========================================================== 
    use warnings;
    use strict;
    use File::Find::Rule;
    
    my $dir_path = "/var/images/";
    my $filter = '/\d{8}-\d{4}$/';
    
    
    #my @folders = reverse sort File::Find::Rule->directory->in( $dir_path );
    #my @folders =  File::Find::Rule->directory->name($filter)->in( $dir_path );
    my @folders =  File::Find::Rule->directory->in( $dir_path );
    
    foreach my $i (@folders){
      print "$i\n";
    }
    The program above produces produces the output below. The code works as written but I can't get it to do what I want. I haven't yet got to the point of sorting the list.


    Code:
    dazz@alpha:~$ ./directory_lister.pl
    /var/images
    /var/images/20130110-2259
    /var/images/20130112-1003
    /var/images/20130108-2115
    /var/images/20130108-2228
    /var/images/current
    /var/images/20130108-2105
    /var/images/20130108-2131
    /var/images/20130109-2344
    /var/images/20130109-2345
    /var/images/lost+found
    /var/images/20130108-2107
    /var/images/20130108-2132
    /var/images/20130108-2109
    /var/images/20130111-2304
    /var/images/20130108-2135
    /var/images/20130109-2340
    /var/images/20130108-2125
    /var/images/20130109-2300
    /var/images/20130108-2233
    /var/images/20130108-2054
    /var/images/test
    /var/images/smallsample
    /var/images/20130108-2055
    dazz@alpha:~$
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,898
    Rep Power
    3887
    Use qr// to quote regular expressions. i.e.
    Code:
    my $filter = qr/\d{8}-\d{4}$/;
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by ishnid
    Use qr// to quote regular expressions. i.e.
    Code:
    my $filter = qr/\d{8}-\d{4}$/;
    Perfect

    Thanks

    Dazz

IMN logo majestic logo threadwatch logo seochat tools logo