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

I'm trying to get a script that deletes all files in a directory that's X days old. Something where the settings for directory, file extensions, and modification date are easy to change.
my $dir = "C:\Windows"; my @ext = qw(gif jpeg pdf doc); my $age = "2" # days ago, not in hours # open directory and delete all files that are modified $age ago
Thanks!

Replies are listed 'Best First'.
Re: Deleting files of a specific extension for dir
by Corion (Patriarch) on Oct 04, 2008 at 18:38 UTC

    So, where's your question?

    Most likely, you want to read about the -M operator in perlop, which was mostly made for just this task.

    Also, you might want to read in perlop about "Quotes and Quotelike Operators", because double quotes and backslashes together are special.

    Other things to read are glob or File::Find or File::Find::Rule.

Re: Deleting files of a specific extension for dir
by Illuminatus (Curate) on Oct 04, 2008 at 19:21 UTC
    On Linux, you do not need perl:
    find $dir -mtime +$age -name '*\.(gif|jpeg|pdf|doc)' -delete
    There is a windows equivalent at http://unxutils.sourceforge.net/. However, as the esteemed corion has stated, File::Find::Rule will perform the same function.