use strict; use warnings; use Getopt::Long qw(GetOptions); use File::Basename; use POSIX qw/strftime/; my $inputdir; my $OPCO; my $OutPath; my $LogFile; ################ GetOptions( 'inputdir=s' => \$inputdir, 'opco=s' => \$OPCO, 'outputdir=s' => \$OutPath, 'logfile=s' => \$LogFile, ) or die "Usage: $0 --inputdir inputPath --opco OPCO --outputdir OUTPUT_PATH --logfile LOG_FILE \n"; my $FILESPLITCNT=2; my $log = $LogFile; open(my $flog, '>>:encoding(UTF-8)', $log) or die "ERR: Could not open file (to write): '$log' $!"; my $finputDir = $inputdir; opendir(DIR, $finputDir) or die "ERR: Can't open directory $finputDir: $!"; my @files = readdir(DIR); my $CDRCNT=0; my $initSeq=0; my $SEQNUM = sprintf("%03d",$initSeq); my $out = $OutPath."/".$OPCO."_SS_".strftime('%Y%m%d_%H%M%S',localtime)."_".$SEQNUM.".cdr"; open(my $fout, '>>:encoding(UTF-8)', $out) or die "ERR: Could not open file (to write): '$out' $!"; print STDOUT "OUTPUTFILE PATH IS:".$out."\n"; foreach my $file (@files) { if ( -f $finputDir . "/" . $file) { my($filename, $dirs, $suffix) = fileparse($file); my $finput = $finputDir . $filename; my $fileLength = length($filename); if($fileLength < 50) { #KEEP EXTRA SPACES to filename #$filename .= (" " * (50 - length($filename))) } open(my $fin, '<:encoding(UTF-8)', $finput) or die "Could not open file (to read) '$finput' $!"; print $flog "Processing File:" . $finput . "\n"; while (my $row = <$fin>) { chomp $row; if($CDRCNT == $FILESPLITCNT ) { close $fout; $CDRCNT = 0; $SEQNUM = $SEQNUM + 1; $out = $OutPath.$OPCO."_SS_".strftime('%Y%m%d_%H%M%S',localtime)."_".sprintf("%03d",$SEQNUM).".cdr"; open( $fout, '>>:encoding(UTF-8)', $out) or die "ERR: Could not open file (to write): '$out' $!"; } print $fout "$filename;$row\n"; $CDRCNT = $CDRCNT + 1; } undef $finput; close $fin; } } close $fout; close $flog;