Feedback appreciated!#!/usr/bin/perl -w my @dirList; my @fileList; my $totalEdits = 0; $procDir = $ARGV[0]; if (!$ARGV[0]) { print "Give directory: "; chomp($procDir = <STDIN>); } 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 unlinki +ng $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? <y|n>\n"; chomp($goOn = <STDIN>); 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 = <STDIN>); print "Please enter what you would like to replace it with:\n" +; chomp($toReplace = <STDIN>); foreach ($i = 0; $i < ($#fileList + 1); $i++) { open(FH, $fileList[$i]); $fileContents = <FH>; 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\" $#file +Parts times in $fileList[$i]\n"; } } print "$totalEdits made in $#fileList files\n"; }
In reply to Search and Replace Entire Directories by CiceroLove
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |