Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I maintain a badly designed website and have to often make a single change to over 400 static webpages. Perl to the rescue! I cobbled this together so it doesnt' have a lot of fancy error-checking but it does open every file of every subdirectory (without recursion) and replaces every instance of your search string with your replace string which it asks on the tty. It then backs up the file it read (by adding .byProc to the end of the filename) and saves the new file. Not the best but it certainly works. You call it by ./progName /directory/path -clean. The -clean switch will search the directories for the .byProc files and unlink them. No safety net!
#!/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"; }
Feedback appreciated!

CiceroLove
Fates! We will know your pleasures: That we shall die, we know; 'Tis but the time, and drawing days out, that men stand upon. - Act III,I, Julius Caesar

In reply to Search and Replace Entire Directories by CiceroLove

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2021-08-03 11:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (38 votes). Check out past polls.

    Notices?