#!/usr/local/bin/perl # This is a program which replaces one set of text with another in all # the files with ".htm" or ".html" extensions in the directory from which # the program is run and all its sub-directories. # It's simple enough to use - just copy this file to the top of the directory tree # you want text replacing, make it executable with the "chmod" command and # then run it. The program asks you what text you want replaced and what # text you wish to replace it with. Each time it replaces text it sends a # message to standard output with the path and name of the file in which it # has replaced text. It also lists the directories it has searched. # NOTE: You may need to include the full path with the call to run # the program. e.g. /users/p4/httpd/htdocs/ciaran/replace # The program match is case insensative i.e. upper case will # match to lower case and visa versa, but, for the moment, it will NOT # ignore spaces, carriage returns etc. i.e. two spaces in a line instead of # one will not return a match and therefore not replace the text. # Tested on a Sparc workstation, for a UNIX platform with Perl 5.0? installed. #************************** The Main Program ****************************** print "What text do you wish to replace?\n"; # Input the text to be replaced. $match = ; # Remove the carriage return chop($match); # Transform the text into uppercase in a temporary variable. $tempmatch = $match; $tempmatch =~ tr/a-z/A-Z/; print "Replacement text?\n"; # Input the text to take its place. $replacement = ; # Remove the carriage return chop($replacement); # Start at the directory in which the program is being run. $dirname = "."; #Call the Replace function. &ReplaceText($dirname); print "The Replace program is now complete", "\n"; #************************** The Subroutine ******************************* sub ReplaceText { # Make some of the variables local to the function - for recursion purposes. local(@allfiles, $i, $totalfiles, @subdir, @htmfiles); local($nextline, $found); local($subdirnumber, $htmfilenumber, $j); # Open the directory, read all the files in it and close it again. opendir(DIR, $_[0]) || die "Can't open directory", $_[0], "\n"; #pick up all the files in the directory. @allfiles = grep(/[a-zA-Z0-9_-]*/, readdir(DIR)); closedir(DIR); # Store the number of files that are in the directory. $totalfiles = @allfiles; # Pick out the subdirectories. $i = 0; $j = 0; while($i < $totalfiles) { if (($allfiles[$i] ne ".") && ($allfiles[$i] ne "..")) { $allfiles[$i] = $_[0] . "/" . $allfiles[$i]; # print "allfiles = ", $allfiles[$i], "\n" ; if(opendir(SUBDIR, $allfiles[$i])) { $subdir[$j] = $allfiles[$i]; closedir(SUBDIR); ++$j; } } ++$i; } $subdirnumber = @subdir; # Open the directory again, select the *.htm & *.html files and # replace the text. opendir(DIR, $_[0]) || die "Can't open directory", $_[0], "\n"; print "Searching directory: ", $_[0], "\n"; # For files ending in *.htm or *.html @htmfiles = grep(/\.html?$/, readdir(DIR)); # Store the number of htm files there are. $htmfilenumber = @htmfiles; $i = 0; while($i < $htmfilenumber) { # Add the files directory path. $htmfiles[$i] = $_[0] . "/" . $htmfiles[$i]; # Make the file readable and writable. chmod(0666,$htmfiles[$i]); $tempfile = "tempfile.htm"; # Open the file. open(SRCHFILE, $htmfiles[$i]); open(TEMPFILE, ">$tempfile"); # Input the contents of the file into a buffer. $buff = ""; while() { $buff = $buff . $_; } # Transform the text into uppercase in a temporary buffer. $tempbuff = $buff; $tempbuff =~ tr/a-z/A-Z/; $startindex = 0; $offset = 0; # Find the first instance of matching text. $found = index($tempbuff, $tempmatch); while($found != -1) { $startindex = $found + length($match); # Replace the old text with the new text. substr($buff, $found + $offset, length($match)) = $replacement; $offset = $offset + (length($replacement) - length($match)); # Tell the user of the replacement. print "Replacing text in: ", $htmfiles[$i], "\n"; # Find the next instance of the matching text. $found = index($tempbuff, $tempmatch, $startindex); } print TEMPFILE $buff; close(TEMPFILE); close(SRCHFILE); rename ($tempfile, $htmfiles[$i]); ++$i; } $i = 0; # Store the number of subdirectories. $subdirnumber = @subdir; closedir(DIR); # For each subdirectory, call this function giving recursion to the end of # the directory tree. while($i < $subdirnumber) { &ReplaceText($subdir[$i]); ++$i; } }