#!/usr/bin/perl use strict; use warnings; use File::Find; use File::Copy; my %dir = ( archive => '/path/to/archive', work => '/path/to/work', error => '/path/to/error', another => '/path/to/another', ); my $startDate = 'Jan1'; my $endDate = 'Jan31'; my @list_of_filenames = qw( a b c d other ); my $archive_target = $startDate . '_' . $endDate; do_stuff_on_my_array($archive_target, \@list_of_filenames); sub do_stuff_on_my_array { my ($archive_target, $aref) = @_; my $found_all = 1; foreach my $file (@$aref) { $found_all=0 unless check_if_file_exists_in_dir( $file, $dir{'archive'} . '/' . $archive_target); } if( $found_all ) { foreach my $file (@$aref) { my $orig = $dir{'archive'} . '/' . $file; my $target = $dir{'work'} . '/' . $file; cp( $orig, $target ); ### my_ftp( $target ); unlink $target; } } else { my $found_path_for_file = search_for_files_in_dir( $dir{'another'}, $aref); #what do we do with these files we found? FILE: foreach my $file (@$aref) { my $fullpath = $found_path_for_file->{$file} ; unless( $fullpath) { warn "file was not found[$file]"; next FILE; } cp ($fullpath, $dir{'work'}); } } } sub check_if_file_exists_in_dir { my( $file, $dir ) = @_; return ( -d $dir && -e "$file/$dir") ? 1 : 0 ; } sub search_for_files_in_dir { my($dir, $aref)=@_; my $files = map{ $_ => 1} @$aref; my $file_path; # build a hash with the names I'm looking for my $wanted = sub { if ( exists $files->{$_} ) { $file_path->{ $_ } = $File::Find::name ; return 1; } else { return 0; } }; if ( %{$file_path} != %{$files} ) { warn("some files could not be found!"); } return $file_path; }