Bulk File Find / Replace Command Line Tool
Don’t you ever want to search many many files in nested directories and find certain text to replace with something else? Is it not tedious? Do we all not have something better to do?
So, after searching the net, I could not find a free tool and I decided it will take less time to write one than to search for one.
I just want to say THANK YOU for the replace.exe program! It was exactly what I was looking for! All the other programs I tried were giving me trouble. It is an excellent product!
- Jeff - a delighted user
I created a command line tool replace.exe (which is scriptable) and decided to share it with you.
You can download the replace.exe now for free. Replace.exe is a Windows command line utility that searches files in directories to find a given text and
replace with another. For enhanced functionality, check out Bestcode File Utilities Pack. (Free version of replace.exe displays our sponsor websites, you may buy Bestcode File Utilities for no-advertisement version of replace.exe)
Currently it does not support regular expression search, the text to search needs to be exact match, except that case sensitive or insensitive search option can be specified.
Here are the screen shots:
Below are the command line options (they can be specified in any order):
Below is the case when we search for “hello” in all the files in the current directory (default), and replace each occurrance in every file with “hello world1”.
Since by default, the destination directory is the same as the source directory, the original files will be modified.
If there was no match, it could look like this:
If we want to copy the modified files (along with the unmodified ones) to a new directory, we could give a destination directory as below:
That’s pretty much all there is to it. Enjoy and let me know if there is a problem. Oh yes! It does not support specifying a destination directory that is under the source directory.
Please don’t do that. It will end up creating bunch of nested directories, because it will appear that such target directory itself is part of the source and it will start processing the target directory as if it
is source too.
Escaping double quotes
On the Windows command line, when you specify a string that has quotes in it, you may have difficulty when you also need to “quote” that whole string. [Some Windows versions do not support escaping via \”
]. There would be no way of escaping the inner quotes. To overcome this problem, replace.exe allows you to specify any character sequence to be interpreted as quotes in the -find and -replace parameters: Notice the \q below.
In this example, we are searching for:
text inside all HTML files.
And we want to replace it with:
text. Since these both text parameters themselves contain an empty space they need to be quoted on the Windows command line. Thus, the outer double
quotes conflict with the inner quotes that are part of the content. So, we use the special flag -quotes \q to mean we will use \q inside our parameters instead of quotes.
So, we write the parameters like:
and then the replace.exe interprets the \q as quotes when it runs.
Above example is a real life case where on one HTTP server I am out of space and need all my HTML pages to point to another server for all IMAGE resources that used to be local.