$file_date = POSIX::strftime("%C%y%m%d", localtime); #get the current date to append to file name while (my ($filename, $recipient ) = each %csv_files) { my $newname = $file_date.$filename; archive_results($recipient, $filename, $newname, $subject); } exit 0; sub archive_results { my($to_email, $old_file, $new_file, $subject) = @_; #name subroutine variables if (-e $old_file) { #only if file exists system("mv $data_path/$old_file $data_path/$new_file"); #rename the flatfile with date $msg = MIME::Lite->new(From => $from_email, To => $to_email, Subject => $subject, Type => 'multipart/mixed'); $msg->attach(Type => 'application/octet-stream', Path => "$data_path/$new_file", Filename => $new_file ); $msg->send(); system("mv $data_path/$new_file $archive_path/$new_file"); #move flatfile to archive directory } } #end archive_results