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

Hello, How would I check a directory so see if any files in the directory are more than two hours old and then if so, send an email message telling me that? Thanks!

Replies are listed 'Best First'.
Re: Checking Age of Files
by ikegami (Patriarch) on Jan 25, 2010 at 17:42 UTC
    1. Get the list of the files in the directory.
    2. For each file in the directory.
      1. Compare their mtime with the current time.
      2. If the difference is greater than two hours,
        1. Note the file name
    3. If any file names were noted,
      1. Send an email
Re: Checking Age of Files
by kennethk (Abbot) on Jan 25, 2010 at 17:42 UTC
    What have you tried? Have you written any code? Effort is appreciated - see How (Not) To Ask A Question.

    You can check how old a file is using the -M function. You can get the contents of your directory using the set opendir, readdir, and closedir, where there is an example at readdir.

    There are large number of email modules freely available on CPAN - one useful search term would be email. Email::Sender::Simple, for example, may do what you want.

    Update: Note toolic's comment below regarding units. -M does return in units of days, but in decimal days. A simple conversion factor of 24 is all that is required.

      You can check how old a file is using the -M function.
      The OP wants hours, but the doc says (emphasis is mine)
      # -M Script start time minus file modification time, in days.
      Probably a solution using stat and one of the CPAN Date/Time modules will be needed.
        It returns a real (as opposed to integer) number of days, so -M * 24 > 2 should do fine for the OP if he's ok with being off by an hour around DST changes.
Re: Checking Age of Files
by Anonymous Monk on Jan 25, 2010 at 17:48 UTC