PERL:检查文件是否存在,通过电子邮件向用户发送报告不存在的标记,压缩文件并传输它们

时间:2014-06-03 02:42:43

标签: excel perl pdf zip putty

任何人都可以帮我创建一个包含两个文件的zip文件,一个是Excel,另一个是pdf。我这样做没有Archive :: zip,因为我无法安装它。我正在尝试浏览目录并获取两个文件excel和pdf然后压缩它们并发送一条错误消息或警告说它已被压缩。

       sub monthly_report_in {
        ### configure local variables
         $StatusP="false";
         $StatusX="false";
         local $id,$pw,$tpwd_id,$geek_pw,$reportm_date,$file_count,$geg_id,$geg_pw,$month_abbrv;
         local $org1="bfn";
         local $org2="geg";
         local $db_server_name=" ";
         local $home_dir=" ";
         local $archive_dir=" ";
         local $smb_server="";
         local $smb_folder="";
         local $smb_folder="";
         local $reportm_temp="";
         local $input_name2="RegistrationStatsexcel.xls";
         local $zip_input_file="RegistrationByCity*.*";

        ############################################################
        # clean up and create file names                           #
        # gets the date, month, and year.                          #
        #Then creates zipped file named montly.month.year.zip      #
        ############################################################
         get_reportm_date($reportm_temp,$reportm_date);
         get_month_abbrv($month_abbrv);
         get_year($year);
        local $file_folder_name="$db_server_name"."."."$reportm_date"."."monthly";
        local $sftp_dir="";
         local $zipped_file="monthly_statsreport.$month_abbrv"."$year".".zip";

        ##############################################################
        # configure email message content                            #
        # sends user the message that the file has been transferred  #
        #  or an error message that says there were no files         #
        ##############################################################
         local $send_mail="email addy";
        local $good_subject="$zipped_file file transferred to server: $DATE";
        local $good_message="$good_subject";
        local $error_messsage1="Error! No Monthly Reports Found:  $DATE";
        local $error_message1="No monthly reports were found.'\n\n' Contact The Help Desk.'\n\n'script name: $SCRIPT";
         local $smb_subject=" $zipped_file transfered to server ";
        local $smb_message="$zipped_input_file transferred to server $smb_folder\n";
        local $zero_subject = "Monthly Stats files are 0 bytes: $DATE";
         local $zero_message = "Monthly report Stats files are 0 bytes in size.\n\n Please Contact the Help Desk .";

        #########################################################
        #creates new directory and changes to new directory     #
        #get the ID and password for the organization           #
        #########################################################
          mkdir($current_dir);
         chdir($current_dir);
         get_id_and_pw($org1,$id,$pw);

        ##########################################################
        #smb transfer and archive pdf & excel file               #
        ##########################################################
         checkif_fileexists($current_dir,$pdf_ext,$StatusP,$error_message,$good_message);
         checkif_fileexists($current_dir,$xls_ext,$StatusX,$error_message,$good_message);
        print "---$StatusP---\n";
        if (($StatusP =~ "false") && ($StatusX =~ "false")) {
            good_mail($error_message,$error_subject1,$send_mail);
               }
               elseif (($StatusP =~ "zero") && ($StatusX =~ "true")) {
                  good_mail($good_message,$good_subject,$send_mail);
               do_zip_files($current_dir,$zip_input_file);
               get_id_and_pw($org2,$geg_id,$geg_pw,$smb_server,$smb_folder,$input_file);
                 smb_put($current_dir,$geg_id,$geg_pw,$smb_server,$smb_folder,$zip_folder,$zip_input_file);
               do_move($current_dir,$zip_input_file,$archive_dir,$zip_input_file);
                  good_mail($smb_message,$smb_subject,$send_mail);
                  }
                     else {
               ### if pdf file exists, this will transfer the file and notify users
                  if ($StatusP =~ "true") {
                  do_zip_files($org2,$geg_id,$geg_pw,$smb_server,$smb_folder,$input_folder);
                  local $error_subject_pdf="$input_file2 report not found: $DATE";
                  local $error_subject_message1a="monthly report $input_file2 was not found today. 'n\n\'Compressed $input_file has been transferrd to $smb_server $smb_folder.'n\n\' Please Contact
                  The Help  check $SCRIPT";
               good_mail($error_message1a,$error_subject1a,$send_mail);
              }
             elseif ($StatusP =! "true");{
             do_zip_files($current_dir,$pdf_ext,$input_file2);
             get_id_and_pw($org2,$geg_id,$geg_id,$geg_pw);
             smb_put($current_dir,$geg_id,$geg_pw,$smb_server,$smb_folder,$input_file2);
                    do_move($current_dir,$zip_input_file,$archive_dir,$input_file2);
                    local $error_subject2="$input_file1 report not found: $DATE";
                 local $error_message2="Monthly report $input_file1 was not found today. 'n\n\' Please Contact The Help Desk $SCRIPT";
                         good_mail($error_message1,$error_subject1,$send_mail);
                         }
                        }
                    chdir($home_dir);
                        ###
                        }

1 个答案:

答案 0 :(得分:1)

这不是你问题的真正答案,但它对于评论来说太大了,应该帮助你找到解决方案。

我所能做的就是提供更好格式化的子程序版本。我希望你能看到阅读有多容易吗?

正如我在评论中所说,local几乎不是正确的用法。您还应该编写非常小的代码段并在编写更多内容时进行全面测试。如果你创建一个完整的子程序,那么它几乎肯定是错误的。

再一次,不要只提交我写的内容。我不知道它是否是正确的,它仍然是一个非常懒惰的编程。你应该首先确保你的子程序被调用,只需要

sub monthly_report_in {
  print "entered 'monthly_report_in'\n";
}

并从那里逐步添加功能。

这是重新格式化。请怀疑地对待

sub monthly_report_in {

  ### Configure local variables

  $StatusP = 'false';
  $StatusX = 'false';

  my ($id, $pw, $tpwd_id, $geek_pw, $reportm_date, $file_count, $geg_id, $geg_pw, $month_abbrv);
  my ($org1, $org2)  = qw/ bfn geg /;
  my $db_server_name = ' ';
  my $home_dir       = ' ';
  my $archive_dir    = ' ';
  my $smb_server     = '';
  my $smb_folder     = '';
  my $smb_folder     = '';
  my $reportm_temp   = '';
  my $input_name2    = 'RegistrationStatsexcel.xls';
  my $zip_input_file = 'RegistrationByCity*.*';

  ############################################################
  # clean up and create file names                           #
  # gets the date, month, and year.                          #
  # Then creates zipped file named montly.month.year.zip      #
  ############################################################

  get_reportm_date($reportm_temp, $reportm_date);
  get_month_abbrv($month_abbrv);
  get_year($year);

  my $file_folder_name = "${db_server_name}.${reportm_date}.monthly";
  my $sftp_dir         = '';
  my $zipped_file      = "monthly_statsreport.${month_abbrv}${year}.zip";

  ##############################################################
  # configure email message content                            #
  # sends user the message that the file has been transferred  #
  # or an error message that says there were no files         #
  ##############################################################

  my $send_mail        = 'email addy';
  my $good_subject     = "$zipped_file file transferred to server: $DATE";
  my $good_message     = $good_subject;
  my $error_messsage1  = "Error! No Monthly Reports Found: $DATE";
  my $error_message1   = "No monthly reports were found.\n\nContact The Help Desk.\n\nScript name: $SCRIPT";
  my $smb_subject      = "${zipped_file} transfered to server ";
  my $smb_message      = "${zipped_input_file} transferred to server ${smb_folder}\n";
  my $zero_subject     = "Monthly Stats files are 0 bytes: $DATE";
  my $zero_message     = "Monthly report Stats files are 0 bytes in size.\n\nPlease Contact the Help Desk.";

  #########################################################
  # creates new directory and changes to new directory     #
  # get the ID and password for the organization           #
  #########################################################

  mkdir($current_dir);
  chdir($current_dir);
  get_id_and_pw($org1, $id, $pw);

  ##########################################################
  # smb transfer and archive pdf & excel file               #
  ##########################################################

  checkif_fileexists($current_dir, $pdf_ext, $StatusP, $error_message, $good_message);
  checkif_fileexists($current_dir, $xls_ext, $StatusX, $error_message, $good_message);
  print "---${StatusP}---\n";
  if ( $StatusP eq 'false' and $StatusX eq 'false') {
    good_mail($error_message, $error_subject1, $send_mail);
  }
  elsif ($StatusP eq 'zero' and $StatusX eq 'true') {
    good_mail($good_message, $good_subject, $send_mail);
    do_zip_files($current_dir, $zip_input_file);
    get_id_and_pw($org2, $geg_id, $geg_pw, $smb_server, $smb_folder, $input_file);
    smb_put($current_dir, $geg_id, $geg_pw, $smb_server, $smb_folder, $zip_folder, $zip_input_file);
    do_move($current_dir, $zip_input_file, $archive_dir, $zip_input_file);
    good_mail($smb_message, $smb_subject, $send_mail);
  }
  else {
    ### If the PDF file exists, this will transfer the file and notify users
    if ($StatusP eq 'true') {

      do_zip_files($org2, $geg_id, $geg_pw, $smb_server, $smb_folder, $input_folder);

      my $error_subject_pdf       = "$input_file2 report not found: $DATE";
      my $error_subject_message1a = "monthly report ${input_file2} was not found today.n\n\Compressed ${input_file} has been transferred to ${smb_server} ${smb_folder}.n\nPlease Contact
The Help check $SCRIPT";
      good_mail($error_message1a, $error_subject1a, $send_mail);
    }
    elsif ($StatusP ne 'true') {
      do_zip_files($current_dir, $pdf_ext, $input_file2);
      get_id_and_pw($org2, $geg_id, $geg_id, $geg_pw);
      smb_put(
        $current_dir, $geg_id,     $geg_pw,
        $smb_server,  $smb_folder, $input_file2
      );
      do_move($current_dir, $zip_input_file, $archive_dir, $input_file2);
      my $error_subject2 = "${input_file1} report not found: $DATE";
      my $error_message2 = "Monthly report ${input_file1} was not found today.n\nPlease Contact The Help Desk $SCRIPT";
      good_mail($error_message1, $error_subject1, $send_mail);
    }
  }

  chdir $home_dir;
}