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

I'm new to Perl and the only real experience I've had with it has been self-taught through a beginners book. I recently was asked to do a script that will perform certain functions in a windows directory, and was hoping I could get a little help. Here's what its supposed to do:

Monitor and scan a windows directory daily and see if any files are older than 5 days. If they are then the script should log the files in a blank text document and delete them from the directory.

You can see what I have so far on my public scratchpad. I'm sure it looks pretty bad, but like I said, I just started learning. I could still use some help with the pieces I'm missing, as well as any clean up I can perform. Any advice you guys can give me would be much appreciated. Thank you.
  • Comment on script that logs and deletes old windows files

Replies are listed 'Best First'.
Re: misguided intern in need of help
by Transient (Hermit) on Jun 17, 2005 at 19:01 UTC
    Some functions that might be good to know:

    open
    print
    readdir
    stat
    unlink

    Try it out! Get a test area set up and go nuts! see what happens :)
Re: misguided intern in need of help
by ikegami (Patriarch) on Jun 17, 2005 at 19:13 UTC

    I like to use the core module Time::Local to check for time differences.

    use Time::Local qw( timelocal_nocheck ); my ($mday, $mon, $year) = (localtime())[3, 4, 5]; my $five_days_ago = timelocal_nocheck(0, 0, 0, $mday-5, $mon, $year); foreach my $filename (...) { next unless (stat($filename))[9] < $five_days_ago; ...do something... }
Re: misguided intern in need of help
by Zaxo (Archbishop) on Jun 17, 2005 at 19:26 UTC

    The -M file test operator will be helpful. Use unlink to delete regular files.

    After Compline,
    Zaxo

      Absolutely. The beta sample chapter from "Learning Perl 4" also gives some insight and examples.

      Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

      Don't fool yourself.
Re: misguided intern in need of help
by ww (Archbishop) on Jun 17, 2005 at 18:59 UTC
    misguided:

    Not to be harsh, but use the monastery's search for at least two recent threads on this very topic; use search before posting new ques... .oO: uht-oh, too late! I've already approved the question....

    So, as a bonus: see also threads with references to modules such as File::find

    Welcome to the Monastery, but save yourself a few ego_bruises by reading How do I post a question effectively? and other introductions to practice and etiquette amongst the monks.

      Thanks for the welcome and advice. I guess I should have looked a little harder through prior questions. I'll research the material as per your suggestion.
Re: misguided intern in need of help
by tcf03 (Deacon) on Jun 17, 2005 at 19:52 UTC
    Heres something Ive used in the past - you'll have to test it and probably modify it somewhat, but this should give you a good start.
    use strict; use warnings; use POSIX qw(strftime); my $modtime_seconds = ( stat($file) )[9]; $modtime_seconds = $modtime_seconds - 86400; my $modtime = ( ! defined ($modtime_seconds) ) ? "null" : POSIX::strftime( "%B %d", $modtime_seconds, 0, 0, 0, 0, 0, 0, 0, + 0 );


    update
    or find /yourdir -type f -mtime +5 -exec rm -rf {} \;


    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson