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

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";