subhankar has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

Example file name :
ccsCDRFileGenerator-0-24161-1176950330-17914920070419_2.CDR

I have to read in files based on the datestamp as given here as 20070419
The script will run every night and I have to pickup files from the previous day. But I cant use ctime mtime as the timestamps might change or other files may also come in that way.

Any help would be much appreciated.

Thanks & regards
Subhankar

Replies are listed 'Best First'.
Re: Picking up files based on datestamp
by johngg (Canon) on Sep 19, 2007 at 08:40 UTC
    As well as using a regular expression, as suggested by moritz, have a look at opendir, readdir and grep. You should be able to arrive at something along the lines of

    my @filesWanted = grep { -f && m{your regex} } readdir $dirHandle;

    I hope this is of use.

    Cheers,

    JohnGG

Re: Picking up files based on datestamp
by siva kumar (Pilgrim) on Sep 19, 2007 at 08:42 UTC
    Identify the "yyyymmdd" value from the file name using the below perl regex.
    $filename = "ccsCDRFileGenerator-0-24161-1176950330-17914920070419_2.C +DR"; ($yyyymmdd) = $filename =~ m/\d+(\d{8})\_\d+\.CDR$/; print "DATE is : $yyyymmdd \n"
    Using the DateTime::Precise module, identify yesterday's "yyyymmdd" value. Then you can pickup the files as you want..
Re: Picking up files based on datestamp
by moritz (Cardinal) on Sep 19, 2007 at 08:05 UTC
    Well, do that.

    Maybe perlre helps you with that.

Re: Picking up files based on datestamp
by subhankar (Novice) on Sep 19, 2007 at 10:09 UTC
    Dear all

    Thank you all for your inputs. I can manage the regexp part, but the main problem is identifying yesterday's "yyyymmdd" value.

    Sivakumar: I cant install any external modules as of now (sucks, but well..) and I dont have the DateTime::Precise module.

    How do I identify yesterday's $yyyymmdd value using the default modules/functions?

    Help!

    Thanks & regards,

    Subhankar
      you can find yesterday's yyyymmdd using
      my $timestampminus = time - 24 * 60 * 60; my ($year, $month, $day) = (localtime($timestampminus))[5,4,3]; $year += 1900; $month++; print "Yesterday was $year$month$day.\n";
      Use time and localtime.

      my ($day, $month, $year) = (localtime(time() - 86400))[3, 4, 5]; $month += 1; $year += 1900;

      I hope this is of use.

      Cheers,

      JohnGG

        time() - 86400

        Beware that this calculation is correct only if days are 24 hours long. Not all of mine are. :)

        perldoc -q yesterday

        -pilcrow
Re: Picking up files based on datestamp
by subhankar (Novice) on Sep 19, 2007 at 11:35 UTC
    Dear Monks,

    Thank you all for your excellent tips!! I am so glad I am doing perl :-)

    Cheers!

    Subhankar
Re: Picking up files based on datestamp
by subhankar (Novice) on Sep 21, 2007 at 06:00 UTC
    Dear Monks

    Please help me a little more with the dates.

    Using the localtime(time) option, currently I am getting the month as a sigle digit i.e. September as 9.

    But how do I get the month as 09?

    ($day, $month, $year) = (localtime(time() - 86400))[3, 4, 5]; $month += 1; $year += 1900;


    Thanks & regards,

    Subhankar
        Thanks Siva!!

        I had already figured that out :-) I thought there could be a way within localtime(time) itself.

        Thanks & regards, Subhankar