Thursday, October 6, 2011

TS&R: Replacing Text in Multiple Files

This blog post (copied across from the original Knol) describes the "textfile-search-and-replace" windows application I've developed. It's far from complete but it is open-source so you download the source and change it yourselves if you want or just work with changes other people have made.

Please feel free to report any bugs or feature requests via the "" link below.

NOTE: Check out other "textfile-search-and-replace" posts (clicking on the tag associated with this post) I've created (or will create) for worked examples.

There are only two requirements to run this application; a Windows OS and Microsoft .NET Framework Version 4. The .Net framework is a free download available from Microsoft which you can download from this URL;

Where do I get the program?
The project home page is below, it is hosted on Google Code;

The software is written using Visual Studio.Net (2010). It is a fairly simple windows application.

Click "Downloads" (second tab from the left, or just click here) and select the Download at the top of the list, this will always be the most recent version. This Knol was produced using "Version 0.01" of the software. Later versions may vary, but probably not by much.

A direct link to this download is available here.

You need to copy the file to somewhere on your local machine. It is not signed so the first time you run it you will see a dialog similar to;

Open File - Security Warning Dialog
Click "Run" (you might want to un-check the "Always ask before opening this file" check-box, otherwise it will keep asking you the same question every time you start the application).

NOTE: You should *always* be a little suspicious of applications downloaded from the Internet. The source is available so you can always check the code and build it yourself if in doubt.

Using The Application
When you first start the application you are presented with the following screen;

Text File Search and Replace V0.01 - Main Dialog
Wen you next start the application it will remember your saved settings so there are some settings you only need to enter once.

How It WorksIn order to use the application you need to specify four things;
  • Search Folder: This is the folder the application will look for files in. Search of sub-folders is not supported so you will need to make sure all the files you want to search are in a single folder OR you will need to do the search multiple times.
  • File Extensions: You need to enter the file extensions (.txt, .rtf, docx, etc) that you would like the application to read the contents of. You need to use the "|" character as a separator between multiple file extensions - for example if you want to search all .txt and .docx files you would need to enter .txt|.docx. File extensions are not case sensitive so .txt will give you .TXT, .txt, .Txt, etc.
  • Replace Strings: This large entry box needs to contain the strings you want to replace in the format Old|New (using the same separator as file extensions). The strings are processed from top to bottom so, for example if you had two lines the first with Old|New and the second with Oldish|Notsooldish then the second string would *never* be used as by the time the application checks for it any instances of "Oldish" would already have been replaced and become "Newish"
  • Output Folder: Rather than modifying your files in the search folder this application creates a copy of the file and works on that. These copies are stored in the "Output Folder" directory. Only files that have been modified by the search will be held there. Once the process has completed it's then up to you to make sure the changes are what you intended(!) and copy the new files back over the originals
Once you're ready click "Execute" the process will begin.

No comments: