#!/usr/bin/perl ############### ## Libraries ## ############### use File::Copy; use File::Find; use File::Path; use File::Basename; use File::stat; use strict; use warnings; use Time::localtime; ################## ## User-defined ## ################## #--check for rptCustomAssembly word my $find = "CustomAssembly"; #--the root location of files to start reading (this can be our SharePoint site) my $rootLocationOfFiles = 'C:\rptTest'; #--the name and location of log file my $logFile = '>C:\rptDest\logRptFindAndReplace.txt'; #--get the start timestamp my $currentTime = time; #--Open a logfile to write the file names open (LOGFILE, $logFile); #--get current timestamp my $startTimeStamp = '[' . timestamp() . ']'; #--write to our logfile print LOGFILE "-------------------------------------------------------------------\n"; print LOGFILE "Start time: $startTimeStamp\n"; print LOGFILE "Please Note:\n"; print LOGFILE "List of reports that are modified by script.\n"; print LOGFILE "-------------------------------------------------------------------\n\n"; #----------------------------------------------------- #--Find and replace $source with $target if CustomAssembly is not referenced in report file my $source = 'en-US'; my $target = 'en-US CustomAssembly, Version=1.0.0.1, Culture=neutral, PublicKeyToken=null '; use File::Find qw(finddepth); my @files; my $counter = 0; my @myHashArray = ( {toFind => ' Embedded', toReplace => ' External' } ); #--END @myHashArray finddepth(sub { return if($_ eq '.' || $_ eq '..'); my ($dir, $name, $ext) = fileparse($File::Find::name,'.rdl'); my $fileName = $_; chop($name); my $fullFileName = "$name\\$fileName"; my $Dest = "$rootLocationOfFiles\\$fileName"; #--read only .rdl files if($ext eq '.rdl') { my $data = read_file($File::Find::name); for ($data) { if ($_ !~ /$find/) { $data =~ s/$source/$target/g; $data =~ s/$_->{toFind}/$_->{toReplace}/g foreach (@myHashArray); write_file($File::Find::name, $data); #--write to the file print LOGFILE "$counter $File::Find::name\n"; print LOGFILE "\t Original line: HERE WOULD BE MY ORIGINAL LINE\n"; print LOGFILE "\t Replaced Line: HERE WOULD BE THE NEW LINE\n"; print "$counter $File::Find::name\n"; $counter = $counter + 1; #system("copy \"$fullFileName\" \"$Dest\""); #--copy files } else { $counter = $counter + 1; $data =~ s/$_->{toFind}/$_->{toReplace}/g foreach (@myHashArray); write_file($File::Find::name, $data); print LOGFILE "$counter $File::Find::name\n"; print LOGFILE "\t Original line: HERE WOULD BE MY ORIGINAL LINE\n"; print LOGFILE "\t Replaced Line: HERE WOULD BE THE NEW LINE\n"; print "$counter $File::Find::name\n"; #print "$_->{toFind} : $_->{toReplace}\n" foreach (@myHashArray); } } } }, $rootLocationOfFiles); my $endTimeStamp = '[' . timestamp() . ']'; my $totalSeconds = time - $currentTime; my $totalMins = ($totalSeconds/60)%60; my $totalSec = $totalSeconds%60; print LOGFILE "-------------------------------------------------------------------\n"; print LOGFILE "\n"; print LOGFILE "Program ended at: $endTimeStamp\n"; print LOGFILE "It took $totalMins minute(s) and $totalSec seconds\n"; print "DONE, it took $totalMins minute(s) and $totalSec seconds\n"; close (LOGFILE); #----------------------------------------------------- #--Read the file sub read_file { my ($filename) = @_; open my $in, '<:encoding(UTF-8)', $filename or die "Could not open '$filename' for reading $!"; local $/ = undef; my $all = <$in>; close $in; return $all; } #----------------------------------------------------- #--Write the file back sub write_file { my ($filename, $content) = @_; open my $out, '>:encoding(UTF-8)', $filename or die "Could not open '$filename' for writing $!";; print $out $content; close $out; return; } #----------------------------------------------------- #--get timestamp sub timestamp { my $t = localtime; return sprintf( "%04d-%02d-%02d at %02d:%02d:%02d", $t->year + 1900, $t->mon + 1, $t->mday, $t->hour, $t->min, $t->sec ); } #-----------------------------------------------------