Sunday, September 4, 2011

Perl Code to Merge/Concat/Combine selected SQL files in a location


#!/usr/bin/perl -w
use strict;
use warnings;
use File::Find;

my $sourceDirectory = "D:/DropBoxFiles/Dropbox/";
my $destinationDirectory = "C:/";

my (@loadDirectory) = "${sourceDirectory}FilesToBeCombined";
my $SQLmerged = "${destinationDirectory}combinedSQLFile.SQL";

my $fname = "";
my $fileCountTemp = 0;

open ( SQLmerged, "+>", $SQLmerged ) or die "\n\n\nCould not open file SQLmerged.txt.$!";

find(\&process_files,@loadDirectory);

sub process_files {

    #print $File::Find::name."\n";
    my $filename = $File::Find::name;

#Check if it is a file or a directory
    if( -f $filename) {

# To get extention: Match a dot, followed by any number of non-dots until the end of the line.
my ($extension) = $filename =~ /(\.[^.]+)$/;

# Check if it is a sql file
if($extension eq ".SQL" || $extension eq ".sql" ){

#Check if the file starts with ETL_ or DATA_
my $fname = substr($filename,rindex($filename,"/")+1,length($filename)-rindex($filename,"/")- 1);

if($fname =~ /^ETL_/ || $fname =~ /^DATA_/){
#print "$fname";
#print "\n";

#OPEN FILE filename for READING (CHECK FOR FAILURES)
open ( openedFile, "<", $filename ) or die "Could not open file B.txt: $!";
print SQLmerged "\n---------\n-- ${fname}\n---------\n\n";

#READ EACH LINE OF FILE filename and add it to SQLmerged
while ( my $line = ) {
 print SQLmerged $line;
}
print "\nAdded (${fileCountTemp}): $fname to combined stg.";
$fileCountTemp++;
}
else{
print "$fname";
print "\n";
}

}
    }
}