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

how can i get the modified date of the files in dd/mm/yy format . also let me know if i intake from user the date period from which to which does a user wants to process files, then how to get the files during that period and store them in an array

Replies are listed 'Best First'.
Re: perl dates
by TedYoung (Deacon) on Jan 04, 2005 at 15:45 UTC

    Hi,

    Date::Format from CPAN can aid you in formatting dates. As for Modification times, you want to use the stat function.

    To print the modification date of a file in dd/mm/yy format you could use:

    use Date::Format; $file = '...filepath...'; $modTimeInSec = (stat $file)[9]; print time2str("%d/%m/%y", $modTimeInSec);

    For more info on formatting dates, check the documentation for Date::Format.

    Your second question is a little more difficult. I will give you some places to look, but how you want to do it will ultimately depend on how exactly you want this to work.

    How does the user enter the date range? Is it an exact date-time, or the current day, or all files in a month? etc. What ever you decide, you may want to use Date::Parse to help you parse dates and times.

    Once you have a start time and end time in seconds since epoch, then you need to search through your files. For example, to get and array of files in the current directory that were modified between $startTime and $endTime, you might use the following:

    @files = grep { my $mod = (stat $_)[9]; $startTime <= $mod && $mod <= $endTime } <*.*>;

    This is a quick example and isn't perfect. The glob operator (<*.*>) specifies a glob pattern to match. If you want to search through directories recursively, check out File::Find.

    Sorry, I can't be more of a help with your second problem, but it really depends on a better explanation of what you are trying to do. I hope my tips help.

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
Re: perl dates
by dragonchild (Archbishop) on Jan 04, 2005 at 15:37 UTC
    What have you tried? This sounds very much like homework. We'll help you if you get stuck or give you advice on how to get started, but we don't do your work for you.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.