#!/usr/bin/perl -w my @dirList; my @fileList; my $totalEdits = 0; $procDir = $ARGV[0]; if (!$ARGV[0]) { print "Give directory: "; chomp($procDir = ); } if ($ARGV[1]) { if ($ARGV[1] eq "-clean") { $cleanUp = 1; $unlinkedFiles = 0; } } push @dirList, $procDir; print "Processing."; while (!$toDie) { $thisDir = shift(@dirList); if ($thisDir) { opendir(CURRDIR, $thisDir); @tempDirList = readdir(CURRDIR); rewinddir(CURRDIR); close(CURRDIR); print "."; foreach ($i = 0; $i < ($#tempDirList + 1); $i++) { if (substr($tempDirList[$i],0,1) ne ".") { $completeHandle = $thisDir . "/" . $tempDirList[$i]; if (-d $completeHandle) { push @dirList, $completeHandle; } elsif (-T $completeHandle) { push @fileList, $completeHandle; if ($cleanUp && index($completeHandle, ".byProc", 0) > 0) { unlink $completeHandle or print "Error unlinking $completeHandle: $!\n"; $unlinkedFiles++; } } } } } else { $toDie = 1; if ($unlinkedFiles) { $unlinkedFiles--; if ($unlinkedFiles == 0) { $unlinkedFiles = "No"; } } print "\n"; } } if ($unlinkedFiles) { print "\n\n$unlinkedFiles files deleted.\n\n"; } if (!$cleanUp) { print "\n\n" . $#fileList . " files found.\n\n"; while (!$canGo) { print "Process all files found? \n"; chomp($goOn = ); if ($goOn eq "y") { #work with each file $canGo = 1; } elsif ($goOn eq "n") { print "Exiting...\n"; exit; } else { print "Must be either yes [y] or no [n]\n"; } } print "Please enter search criteria (can be RegExp):\n"; chomp($toFind = ); print "Please enter what you would like to replace it with:\n"; chomp($toReplace = ); foreach ($i = 0; $i < ($#fileList + 1); $i++) { open(FH, $fileList[$i]); $fileContents = ; close(FH); my @fileParts = split($toFind,$fileContents); $totalEdits += ($#fileParts--); if ($#fileParts < 1) { #print "No instances of \"$toFind\" found in $fileList[$i].\n"; } else { $oldFile = $fileList[$i] . ".byProc"; open(FH, ">>$oldFile"); print FH $fileContents; close(FH); open(FH,">$fileList[$i]"); $fileContents =~ s/$toFind/$toReplace/eg; print FH $fileContents; close(FH); print "Replaced \"$toFind\" with \"$toReplace\" $#fileParts times in $fileList[$i]\n"; } } print "$totalEdits made in $#fileList files\n"; }