| Home | “‘_PERL_SandR_IF.bat’ saves a lot of time and trouble if you want to perform the same ‘search and replace’ operation on more than one file.” | Basic version | Advanced version |
“_PERL_SandR_IF.bat” (contained in “_REPLACE2.zip” - Download now) works with all versions of Windows.
NOTE: This web page assumes that you have downloaded and installed Perl in a folder named “C:\Perl”. Go to Program in Perl on Your Windows Computer” for instructions on how to do this. (Use the ALTERNATE METHOD.)
NOTE: Before performing a multiple file global search and replace using the following method, make sure that you have backup copies of all of the files that you are editing!
“_PERL_SandR_IF.bat” saves a lot of time and trouble if you want to perform the same “search and replace” operation on more than one file. In order to perform one or more search and replace operations on multiple files, you simply change four lines in “_PERL_SandR_IF.bat,” and then double click on it’s icon.
One thing that I really like about this method is that, unlike other methods, it does not change the time/date stamp of files which it doesn’t change. This makes it very easy to separate the modified files by sorting by “Date Modified.”
Note: If any of the following lines are cut off on the right, try using IE’s “View/Text Size” to make the text
smaller.
@echo off
if "%OS%" == "Windows_NT" goto WinNT
C:\Perl\bin\perl -x -S "%0"
goto endofperl
:WinNT
C:\Perl\bin\perl -x -S %0
if NOT "%COMSpEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
if %errorlevel% == 9009 echo You do not have perl in your PATH.
if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
goto endofperl
#!perl -w
#line 12
use File::Copy;
sub printIt
{
$s=shift;
print $s;
print LOG $s;
}
sub SandR
{
$search=shift;
$replace=shift;
&printIt("~s`$search`$replace`\n");
$changes+=($temp1=~s`$search`$replace`);
}
sub SandRg
{
$search=shift;
$replace=shift;
&printIt("~s`$search`$replace`g\n");
$changes+=($temp1=~s`$search`$replace`g);
}
$0=~/.*\\(.*)/;
$thisFileName=Win32::GetLongPathName($1);
$logFileName=$thisFileName;
$logFileName=~s/\.\w+?$/.log/;
open(LOG,">$logFileName") or die "Can't open \"$logFileName\": $!.\n";
$dirname=".";
$fcount=0;
$totalChanges=0;
##### Change the following to the file name extension(s) of the files you wish to edit:
$filenamemask='\.(htm|html)$';
opendir(DIR, $dirname) or die "Can't opendir \"$dirname\": $!.\n";
print"\n";
while(defined($fname=readdir(DIR)))
{
if(($fname=~m/$filenamemask/i)&&($fname ne $thisFileName)) # Test for desired file name.
{
$fcount++;
$changes=0;
open(IN,$fname) or die "Can't open \"$fname\": $!.\n";
$tempfile='C:\\temp.tmp';
open(OUT,">$tempfile") or die "Can't open \"$tempfile\": $!.\n";
@buffer=<IN>;
close(IN);
$"="";
$buffer="@buffer";
##### First we find the portion of the text that the search / replace operations are to be performed upon:
if($buffer=~s`<title>(.*?)</title>`<title>6TY34e</title>`)
{
$temp1=$1;
##### The format for each of the following are 'search string','replacement string':
SandRg('\'','’');
SandRg('"','\'');
$buffer=~s`6TY34e`$temp1`;
}
print OUT $buffer;
close(OUT);
if($changes)
{
copy($tempfile,$fname) or die "Copy to \"$fname\" failed: $!";
$totalChanges+=$changes;
&printIt("$changes changes made to file \"$fname\".\n\n");
}
else
{
&printIt("No changes made to file \"$fname\".\n\n");
}
}
}
|
Listing of “_PERL_SandR_IF.bat”
EXAMPLE: Suppose that you want to edit only the text which appears between <title> and </title> in a large number of “.htm” and “.html” files. If the text contains one or more apostrophies ('), you want to change them to ’, and if the text contains one or more quote signs ("), you want to change them to apostrophies ('). The following are lines 49, 68, 74 and 75 from the above .bat file, which are designed to do the job:
$filenamemask='\.(htm|html)$'; |
if($buffer=~s`<title>(.*?)</title>`<title>hr"+"ef=ma"+"ilto:"+temp1+"\x40"+temp2+".com>"+temp1+"\x40"+temp2+".com</title>`) |
SandRg('\'','’');
SandRg('"','\'');
|
Line 49 could be shortened as follows:
$filenamemask='\.html?$'; |
Note: Lines 49, 68, 74 and 75 all use what are called Regular Expressions.
For more examples of search / replacement string syntax using Regular Expressions, see http://LLBest.com/RegularExpressions.htm.
For complete documentation on Regular Expressions, see http://ashutoshsaxena.tripod.com/jstut/quickstart/ch080.html.
Questions, comments and suggestions are welcome.
Howard Best ()
| Home | THIS WEB PAGE URL: http://LLBest.com/_REPLACE2.htm | Basic version | Advanced version |