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

I have a routine that searches a directory and it's sub directories for files that are older than five days. All is working great when I use my perl program on windows or UNIX Platforms. But now I need to have an additional check that the file also starts with the letter "L" or the word "Log". Any time I add this additional check in my sub routine no files are found at all, so I have something wrong. Here is what I have that works:
{ find (sub {my $filename = $File::Find::name; if ( -f $filename ) { my $filetime = (stat "$filename")[9]; my ($sec, $min, $hour, $day, $month, $year) = (localtime($filetime +))[0,1,2,3,4,5]; if ($now - $fivedaystime > $filetime){ my $mm = $month+1; my $yy = $year+1900; open (LOGFILE, '>>', $script_auditlog) or die "Could not open fi +le '$script_auditlog' $!"; print LOGFILE "$filename last modified on $mm\/$day\/$yy $hour\: +$min\:$sec was removed.\n"; push (@file_list, $filename); } } }, $pslog_directory); close LOGFILE; }
To accomplish what I need I was trying to do this:
if ( -f $filename && $filename =~ /^Log*/ )
Or I tried this:
if ( -f $filename && $filename =~ /^L/ )
The files in the directory that is searched look like this:
Log.txt.20151102 Log.txt.20151103 Fsvrcom1.trc Fsvrcom2.trc
I need to leave the files that don't start with "L" alone. Any help is greatly appreciated.

Replies are listed 'Best First'.
Re: File name search with regex
by NetWallah (Canon) on Dec 03, 2015 at 22:02 UTC
    You are deriving $filename from $File::Find::name.

    From the docs,
    $File::Find::name is the complete pathname to the file.

    So , it includes the directory path, which presumably does not start with "L", and will never match /^L/.

    Use $_ inside the wanted function, to get just the file name.

            Our business is run on trust. We trust you will pay in advance.

      Thank you NetWallah. I was looking for what was wrong and you pointed that out. I am new to using perl and using regex. I changed the regex to /Log.txt/ and all was well. I appreciate the other ideas from yourself and the others of how else to try to accomplish what I was doing. I will look in to them as there is always time later for improvements, but for now I am able to carry on with what I have. Thank you very much for the help. -Joleen
Re: File name search with regex
by RichardK (Parson) on Dec 03, 2015 at 23:38 UTC

    I find that File::Find::Rule is much easier to use than raw File::Find.

    So you could get just the files that you want to process something like this :-

    my @logs = File::Find::Rule->file()->name('L*')->mtime("<=$five_days_a +go")->in($dir);
Re: File name search with regex
by stevieb (Canon) on Dec 04, 2015 at 00:11 UTC

    Update: I misread the OP as NOT wanting L|Log files (I'm leaving my OP below as-is/original). RichardK's response here is the better one. He also taught me about the mtime method, so at least I learned something :) /update

    Hi oldenuf2no, welcome to the PerlMonks Monastery!

    File::Find::Rule will help clean up your code, the -M flag can do the check, and throw in a tiny regex to bypass files that start with L or Log and you end up with this:

    use warnings; use strict; use File::Find::Rule; my $dir = '.'; my @files = File::Find::Rule->file()->name('*')->in($dir); for (@files){ if (-M $_ > 5 && $_ !~ /^(?:L|Log)/){ print "$_\n"; } }

    -stevieb