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 Linuxer!
This code should explain what I am doing.

#!/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";

Replies are listed 'Best First'.
Re^3: Trouble copying files from an array to new dir
by linuxer (Curate) on Feb 11, 2009 at 21:45 UTC

    OK, that's the code. But what do you expect from us now?

    Did you check, what toolic and me already suggested?

    Do you still have problems? What kind of problems? What happens when you execute the script? What do you want to happen?

    At least, my %files(); causes an syntax error.

    You still don't check whether mkdir is successful or not.

    I just rewrote your script to get it syntactically OK. I think there is more optimization possible.
    I don't know, if it does what you want, but you might get the idea, what I meant with my previous comments.

    #! /usr/bin/perl use strict; use warnings; use DateTime; use File::Copy; use File::Find; use File::stat; use Time::localtime; use File::Spec::Functions qw( catdir catfile ); my $now = DateTime->now( time_zone => 'floating' ); my $yesterday = $now->substract( days => 1 ); my $year = $yesterday->year; my $month = $yesterday->month; my $month_abbr = uc( $yesterday->month_abbr ); 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 $basedir = '/Users/mgavi.brathwaite/Desktop/BioinfDev/SequenceAssem +blyProject'; my $root = catdir( $basedir, "Sequencing_Results/Results $year/New + Version/Amanda Li/$month_abbr" ); my $komp_dir = catdir( $basedir, 'KOMP' ); #> sub routines #> ------------------------------------------------------------ sub wanted { # return if current filename ends with 'xls' return if $File::Find::name =~ m/xls\z/; # return if file is younger than 1 day return if -M $File::Find::name > 1.0; # determine maid number if ( my ( $maid ) = $File::Find::name =~ m{/(\d*)} ) { # store full path to file if maid number could be determined; +see: "perldoc perldsc", Hashes of Arrays push @{ $files{$maid} }, $File::Find::name; } } sub maid_dirs { my ( $maid ) = @_; my @dirs = glob( catfile( $komp_dir, '*' ) ); for my $dir ( @dirs ) { return $dir if -d $dir and $dir =~ m{/$maid\D}; } # if no suitable dir was found return undef; } #> main script #> ------------------------------------------------------------ # determine files my %files; find( \&wanted, $root ); for my $maid ( keys %files ) { my @files = @{ $files{$maid} }; next if @files != 4; if ( my $dest_dir = maid_dirs( $maid ) ) { my $directory = catdir( $dest_dir, 'sequencing' ); mkdir( $directory, 0755 ) or die "mkdir failed: $!\n"; for my $file ( @files ) { copy( $file, catfile( $directory, $file ) ) or die "copy f +ailed: $!\n"; } } } print "Job done\n";