Search and Replace Text in Multiple Files in Only 7 Lines

Have you ever needed to perform search / replace operations on multiple files Google TV in a folder and all its subfolders? Have you ever needed to search / replace text in a list of files? How about carrying out multiple search / replace operations on a single file based on search / replace pairs specified in a text file? This article shows how to do each of these common search / replace operations using TEXTools–and believe it or not–in only 7 lines of batch code!

To carry out each of these tasks, we’ll be utilizing TEXTools (TCL.exe) from a batch file. TEXTools is the perfect compliment to batch file programming as it greatly extends the power of batch files. Without implementing a single for loop, a TEXTools endowed batch file can nonetheless easily process multiple files. This is made possible because batch files can be constructed on-the-fly by another batch file and then called by that batch file, (its kind of like a train that builds its track as it goes). The reason that a for loop isn’t needed here to process multiple files is simply because TEXTools can take a list of files that need to be processed and translate that list into a batch file that, when run, results in the processing of each of the files. For example, suppose we wanted to carry out some relatively complex text editing operations on the following list of files:

myfile.txt

yourfile.txt

theirfile.txt

One way to do this would be to place the editing logic into a batch file called procfile.bat and then process each file–one after the other–using procfile.bat like this:

procfile.bat myfile.txt

procfile.bat yourfile.txt

procfile.bat theirfile.txt

That’s great if you only have 3 files, but what if you have 100 or even 1000 files to process? That’s where TEXTools comes into the picture. If batch files are just text and they can be constructed on-the-fly, then why not use TEXTools to turn our list of filenames into a batch file that, when run, results in the processing of each of the files? If a batch file like procfile.bat is going to be employed in the process anyway, then we’re already half-way to our goal. What we want is for the list of file names to be preceded by “procfile.bat” with one change–procfile.bat needs to be called. Now we have a batch file that will do all the work for us:

Leave a Comment