| Home | “‘_PERL_SandR_IF2.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 | Intermediate version |
“_PERL_SandR_IF2.bat” (contained in “_REPLACE3.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_IF2.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 seven or eight lines in “_PERL_SandR_IF2.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:
while($buffer=~s`<p>(.*?)</p>`6TY34e</p>`s)
{
$temp1=$1;
##### Next, we check the text to see if certain strings are present:
if((($temp1=~s`<i>`<i>`g)==1)&&(($temp1=~s`</i>`</i>`g)==1)&&($temp1=~m`^\s*?<i>`)&&($temp1=~m`</i>\s*?$`))
{
##### The format for each of the following are 'search string','replacement string':
SandRg('^\s*?<i>','<p class=VerseText>');
SandRg('</i>\s*?$','');
}
##### The format for each of the following are `search string`replacement string`:
$buffer=~s`6TY34e`g543dt$temp1`;
}
##### The format for each of the following are `search string`replacement string`g (global):
$buffer=~s`g543dt`<p>`g;
$buffer=~s`<p><p class=VerseText>`<p class=VerseText>`g;
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");
}
}
}
closedir(DIR);
&printIt($fcount?"$totalChanges total changes made to $fcount \"$filenamemask\" file(s).\n":"No \"$filenamemask\" files found.\n");
close(LOG);
print"File \"$logFileName\" created.\n\n";
__END__
:endofperl
if exist C:\temp.tmp del C:\temp.tmp
pause
|
Listing of “_PERL_SandR_IF2.bat”
EXAMPLE: Suppose that, in a large number of “.htm” and “.html” files, you want to edit all of the text which appears between the first <p> and the first </p> (the first paragraph) only if the <p> is immediately followed by <i>, and the </p> is immediately preceeded by </i>, and then only if there is only one occurrance of <i> and only one occurrance of </i> within the paragraph. Then and only then should the <p><i> be changed to <p class=VerseText> and the </i></p> changed to </p>. Repeat this proceedure for the second paragraph, the third paragraph, etc. until the end the the file is reached. The following are lines 49, 68, 74, 79, 81, 85, 90 and 91 from the above .bat file, which are designed to do the job:
$filenamemask='\.(htm|html)$'; |
while($buffer=~s`<p>(.*?)</p>`href=. onclick="try{loadIt('.');return false;}catch(e){}" title=http://LLBest.com/>Home</p>`s)
|
if((($temp1=~s`<i>`<i>`g)==1)&&(($temp1=~s`</i>`</i>`g)==1)&&($temp1=~m`^\s*?<i>`)&&($temp1=~m`</i>\s*?$`)) |
SandRg('^\s*?<i>','<p class=VerseText>');
SandRg('</i>\s*?$','');
|
$buffer=~s`href=_REPLACE.htm onclick="try{loadIt('_REPLACE.htm');return false;}catch(e){}" title=http://LLBest.com/_REPLACE.htm>Basic version` |
$buffer=~s``g; $buffer=~s`<p><p class=VerseText>`<p class=VerseText>`g; |
Line 49 could be shortened as follows:
$filenamemask='\.html?$'; |
Note: Lines 49, 68, 74, 79, 81, 85, 90 and 91 all use what are called Regular Expressions.
For more examples of search / replacement string syntax using Regular Expressions, see Intermediate version.
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/_REPLACE3.htm | Basic version | Intermediate version |