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

Hello,
I need to cp the return values to the "$seqdir"
dir, but nothing is happening. Any ideas?

#!/usr/bin/perl use strict; use DateTime; use File::Copy; use File::Find; use File::stat; use Time::localtime; my $now = DateTime->now(time_zone => 'floating'); my $yesterday = $now->subtract(days=>1); my $year = $yesterday->year; my $month = $yesterday->month; my $month_abbr = uc($yesterday->month_abbr);# uppercase string chars my $day = $yesterday->day; my $year_abbr = substr "$year",2,2; my $mdy = $now->strftime("%m-%d-%y"); print "$year\t$year_abbr\t$month\t$month_abbr\t$day\n"; my $root="/Users/mgavi.brathwaite/Desktop/BioinfDev/SequenceAssemblyPr +oject/Sequencing_Results/RESULTS $year/New Version/Amanda Li/$month_a +bbr "."$year"; print "$root\n"; my $seqdir = "/Users/mgavi.brathwaite/Desktop/BioinfDev/SequenceAssemb +lyProject/testdir1"; mkdir $seqdir, 0755; # I want to use File::stat to determine age of dir # to determine if it is recent. This is a filtering # process to determine which files to process my $seq_files = find(\&wanted, $root); copy("$seq_files","$seqdir") or die "File cannot be copied."; sub wanted { my $targetfile = $File::Find::name; next if $targetfile =~ /xls$/; my $mod_time = (-M $targetfile < 1); print "Found it $targetfile\n" if /^(\d+\D\d)_(LacZ|pgK|SD|SU)/; # skip unless mod date is less than one day ago return if ( (-M $targetfile < 1) && (/^(\d+\D\d)_(LacZ|pgK|SD|SU) +/) ); } print "done\n";

Replies are listed 'Best First'.
Re: Trouble filtering files based on date
by ikegami (Patriarch) on Jan 30, 2009 at 16:46 UTC

    -M is a weirdly named function whose sole argument is a file name or a file handle.

    my $mod_time = (-M < 3) $targetfile;
    should be
    my $mod_time = (-M $targetfile) < 3;

      Thanks!
Re: Trouble filtering files based on date
by moritz (Cardinal) on Jan 30, 2009 at 16:48 UTC
    my $mod_time = (-M < 3) $targetfile;

    You probably meant my $mod_time = (-M $targetfile) < 3; instead.

Re: Trouble filtering files based on date
by hbm (Hermit) on Jan 30, 2009 at 17:49 UTC

    lomSpace, here's a cleaner version of your approach:

    use strict; use warnings; use File::stat; use File::Find; Use File::Copy; use Time::localtime; find sub { if (/^\d+\D\d_(?:LacZ|pgK|SD|SU)/ && -M > 1) { print "$_\n", "ctime: " . ctime(stat($_)->ctime), "\n", "mtime: " . ctime(stat($_)->mtime), "\n"; # copy("$_","$seqdir"); # copy file to new directory } }, $root;

    Update: I meant to add warnings, which caught an error in your original post...

      Hi!
      All of the files are being cp. I only need the
      most recent files. When I use

      if (/^\d+\D\d_(?:LacZ|pgK|SD|SU)/ && -M < 1)

      I get the error message:
      Warning: Use of "-M" without parentheses is ambiguous at find.pl line 23.
      Thanks, It's slick!
Re: Trouble filtering files based on date
by Illuminatus (Curate) on Jan 30, 2009 at 17:24 UTC
    Assuming you are on *nix, why would you not use find command along with awk:
    ./foo.pm`find . -mtime 0 -print | awk 'BEGIN{line=""};{line=line " +" $0};END {print line }'`
    Your code would have to remove "." and '..', but this is trivial.