in reply to Re: Trouble copying files from an array to new dir
in thread Trouble copying files from an array to new dir

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.