Hi Toolic!
The code is part of a larger program that uses two subroutines
that allow me to copy files based on time and
then match them with the dir that have the same id at the beginning of the id string.

#!/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"; # location of files to be processed. my $root="/Users/mgavi.brathwaite/Desktop/BioinfDev/SequenceAssemblyPr +oject/Sequencing_Results/RESULTS $year/New Version/Amanda Li/$month_a +bbr "."$year"; print "$root\n"; # location of maid directories my $komp_dir ="/Users/mgavi.brathwaite/Desktop/BioinfDev/SequenceAssem +blyProject/KOMP"; my %files(); find(\&wanted, $root); #check the arrays in the hash looking for maids with 4 files foreach my $k (keys(%files)){ #get the array from the array reference. #See how the reference gets in the has below. my @arr=@{$files{$k}}; #skip unless we have 4 files in the array next unless ( scalar my @array) == 4; #look for KOMP maid directory we want to copy to #glob dirs with a regex something like /$k\D/ - lets call it $KOMP_d +ir $komp_dir = maid_dirs($k); #Create the sequencing sub dir under $KOMP_dir #so now KOMP_dir=KOMP_dir+"/sequencing" mkdir $komp_dir."/sequencing",0755; $komp_dir=$komp_dir."/sequencing/"; #now iterate through the array, copying the files. You know the mai +d number ($k) foreach my $f (@arr){ copy("$f","$komp_dir/$f") or die "Copy failed: $!"; } #do_phred_phrap($komp_dir); } =for sub do_phred_phrap{ my $dir=shift; } =cut ################################ Subroutines ######################### +####### sub wanted { my $targetfile = $File::Find::name; return if $targetfile =~ /xls$/; # -M Script start time minus file modification time, in days. # skip unless mod date is less than one day ago return if (-M $targetfile > 1.0); #determine maid number (my $maid = $1) = $targetfile=~ /.*\/(\d*)/; my @arr=null; if ($files{$maid}){ @arr=@{$files{$maid}}; }else{ @arr=[]; } # IMPORTANT!!!! $targetfile should be the full path to the file s +o # you don’t have to figure out how to recreate it when copying push @arr, $targetfile; #set the array reference as the hash value. #You get the array back with my code above in the ‘if’ statement. + $files{$maid}=\@arr; } #you will pass in the maid number as an argument sub maid_dirs{ my $maid=shift; @dirs = glob("/Users/mgavi.brathwaite/Desktop/BioinfDev/Sequence +AssemblyProject/KOMP/*"); foreach $komp_dir(@dirs) { if (-d $komp_dir){ if ($komp_dir=~/.*\/$maid\D/){ return $komp_dir; } } } } print "\ndone\n";
Your wisdom is appreciated.

In reply to Re^2: Trouble copying files from an array to new dir by lomSpace
in thread Trouble copying files from an array to new dir by lomSpace

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.