| Home | Basic/Intermediate version | Advanced version | Super Advanced version |

Free Download & Tutorial: How to Perform Multiple Search and Replace Operations on Multiple Files in One Fell Swoop! (Super Advanced)

By Howard Charles Best, May 18, 2010

(llbest.com)


This tutorial assumes the following 3 things:

1. That extensions for known file types are not hidden. (1. Click Start / My Documents. 2. Click Tools / Folder Options…. 3. Click the View tab. 4. Make sure that Hide extensions for known file types is unchecked.)

2. That you have Notepad2 or a shortcut to it on your desktop.

3. That you have installed Perl according to the tutorial at:
http://llbest.com/PerlProgramming.htm.


On this web page:
A. Introduction
B. Get the Free Download
C. Main Features
D. Example
E. Preview Mode
F. Subfolder Support
G. Conclusion


A. Introduction

This web page is a tutorial on how to use a free Perl program to do search and replace operations on a super advanced level of complexity. As I said in the advanced and the intermediate versions, the Perl program has saved me tremendous amounts of time. I have used it hundreds of times, sometimes making changes to thousands of HTML files at a time! This web page uses exactly the same Perl program as the advanced and the intermediate versions. The only difference is that some Regular Expression Perl statements have been replaced.


B. Get the Free Download

The free download (_REPLACE4.zip) contains the following 2 files:

Screen capture of Notepad2 showing the 2 files

1. Download now (1.82 KB).

2. Unzip _REPLACE4.zip to a folder called _REPLACE4..


C. Main Features:

1. Multiple files can be changed in one fell swoop.

2. These files can be a mixture of regular ANSI and UTF-8.

3. Optionally, files in subfolders may also be included.

4. Changed and unchanged files can be easily separated by sorting by “Date Modified.” (Unchanged files are not rewritten.)

5. A log file is created which lists how many changes were made to each file.

6. Optionally, Preview Mode can be used to do a find or to “go through the motions” without actually making any changes to any files.

7. If the .bat file is renamed (in order to create specialized versions, for example), then the new .log file will automatically have the new name.


D. Example:

Suppose that you have a folder containing hundreds of .htm files. Each of the .htm files may contain one or more of the following diacritic characters: á, é, í, ó, ú, and/or ý. For the purpose of search engine compatibility, you want to fool the search engines’ spidering software into thinking that there are no diacritics, but still allow the diacritics to appear on the computer monitor. This can be done by making sure that all of the letters in the body of the HTML do not contain any diacritics, but then, using JavaScript, replace the text immediately after the page has loaded with the diacritics version.

The following shows how lines 23 through 26 and lines 67 through 77 were changed in _PERL_SandR.bat in order to get the job done in fine style:

Note: The $buffer=~s`(<p.*</p>)`… line of regular expression Perl code above is much longer and much more complicated than the code used in the Advanced version. See if you can figure it out using http://perldoc.perl.org/perlreref.html and http://perldoc.perl.org/index-functions.html.

Double click _PERL_SandR.bat and you should see:

Screen capture of search and replace operation

Now there are 4 files:

Screen capture showing the 4 files

Before:

<html>
<head>
</head>
<body>
<p>
První
<p>
Druhý
<p>
Tretí</p>
</body>
</html>

After:

The After version above shows that the diacriticless version is there between the <body…>…</body> for the search engines to spider, but the text is replaced by the diacritics version the second that the page loads, so the diacritics version is all that the visitors actually see!


Note: Lines 25, 26, 71 and 75 of _PERL_SandR.bat and _PERL_SandR.bat use what are called Regular Expressions. For more examples of search / replacement string syntax using Regular Expressions, see http://llbest.com/RegularExpressions.htm. For a tutorial on Perl regular expressions, see http://perldoc.perl.org/perlretut.html. For a reference, see http://perldoc.perl.org/perlreref.html.


E. Preview Mode

The .bat file contains the following in lines 15 through 17:


F. Subfolder Support

The .bat file contains the following in lines 19 through 21:


G. Conclusion

Now you are ready to perform multiple search and replace operations on any number of files:

1. Be sure to keep backup copies of all of the files until you are sure that the search and replace operations were done correctly.

2. Copy the files to be edited to a temporary folder such as C:\Temp2.

3. Copy _PERL_SandR.bat to the same folder.

4. Comment out lines 69 through 75 and add your own particular search and replace operation line(s).

5. If appropriate, comment out both lines 25 and 26 and then add a file name mask assignment line which fits the particular files that you wish to change.

6. Double click the _PERL_SandR.bat file’s icon, and if all goes according to plan, all of the files will now be edited automatically!


| Home | THIS WEB PAGE URL: http://llbest.com/_REPLACE4.htm | Basic/Intermediate version | Advanced version | Super Advanced version |