| Home | Simple version | Whole Word version | Regular Expression version |

Free Download & Tutorial: How to Perform Multiple Search and Replace Operations on Multiple Files Using a List of Regular Expressions

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 using an internal list of Regular Expressions. The Regular Expressions may contain Unicode characters in which case the .bat file must be saved as a UTF-8 file without signature.


B. Get the Free Download

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

Screen capture of Notepad2 showing the 2 files

1. Download now (1.83 KB).

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


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. Changed and unchanged files can be easily separated by sorting by “Date Modified.” (Unchanged files are not rewritten.)

4. A log file is created which itemizes the number of changes by file and within files, by Regular Expression.

5. 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 want to update a folder full of a mixture of .htm and .html files, some of which may be missing the > character on the end of the </html>. You also want to check to make sure that each .htm / .html file has one and only one </html>. Each .htm / .html file also contains the string: http://yourwebsite.com/..., and you want to change each of the ... strings to the actual file name of that particular file. Using this web page as an example, http://llbest.com/... should be changed to http://llbest.com/_SandR_RE.htm.

Here’s how to do it:

1. Drag and drop the _PERL_SandR.bat file’s icon onto the Notepad2 icon. Then you will see lines 22 through 35 as follows:

Note: # at the beginning of a line indicates that the line is a comment, so the Perl compiler simply ignoes it.

Note: Line 24 could have been shortened as follows:

Here is what _PERL_SandR.test looks like:

2. Double click _PERL_SandR.bat file’s icon. Then you should see the following:

Screen capture of search and replace operation

2 new files are created:

Screen capture showing the 4 files

_PERL_SandR.test was copied to _PERL_SandR.htm and then the search and replace operations were performed:

Here is what the LOG file looks like:

(_PERL_SandR.log)

It indicates that there was one and only one <html> tag in the test file, but it does not indicate whether or not it was actually changed.


Note: Lines 24, 25, 30 and 33 of _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 14 through 16:


F. Subfolder Support

The .bat file contains the following in lines 18 through 20:


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 changed to a temporary folder such as C:\Temp2.

3. Copy _PERL_SandR.bat to the same folder.

4. Comment out lines 30 and 33 and then add your own particular Regular Expression line(s).

5. If appropriate, comment out both lines 24 and 25 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/_SandR_RE.htm | Simple version | Whole Word version | Regular Expression version |