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

Free Download & Tutorial: How to Perform Multiple Search and Replace Operations on Multiple Files Using an External Perl Code File (Super Advanced)

By Howard Charles Best, May 21, 2010

(llbest.com)


This tutorial assumes the following:

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 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. The Header Line
F. How To Eliminate the Drag and Drop
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. This Perl program, in the form of a .bat file, never has to be changed. It is exactly the same Perl program which is used for the basic/intermediate version and the advanced version. All of the instructions on how to do the search and replace operation(s) are contained in a separate .perlcode file, which is to be dragged and dropped onto the .bat file.


B. Get the Free Download

The free download (_SandR_3_SuperAdvanced.zip) contains the following 3 files:

Screen capture of Notepad2 showing the 3 files

1. Download now (2.45 KB).

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


C. Main Features:

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

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

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

4. A Perl code (.perlcode) file controls all of the changes.

5. The Perl code file can be either ANSI or UTF-8.

6. Changed ANSI files are automatically converted to UTF-8 format if the Perl code file is UTF-8.

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

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

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


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.

1. Drag and drop the _SandR_3_SuperAdvanced.perlcode file’s icon onto the Notepad icon. Then you will see the following:

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.

2. Drag and drop the _SandR_3_SuperAdvanced.test file’s icon onto the __SandR_DragAndDropPerlCodeOnMe.bat file’s icon. Then you should see the following:

Screen capture of search and replace operation

Now there are 5 files:

Screen capture showing the 5 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!

Here is what the .log file looks like:

(__SandR_DragAndDropPerlCodeOnMe.log)


Note: The lines of Perl code in the _SandR_3_SuperAdvanced.perlcode file contains 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. The Header Line

Each .perlcode file may have an optional header line. The header line, if it is included, must begin with ###.

The header line may contain any one, two, three or all four of the following:

1. A file name mask such as $FileNameMask='\.htm$';. ($FileNameMask='\.txt$'; is the default)

2. Either $previewMode=0; (Default: No preview mode) or $previewMode=1; (Preview mode: Do not actually make any changes.)

3. Either $doTheSubdirs=0; (Default: Do not include subfolders) or $doTheSubdirs=1; (Include subfolders.)


F. How To Eliminate the Drag and Drop

Here's a way to eliminate the need to drag and drop the .perlcode file onto the __SandR_DragAndDropPerlCodeOnMe.bat file. Instead, you can simply double click the .perlcode file:

1. Move the __SandR_DragAndDropPerlCodeOnMe.bat file to a common area such as C:\DOSSTUFF.

2. Right click on the .perlcode file, and select Open with/Choose default program....

3. Click the Browse... button.

4. Navigate to and double click the C:\DOSSTUFF\__SandR_DragAndDropPerlCodeOnMe.bat file.

5. Make sure that the “Always use the selected program to open this kind of file” checkbox is checked.

6. Click the OK button.


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 __SandR_DragAndDropPerlCodeOnMe.bat and _SandR_3_SuperAdvanced.perlcode to the same folder.

4. Comment out all but the first line of the _SandR_3_SuperAdvanced.perlcode file. Then add your own particular search and replace operation line(s).

5. If appropriate, change (htm|html) to file name extensions that match the particular files that you wish to change.

6. Drag and drop the _SandR_3_SuperAdvanced.perlcode file’s icon onto the __SandR_DragAndDropPerlCodeOnMe.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/_SandR_3_SuperAdvanced.htm | Basic/Intermediate version | Advanced version | Super Advanced version |