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

Hello, I am setting up a cronjob. I need for the script to use the current date to determine which dir to search. Example:
/RESULTS 2009/JAN 2009 has subdirs with dates such as:
Results 01-05-09, Results 01-06-09,...,Results 01-14-09.
How do I get my script to recognize that today is 01-14-09
and read only from the dir Results 01-14-09?
  • Comment on How to use use the days date to read a dir?

Replies are listed 'Best First'.
Re: How to use use the days date to read a dir?
by Fletch (Bishop) on Jan 14, 2009 at 18:15 UTC

    Feed the output from localtime into an appropriately crafted call to strftime from the core POSIX module.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: How to use use the days date to read a dir?
by ww (Archbishop) on Jan 14, 2009 at 19:39 UTC

      I humbly accept the advice. I have not intentionally ignored Ikegami.
      I immediately started testing it in my code.

      I will be consistent about mentioning what
      I have attempted and the subsequent results.

Re: How to use use the days date to read a dir?
by zentara (Cardinal) on Jan 14, 2009 at 18:14 UTC
    #!/usr/bin/perl use POSIX 'strftime'; #my $date = strftime "%x_%T", localtime; my $date = strftime "%x", localtime; $date =~ tr#/#-#; print "$date\n";

    I'm not really a human, but I play one on earth Remember How Lucky You Are

      my $date = strftime "%x", localtime; $date =~ tr#/#-#;

      If you want a specific format, why don't you use that format instead of munging the "national representation of the date"? It doesn't even work (I get "2009-01-14").

      Fix:

      use POSIX qw( strftime ); my $date = strftime "%m-%d-%y", localtime; print "$date\n";

      But since he didn't ask for the date, he'd want

      my $dir = strftime "Results %m-%d-%y", localtime;
        That was very helpful! Thanks!

      I truly appreciate you sharing your wisdom. I will be taking
      a perl course next month at the LearningTree.
      That should plug up some holes.

        Some advice: you learn more by trying to find answers yourself. Then if all else fails, ask here. Searching google and groups.google.com will give you all the answers you probably need as a beginner. Search google for "perl grep files" or "perl grep directories", etc. All we do here, is repeat what was already answered many times before, and is stored in google.(or the searchbox/tutorials here at perlmonks)

        I'm not really a human, but I play one on earth Remember How Lucky You Are
      Thanks, but how do I filter a list of dirs based on the date?
        You don't need to filter when you already know the exact file name.
Re: How to use use the days date to read a dir?
by JavaFan (Canon) on Jan 14, 2009 at 18:15 UTC
    Start with perldoc -f localtime and come back if you have more specific questions.
      I need to filter a glob based on the current date. I only want the most current dirs.
      Ex. today is 01-14-09. I only want that part of the glob. How do you grep that?
        What have you tried?