Search / Replace Script, Little Update

I would like a slight alteration of this script

I would like to be able to do this: swap.cfg  c:/reports  

and have it go through c:/reports and do in place search / replace in the files

Here is a sample swap.cfg   search for Replacewith


or if someone could explain how the config file works below that would be cool too.

#SWAP.PL by J.Tom Germain,
#September, 1996
#Copyright 1996-2000
#You are free to use & modify this script for your own purposes as long
#as it includes the credit you see on line 3, and don't sell it.
#I am not responsible for losses or injury of any kind resulting from the
#use of this script
#Other freeware available at

if($#ARGV < 0){
print "Enter string to search for:\n ";
$search = <STDIN>;
print "\nEnter string to replace it with:\n ";
$replace = <STDIN>;
print "\nEnter directory path to search:\n ";
$dirpath = <STDIN>;
$doit[0] = "$search^^$replace^^$dirpath";
$filename = $ARGV[0];

if($filename !~ /\w/){
print "You must specify a configuration file.\n";
print "If you wish to enter parameters manually, don't put arguments.\n";

$dirpath = $ARGV[1];

if($filename !~ /\./){$filename .= '.rep';}
$filename =~ s/\s//g;

open(FIL, "<$filename") || die "Can't open $filename\n";
@doit = <FIL>;

print "\nStarting search and replace...(Copyright 1996-2000 Tom Germain)\n";

$count = 0;

foreach $line (@doit){
$line =~ s/(\n|\r|\t)//g;

($search,$replace,$newdirpath) = split(/\^\^/,$line);
if($newdirpath ne ""){$dirpath = $newdirpath;}

if($dirpath eq ""){$dirpath = '.';}
$dirpath =~ s/\s//g;

if($dirpath !~ /(\/)$/){$dirpath .= '/';}

print "Opening $dirpath\n";
opendir(DIR, "$dirpath") || next;
print "Okay\n";

@files = readdir(DIR);

@files = grep(!/^(\.|\.\.)$/,@files);

foreach $file (@files){

if(!(-f "$dirpath$file")){

if(-d "$dirpath$file"){
$doit[$#doit + 1] = "$search^^$replace^^$dirpath$file";

open(FIL,"<$dirpath$file") || next;

@data = ();

@data = <FIL>;

if($num = grep(/$search/i,@data)){

foreach $line (@data){
if($line =~ s/$search/$replace/gi){$count++;}

open(FIL,">$dirpath$file") || next;
print FIL @data;
} #end loopx

} #end loopy
} #end loopz

print "\nDONE! Total matches replaced: $count\n";

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

while( <> ){
   push @replace,[split];
   $_ = qr/\Q$_\E/ for $replace[-1][0];
   last if eof;
{local $^I=".bak";
while( $line=<> ){
   $line =~s/$_->[0]/$_->[1]/g for @replace;
   print $line;
rjohnsonjrAuthor Commented:
Closed, 500 points refunded.
The Experts Exchange
Community Support Moderator of all Ages

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.