Search Replace Perl Script
Last changed: -195.241.60.154

.
Summary

Get Perl here, if you don't have it yet:

http://www.activestate.com/

The following Perl modules are necessary for this source to work (Perl complained to me about needing them):

Rule.pm

- download it from http://search.cpan.org/src/RCLAMP/File-Find-Rule-0.28/lib/File/Find/Rule.pm

- put it in the directory C:\Perl\lib\File\Find

Compare.pm

- download it from http://search.cpan.org/src/RCLAMP/Number-Compare-0.01/Compare.pm

- put it in the directory C:\Perl\lib\Number

Glob.pm

- download it from http://search.cpan.org/src/RCLAMP/Text-Glob-0.06/lib/Text/Glob.pm

- put it in the directory C:\Perl\lib\Text

SearchReplace.bat

 REM First parameter: directory where the .WIKI / .AWIKI files are
 REM Second parameter: the string to replace (watch out, regular expressions!)
 REM Third parameter: The string to replace with (watch out, regular expressions!)
 REM ---------------
 REM example:
 REM perl SearchReplace.pl C:\\temp\\WikiBases\\MyWiki 001d043702\/helpdesktest helpdeskbase
 REM ---------------
 pause

SearchReplace.pl

 #-------------------------------
 # This Perl script is made to help with the transfer of .WIKI / .AWIKI files to a webserver
 # with a different name. It Replaces a text with another text. This is handy to replace links in the .WIKI files that point to a different webserver. 
 # Make a backup of the files you want to replace first!


 # Arguments:
 # 1: directory where the .WIKI / .AWIKI files are
 # 2: the string to replace (watch out, regular expressions!)
 # 3: The string to replace with (watch out, regular expressions!)
 #
 # NOTE: The filedate / time is preserved by this script (otherwise the "Recent Changes" list will be useless)


 #-------------------------------
 # License Statement
 # Feel free to use this sourcecode for personal or commercial purposes.
 # The Software comes "as is", with no warranties. None whatsoever. This means
 # no express, implied or statutory warranty, including without limitation, warranties
 # of merchantability or fitness for a particular purpose or any warranty of title or
 # non-infringement. The author will not be liable for any damages, either indirect, special, consequential, 
 # or incidental related to the Software.
 #
 # Author: WouterCX
 # Date:   09-09-2004
 #-------------------------------


 use File::Find::Rule;
 use File::stat;
 use Time::localtime;


 #-------------------------------
 # get arguments from commandline
 #-------------------------------
 ### first argument = directory where the wiki's are
 $path = shift(@ARGV);


 ### second argument = old text
 $oldtext = shift(@ARGV);


 ### third argument = new text
 $newtext = shift(@ARGV);


 @files = File::Find::Rule->file()
                         ->in("$path");


 foreach $filename (@files)
 {
   ## Does the filename end with .wiki or .awiki?
  if ($filename =~ /\.wiki$|\.awiki$/)
  {


  print "$filename \n";


  ## Store the file date / time
  $store_date = stat($filename)->mtime;


  ## Open the file
  open (IN, "$filename") || die $!;
  ## print the modified contents to .bak file
  open (OUT, ">$filename.bak") || die $!;


  while ($_ = <IN> ) {


     ## If text exists, replace text
     ## Search = case insensitive
     if ($_ =~ /$oldtext/i) {
         # Replace text, all instances on a line (/g), case insensitive (/i)
         $_ =~ s/$oldtext/$newtext/gi;
     }


     print OUT "$_";


  }


  close (OUT);
  close (IN);
  } #  if ($filename =~ /\.wiki|\.awiki/)


  ## copy the modified .bak file contents to the original file,
  ## thereby overwriting the original
  rename("$filename.bak","$filename") || die $!;


  ### modify file date / time back to original
  ### first parameter = accessed time, second parameter = modified time, third parameter = filename
  utime $store_date, $store_date, $filename;


 } #foreach $filename (@files)

Back to FlexWikiTools