Pages

Monday, March 3, 2014

Perl by Example. Read and write to file. call to Windows System commands. Work with Array, HashTable, and more.

Description.
This is an example of running perl script from batch.
The Main Logic for this process, is on monthly basis, per each customer, zip the files generated during the last month, and move them to a target folder.


The flow:
1. 
folders.ini
   Manually populate folders.ini file with:
- Set of general parameters.
- Set of entries per customer.
- Each customer has an unique name, a list of folders, and file match criteria.

2. monthly_zipper_main.bat
    Is wrapping the call to monthly_zipper_main.pl.

3. monthly_zipper_main.pl
      Handling the main flow of events:
   - Set the TIMESTAMP for log files.
   - Open log file for write. 
   - Read folders.ini file.
   - Populate HashTable with entries from folders.ini.
   - Populate Array with customer ID.

   - Loop on the Customers Array, and per each entry call to handle_folder.pl.
   - Call to sendMail.bat

4.  handle_folder.pl
     Handle Logic for each customer. 
   - Loop on all files in customer source folder that match the matching criteria.
   - Move the files to customer target folder.
   - Zip files.
   - Delete the files, after zip completes.

5. sendMail.bat 
   - Is using standard Windows blat mailing utility to reports status of the process and attach the log file.


The code

monthly_zipper.ini
ZIP_CMD=C:\Program Files\7-Zip\7z.exe
ZIP_FILE_NAME=PROC_%CUSTOMER%_%DATETIME%.zip
LOG_FILE_PATH=D:\PROC_FILES\Logs
LOG_FILE_NAME=PROC_Files_Monthly
#SEND_MAIL_SEVERITY=ALL/ERROR - Set this to ERROR to receive only error notifications.
SEND_MAIL_SEVERITY=ALL
#KEEP_FILES_IND=Y/N - set this to Y to keep original files
KEEP_FILES_IND=Y
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#IMPORTANT!!! - CUSTOMER_99 MUST BE UNIQUE!!!
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#================================================================

#CUST_A output
CUSTOMER_01=CUST_A_OUTPUT
#Get files that match CD*
CUSTOMER_01_FILES=^CD{1}[a-zA-Z0-9]*
CUSTOMER_01_SOURCE_FOLDER="D:\PROC_FILES\Files\workarea\CUST_A\output"
CUSTOMER_01_TARGET_FOLDER="D:\PROC_FILES\Files\archive\CUST_A\output"

#CUST_A processed
CUSTOMER_02=CUST_A_SUCCESS
#get Files that match CD*.success
CUSTOMER_02_FILES=^CD{1}[a-zA-Z0-9]*.success$
CUSTOMER_02_SOURCE_FOLDER="D:\PROC_FILES\Files\workarea\CUST_A\processed"
CUSTOMER_02_TARGET_FOLDER="D:\PROC_FILES\Files\archive\CUST_A\processed"
#================================================================#CUST_B output
CUSTOMER_03=CUST_B_OUTPUT
#Get files that match CD*
CUSTOMER_03_FILES=^CD{1}[a-zA-Z0-9]*
CUSTOMER_03_SOURCE_FOLDER="D:\PROC_FILES\Files\workarea\CUST_B\output"
CUSTOMER_03_TARGET_FOLDER="D:\PROC_FILES\Files\archive\CUST_B\output"

#CUST_B processed
CUSTOMER_04=CUST_B_SUCCESS
#get Files that match CD*.success
CUSTOMER_04_FILES=^CD{1}[a-zA-Z0-9]*.success$
CUSTOMER_04_SOURCE_FOLDER="D:\PROC_FILES\Files\workarea\CUST_B\processed"
CUSTOMER_04_TARGET_FOLDER="D:\PROC_FILES\Files\archive\CUST_B\processed"
#================================================================

monthly_zipper_main.bat
perl monthly_zipper_main.pl

monthly_zipper_main.pl

#! /usr/bin/perl

use DBI;
use Time::gmtime;
use File::Copy;
use DateTime;
use Path::Class;

use strict;
use warnings;

#===================
#Global Parameters
#===================
my %hashTable;
my %hashTableCustomers;
my $INI_FILE='monthly_zipper.ini';
my $display_delimiter="========================================\n";

my $run_date='';
my $yearly_folder="";
my $monthly_folder="";
my $yearly_and_monthly_folder="";
my $timestamp="";
my $logFile="";

my $CUSTOMER_NAME="";
my $CUSTOMER_FOLDER="";
my $FILE_FILTER="";
my $TARGET_FOLDER="";
my $CST_ZIP_FILE="";
my $ZIP_CMD="";
my $LOG_FILE_PATH="";
my $LOG_FILE_NAME="";
my $KEEP_FILES_IND="";
my $SEND_MAIL_SEVERITY="";

my $curr_cust="";
my @customers_array;
#==================================
# The code starts here
#==================================

   my $result=main();
   exit $result;


#==================================
# sub main
#==================================
sub main{   
   no warnings 'uninitialized';
   
   setRundate();
   
   readIniFile("$INI_FILE");
   
   setGeneralParams("ZIP_CMD", "LOG_FILE_PATH","LOG_FILE_NAME","KEEP_FILES_IND","SEND_MAIL_SEVERITY");
   $result=setLogFileName();
   handleStatus($result, "setLogFileName",undef);
      
   open my $LOG_HANDLER, '>', $logFile or die ("Could not open log file: ".$logFile);

   reportStartOfProcess($LOG_HANDLER);   
   reportGeneralParameters($LOG_HANDLER); 

   
   foreach (@customers_array){
     $curr_cust = $_;
#print "Current Customer: ".$curr_cust."\n";
     $result=setCustomerParams($curr_cust, $curr_cust."_SOURCE_FOLDER", $curr_cust."_TARGET_FOLDER",$curr_cust."_FILES", $LOG_HANDLER); 
#print "1 In Loop the result is = ".$result."\n";
     handleStatus($result, "setCustomerParams() for Customer: ".$curr_cust,$LOG_HANDLER);
if ($result != 0){
  last;
}
     close $LOG_HANDLER;   
     $result=handleCustomer($LOG_HANDLER);
     open $LOG_HANDLER, '>>', $logFile or die ("Could not open log file: ".$logFile);
     $result=handleStatus($result, "handleCustomer() for Customer: ".$curr_cust,$LOG_HANDLER);
#print " In Loop the result is = ".$result."\n";
if ($result != 0){
  last;
}
   }
   
   reportEndOfProcess($LOG_HANDLER, $result);   
   close $LOG_HANDLER;
   sendMail($result);
}   
#==================================
# subs
#==================================

sub setRundate{
  my $dateTime=DateTime->now;

  my $result;

  my $month=$dateTime->month;
  my $year=$dateTime->year;
  
  if ($month < 10){ $month="0".$month ; }
  my $day=$dateTime->day;
  if ($day < 10){ $day="0".$day ; }
  my $hh=$dateTime->hour;
  if ($hh < 10){ $hh="0".$hh ; }
  my $mm=$dateTime->minute;
  if ($mm < 10){ $mm="0".$mm ; }
  my $ss=$dateTime->second;
  if ($ss == 60){ $ss=0; };
  if ($ss == 61){ $ss=1; };
  if ($ss < 10){ $ss="0".$mm ; }

  $run_date=$dateTime->year.$month.$day;
  
  if ($month > 1){
$monthly_folder=$year.$month-1;
$yearly_folder=$year;
  }else{
    $monthly_folder=$year-1.12;
$yearly_folder=$year-1;
  }
  $timestamp=$run_date."_".$hh.$mm.$ss;
  $yearly_and_monthly_folder=$yearly_folder."\\".$monthly_folder;
}

sub setLogFileName{
  $logFile=$LOG_FILE_PATH."\\".$LOG_FILE_NAME."_".$timestamp.".log";
  my $status=0;
  #print "Will try to create now new folder: ".$LOG_FILE_PATH;
  if (!-d "$LOG_FILE_PATH" ){
my $cmd = "mkdir "."\"".$LOG_FILE_PATH."\"";
$status=system($cmd);
  }
  if ($status == 0 ){  
    print "\n";
    print $display_delimiter;
    print "\n";
    print "All Events are qritten to Log: ".$logFile."\n";
    print "\n";
    print $display_delimiter;
    print "\n";
  }
  $status;
}


sub reportStartOfProcess{
    my $log_handler = shift;

printMultipath ("\n",$log_handler);
    printMultipath ($display_delimiter,$log_handler);
printMultipath ("Monthly Files Backup Process Started at: ".$timestamp."\n",$log_handler);
printMultipath ($display_delimiter,$log_handler);
printMultipath ("\n",$log_handler);
}


sub readIniFile{
  my $i_ini_file=shift;
  
  my $line='';
  my $key='';
  my $value='';    
  my $match_str = "CUSTOMER_[0-9][0-9]"; 
  
  #read from ini file
  open(FILE_HANDLER, "$i_ini_file") or die ("Could not open ini file: "."$i_ini_file"."\n");
  my $count=0;
  foreach $line (<FILE_HANDLER>){
    chomp($line);
    if ($line =~ /^$/) { 
#nothing. Ignore empty lines
    }elsif($line =~ /^#/) { 
#nothing. Ignore # comment lines lines
}else{
 ($key, $value)=split(/=/,$line);
  $hashTable{$key} = $value;
  
  #print "Trying to match ".$match_str." to ".$key."\n";
  if ($key =~ /^$match_str$/){
    push (@customers_array,$key);
  }
   }
 }
 close (FILE_HANDLER);
}

#set general parameters
sub setGeneralParams{

   #my $file_filter = $_[0];
   my $zip_cmd = shift;
   my $log_file_path = shift;
   my $log_file_name = shift;
   my $keep_files_ind = shift;
   my $send_mail_severity = shift;
   
   #$FILE_FILTER=$hashTable{"$file_filter"};
   $ZIP_CMD = $hashTable{"$zip_cmd"}; 
   $LOG_FILE_PATH = $hashTable{"$log_file_path"};
   $LOG_FILE_NAME = $hashTable{"$log_file_name"};
   $KEEP_FILES_IND = $hashTable{"$keep_files_ind"};
   $SEND_MAIL_SEVERITY = $hashTable{"$send_mail_severity"};   
   
   print $display_delimiter;
   print "Running With General Parameters:"."\n";
   print "ZIP_CMD: ".$ZIP_CMD."\n";
   print "LOG_FILE_PATH: ".$LOG_FILE_PATH."\n";   
   print "LOG_FILE_NAME: ".$LOG_FILE_NAME."\n";
   print "KEEP_FILES_IND: ".$KEEP_FILES_IND."\n";
   print "SEND_MAIL_SEVERITY: ".$SEND_MAIL_SEVERITY."\n";
   print $display_delimiter;
}

sub setCustomerParams{
   #no warnings 'uninitialized';
   my $customer_name = shift;
   my $source_folder = shift;
   my $target_folder = shift;
   my $file_filter = shift;
   my $log_handler = shift;
   #print "setCustomerParams: "."\n"; print $customer_name."\n";   print $source_folder."\n";   print $target_folder."\n";   print $file_filter."\n";
   
   $CUSTOMER_NAME=$hashTable{$customer_name};
   #print "CUSTOMER_NAME = ".$CUSTOMER_NAME."\n";
   if (!defined $CUSTOMER_NAME){     
     printMultipath("Error in setCustomerParams() for Customer: ".$customer_name."\n", $log_handler);
     printMultipath("Customer not found in monthly_zipper.ini file. "."\n", $log_handler);
return 1;
   }
   
   #print "Looking by ".$customer_name."\n";
   #print "Found "."$CUSTOMER_NAME"."\n";
   
   $CUSTOMER_FOLDER = $hashTable{$source_folder}; 
   #print "Looking by ".$source_folder."\n";
   #print "Found "."$CUSTOMER_FOLDER"."\n";
   $TARGET_FOLDER = $hashTable{"$target_folder"};
   #print "Found "."$CUSTOMER_FOLDER"."\n";
   $TARGET_FOLDER = $TARGET_FOLDER."\\".$yearly_and_monthly_folder;
   $FILE_FILTER = $hashTable{"$file_filter"};
   $CST_ZIP_FILE=$hashTable{"ZIP_FILE_NAME"};
   #print "\n\nCST_ZIP_FILE before = $CST_ZIP_FILE \n";
   $CST_ZIP_FILE =~ s/%CUSTOMER%/$CUSTOMER_NAME/g;
   $CST_ZIP_FILE =~ s/%DATETIME%/$timestamp/g;
   #print "CST_ZIP_FILE AFTER = $CST_ZIP_FILE \n";
   return 0;   
}

sub reportGeneralParameters{
  my $log_handler = shift;
  
  printMultipath ("\n",$log_handler);
  printMultipath ($display_delimiter,$log_handler);
  printMultipath ("Input Parameters: \n",$log_handler);
  printMultipath ("Run Date:".$run_date."\n",$log_handler);
  printMultipath ("Log File: ".$logFile."\n",$log_handler);
  printMultipath ("Keep Files: ".$KEEP_FILES_IND."\n",$log_handler);
  
  printMultipath ($display_delimiter,$log_handler);
  printMultipath ("\n",$log_handler);
}

sub reportEndOfProcess{
    my $log_handler = shift;
    my $status = shift;

printMultipath ("\n",$log_handler);
    printMultipath ($display_delimiter,$log_handler);
if ($status == 0){
printMultipath ("Monthly Files Backup Process has Finished Successfuly"."\n",$log_handler);

}else{
printMultipath ("Error!!!! Monthly Files Backup Process has Terminated with Error."."\n",$log_handler);
}
printMultipath ($display_delimiter,$log_handler);
printMultipath ("\n",$log_handler);
printMultipath ($display_delimiter,$log_handler);
printMultipath ("\n",$log_handler);

$status;
}

sub handleCustomer{
        #reportCallingParameters();

        my $log_handler = shift;
$result=system($^X,"handle_folder.pl", 
$run_date, 
$monthly_folder,
$CUSTOMER_NAME,
$CUSTOMER_FOLDER, 
$FILE_FILTER, 
$KEEP_FILES_IND,
$TARGET_FOLDER, 
$ZIP_CMD,
$CST_ZIP_FILE,
$logFile);
        #print "Exit point after call to handle_folder.pl(). Exist status is: ".$result."\n";
$result;
}

sub handleStatus{
  my $status = shift;
  my $method = shift;
  my $log_handler = shift;
  
  if ($status != 0){
    if (defined $log_handler){
printMultipath ("Error in Routine: ".$method." Status: ".$status."\n",$log_handler);
    }else{
print ("Error in Routine: ".$method." Status: ".$status."\n");
    }
    return $status;
  }
  $status;
}

sub sendMail{
  my $status=shift;
  my @mailArg = "";
  my $sendMailInd=0;
  if ($status != 0){
    @mailArg=("sendMail.bat","MonthlyZip_Error.txt",$logFile);
    system(@mailArg);
  }else{
    if ($SEND_MAIL_SEVERITY eq "ALL"){
    @mailArg=("sendMail.bat","MonthlyZip_Success.txt",$logFile);
    system(@mailArg);
}
  }
}

sub printMultipath{
  my $str = shift;
  my $log_handler = shift;
    print $log_handler ($str);
print ($str);  
}

sub reportCallingParameters{
        print "Calling handleCustomer() with Parameters:"."\n";
print "run_date = ".$run_date."\n";
print "monthly_folder = ".$monthly_folder."\n";
print "CUSTOMER_NAME = ".$CUSTOMER_NAME."\n";
print "CUSTOMER_FOLDER = ".$CUSTOMER_FOLDER."\n";
print "FILE_FILTER = ".$FILE_FILTER."\n";
print "KEEP_FILES_IND = ".$KEEP_FILES_IND."\n";
print "TARGET_FOLDER = ".$TARGET_FOLDER."\n";
print "ZIP_CMD = ".$ZIP_CMD."\n";
print "CST_ZIP_FILE = ".$CST_ZIP_FILE."\n";
print "logFile = ".$logFile."\n";
}

handle_folder.pl

use File::Copy;
use File::stat;
use Win32::API;
use Win32::DriveInfo;
use Cwd;
use POSIX qw(strftime);

#================================
# Input Parameters
#================================
my $i_run_date = shift;
my $i_monthly_folder = shift;
my $i_customer_name= shift;
my $i_active_folder = shift;
my $i_file_filter = shift;
my $keep_files_ind = shift;
my $i_target_folder = shift;
my $i_zip_cmd = shift;
my $i_zip_file_name = shift;
my $logFile = shift;

#================================
# Global Variables
#================================
my $filesize = 0;
my $total_file_size=0;
my $space_protection=4;

my $ACTION_GET_SPACE="GET_SPACE";
my $ACTION_MOVE_FILES="MOVE_FILES";
my $ACTION_DELETE_FILES="DELETE_FILES";
my $display_delimiter="========================================\n";
my @filesList;
my $status=0;

#====================================================
# Code Start Here
#====================================================
  
  #print "Inside handle_folder.pl"."\n";
  #reportInputParams();
  
  open my $LOG_HANDLER, '>>', $logFile or die ("Could not open log file: ".$logFile);
  reportCustomerStart($i_customer_name, "$i_active_folder", "$i_target_folder","$i_zip_file_name", $LOG_HANDLER);
  $status=createFolder("$i_target_folder", $LOG_HANDLER);
  handleStatus($status, "createFolder", $LOG_HANDLER);
    
  #print ("\nMoving ".$i_file_filter." Files"."\n"."From ".$i_active_folder."\n"."To   ".$i_target_folder."\n");
     
  $total_file_size=handleFiles("$ACTION_GET_SPACE", "$i_active_folder", "$i_file_filter", undef,$LOG_HANDLER);
  $status=checkFreeSpace($total_file_size, $LOG_HANDLER);
  handleStatus($status, "checkFreeSpace", $LOG_HANDLER);
  
  $status=handleFiles($ACTION_MOVE_FILES,"$i_active_folder", "$i_file_filter", "$i_target_folder",$LOG_HANDLER);
  handleStatus($status, "moveFiles", $LOG_HANDLER);
  
  $status=deleteFiles($keep_files_ind, $LOG_HANDLER);  
  handleStatus($status, "deleteFiles", $LOG_HANDLER);
  
  reportCustomerEnd($i_customer_name, "$i_active_folder", "$i_target_folder","$i_zip_file_name",$LOG_HANDLER);
  close $LOG_HANDLER;
  exit 0;

#=================================
# Start subs
#=================================
sub checkFreeSpace{
  my $total_file_size=shift;
  my $log_handler=shift;
  
  my $free_space = Win32::DriveInfo::DriveSpace('c');  
  $total_file_size *= $space_protection;  
  if ($total_file_size > $free_space){  
    printMultipath ("Error!!! NO SPACE ON DEVICE!!!!\n",$log_handler);
printMultipath ("Requested Space:  $total_file_size*2 \n",$log_handler);
printMultipath ("Free Space:  $free_space \n",$log_handler);
1;
  }else{
    0;
  }
}


sub reportCustomerStart{

  my $i_customer_name=shift;
  my $i_active_folder=shift;
  my $i_target_folder=shift; 
  my $i_zip_file_name=shift; 
  my $log_handler=shift;
  
  printMultipath ("\n",$log_handler);
  printMultipath ($display_delimiter,$log_handler);
  printMultipath ("Starting Handle Customer: ".$i_customer_name."\n",$log_handler);
  printMultipath ("\n",$log_handler);
  printMultipath ("  Files Folder: ".$i_active_folder."\n",$log_handler);
  printMultipath ("  Backup Folder:    ".$i_target_folder."\n",$log_handler);
  printMultipath ("  Zip file name:    ".$i_zip_file_name."\n",$log_handler);
  printMultipath ($display_delimiter,$log_handler);
  printMultipath ("\n",$log_handler);
}


sub reportCustomerEnd{

  my $i_customer_name=shift;
  my $i_active_folder=shift;
  my $i_target_folder=shift;
  my $i_zip_file_name=shift;
  my $log_handler=shift;

  printMultipath ($display_delimiter,$log_handler);
  printMultipath ("\n",$log_handler);  
  printMultipath ("Finished Handle Customer.........: ".$i_customer_name."\n",$log_handler);
  printMultipath ("Status...........................: Success"."\n",$log_handler);
  printMultipath ("\n",$log_handler);
  printMultipath ($display_delimiter,$log_handler);
  printMultipath ("\n",$log_handler);
}



sub handleFiles{

    my $action=shift;
my $folder=shift;
my $filter=shift;
    my $targer_folder=shift;
    my $log_handler=shift;

    my $total_file_size=0;
my $file_counter=0;
my $status=0;

opendir (DIR_HANDLER, $folder) or die ("ERROR: Could not open folder: $folder"."\n");

    while (my $file = readdir(DIR_HANDLER)) {
 # We only want files
 next unless (-f "$folder/$file");
 
 # Use a regular expression to find files ending in $filter  
 #print "Current Folder\File: "."$folder/$file"."\n";
 #print "Current File: "."$file"."\n"; 
 #print "$file =~ /$filter/) = ".($file =~ /$filter$/)."\n";
 #if ($file =~ m/$filter/){
 #  print $file." MATCH ".$filter."\n";
 #}
   
 if ($file =~ m/$filter/){    
    my $file_yyyymm=getTimeStamp("$folder/$file");                              #We need files that were created during last month
    #print ($file_yyyymm." eq ".$i_monthly_folder."\n");
    if ($file_yyyymm eq $i_monthly_folder){
      #Just calculate needed space
      if ($action eq $ACTION_GET_SPACE){
  #print("Get size for file $file\n");
  $filesize = stat("$folder/$file")->size;
  $total_file_size+=$filesize;
  $file_counter+=1;
      }  
      #move file to YYYYMM flder and zip
      elsif ($action eq $ACTION_MOVE_FILES){
  #print("\nProcessing File".$file." ........Start"."\n");
  #print("Moving from ".$i_active_folder." to ".$i_target_folder."\n");
 $status=moveFile("$i_active_folder", "$file", "$i_target_folder");
 #print "status after moveFile: ".$status."\n";
 if ($status == 0){
   $status=zipFile("$i_active_folder", "$file", "$i_target_folder");
 }
 #print "status after zipFile: ".$status."\n";
 if ($status == 0){
    push @filesList, "$i_target_folder\\$file";
    #unlink $i_target_folder\$file; 
    #print("Finished Processing File: ".$file."\n");
    $file_counter+=1;
    if ($file_counter % 100 == 0 ){
    printMultipath ($file_counter." Files Handled"."\n", $log_handler);
    }
  }else{
    printMultipath ("ERROR!!! Failed to zip file: ".$file."\n", $log_handler);
    return 1;
  }
    }
}
     }
 
     #print $log_handler "Finished handling $file\n";
     #print $log_handler "Size occupied by files: $total_file_size \n";
  }
  #print $log_handler ("Closing Directory $i_active_folder \n");
  closedir(DIR_HANDLER);
  if ($action eq $ACTION_GET_SPACE){
      printMultipath ("Looking for files named: ".$filter."\n"."\n",$log_handler);
      printMultipath ("Files Found............: ".$file_counter."\n",$log_handler);
      #print $log_handler ("About to process $file_counter files........ Calculating Free Space\n");
      $total_file_size
  }elsif ($action eq $ACTION_MOVE_FILES){
      printMultipath ("Finished Zipping Files\n",$log_handler );
      printMultipath ("Files Handled..........: ".$file_counter."\n", $log_handler );
      printMultipath ("\n",$log_handler );
  }  
  $status;
}

sub deleteFiles{
  my $keep_files_ind = shift;
  my $log_handler = shift;

foreach my $fileToDelete(@filesList){
     if ($keep_files_ind eq "Y"){
    $status = printMultipath ("This file should be deleted: ".$fileToDelete."\n",$log_handler );
 }else{
    printMultipath ("Deleting File: ".$fileToDelete."\n",$log_handler );
    $status = unlink $fileToDelete;
 }
 if ($status != 1){
   printMultipath ("Error Inside deleteFiles. status = ".$status."\n",$log_handler );
   return $status;
 }else{
   #translate to perl status
   $status=0;
 }
       }
 #print "Exit with status: = ".$status."\n";
 $status;
}

sub moveFile{
  my $source_folder=@_[0];
  my $file=@_[1];
  my $targer_folder=@_[2];
  #print "About to Move $file from $source_folder to $targer_folder\n";
  $status = move ("$source_folder\\$file","$targer_folder\\$file") or die ("$source_folder\\$file, $targer_folder failed: $!");
  #print "Move finished with status: ".$status."\n";
  #Success exit status from system calls on Windows is 1
  if($status == 1){
    $status=0;
  }
  $status;
}

sub zipFile{
  my $targer_folder=@_[0];
  my $file=$_[1];
  my $cmd = "\""."$i_zip_cmd"."\""." a -y -tzip "."\""."$i_target_folder\\$i_zip_file_name"."\""." "."\""."$i_target_folder\\$file"."\""."| find "."\""."Compressing"."\"";
  #print "Running command: ".$cmd."\n";
  $status=system($cmd); 
  $status;
}

sub createFolder{
  my $i_folder = shift;
  my $log_handler = shift;
  
  my $status=0;
  #print "Will try to create now new folder: ".$i_folder;
  if (!-d "$i_folder" ){
    printMultipath ("\n",$log_handler);
    printMultipath ($display_delimiter,$log_handler);
    printMultipath ("Creating Monthly Directory:..............."."$i_folder"."\n",$log_handler);
    my $cmd = "mkdir "."\"".$i_folder."\"";
    printMultipath ("Running mkdir command: ".$cmd."\n",$log_handler);
    $status=system($cmd);
    if ($status != 0){
       printMultipath ("Error in Routine createFolder"."\n",$log_handler);
       printMultipath ("Failed to Create Folder: ".$i_folder."\n",$log_handler);
       printMultipath ("Return status is: ".$status."\n",$log_handler);
       return $status;   
    }
    printMultipath ("Created.\n",$log_handler);
    printMultipath ($display_delimiter,$log_handler);
    printMultipath ("\n",$log_handler);
  }
  $status;
}

sub getTimeStamp{
  my $file = @_[0];
  my $file_time = (strftime "%Y%m", (localtime(stat("$file")->mtime)));
  $file_time
}

sub handleStatus{
  my $status = shift;
  my $method = shift;
  my $log_handler = shift;
  
  if ($status != 0){
    printMultipath("Error in Routine ".$method."\n",$log_handler);
    printMultipath("Exit handle_folder.pl script with status: ".$status."\n",$log_handler);
    exit 2;
  }
  $status;
}

sub printMultipath{
  my $str = shift;
  my $log_handler = shift;
  print $log_handler ($str);
  print ($str);  
}

sub reportInputParams{
  print "Calling handleCustomer() with Parameters:"."\n";
  print "i_run_date = ".$i_run_date."\n";
  print "i_monthly_folder = ".$i_monthly_folder."\n";
  print "i_customer_name = ".$i_customer_name."\n";
  print "i_active_folder = ".$i_active_folder."\n";
  print "i_file_filter = ".$i_file_filter."\n";
  print "keep_files_ind = ".$keep_files_ind."\n";
  print "i_target_folder = ".$i_target_folder."\n";
  print "i_zip_cmd = ".$i_zip_cmd."\n";
  print "i_zip_file_name = ".$i_zip_file_name."\n";
  print "logFile = ".$logFile."\n";
}


sendMail.bat
blat %1 -to fname.lname@gmail.com,fname.lname@gmail.com -i from.someone@gmail.com -subject "Monthly ZIP Report. Please see attached log." -attacht %2

No comments:

Post a Comment