Here are some suggestions to a question about how to replace strings in a file. Some of them seem quite elaborate. There are examples in PowerShell (Windows), some external utilities, VBScript, JScript, the Windows Batch language, and perl.
How can you find and replace text in a file using the Windows command-line environment?
I am writing a batch file script using Windows command-line environment and want to change each occurrence of some text in a file (ex. "FOO") with another (ex. "BAR"). What is the simplest way to do that? Any built in functions?
up vote78down voteaccepted If you are on Windows version that supports .Net 2.0, I would replace your shell. PowerShell gives you the full power of .Net from the command line. There are many commandlets built in as well. The example below will solve your question. I'm using the full names of the commands, there are shorter aliases, but this gives you something to Google for. up vote70down vote Just used FART ("F ind A nd R eplace T ext" command line utility):
excellent little freeware for text replacement within a large set of files. The setup files are here. will preview the replacements to recursively do in the files of this Perl distribution. Only problem: the FART website icon isn't exactly tasteful, refined nor elegant ;) ||
show 2 more comments up vote36down vote Replace - Replace a substring using string substitution Description: To replace a substring with another string use the string substitution feature. The example shown here replaces all occurrences "teh" misspellings with "the" in the string variable str. Script Output: ref: http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace show 1 more comment up vote21down vote Create file replace.vbs: To use this revised script (which we’ll call replace.vbs) just type a command similar to this from the command prompt: cscript replace.vbs "C:\Scripts\Text.txt" "Jim " "James " up vote14down vote I don't think there's a way to do it with any built-in commands. I would suggest you download something like Gnuwin32 or UnxUtils and use the sed command (or download only sed): up vote12down vote I have written a small hybrid JScript/batch utility called REPL.BAT that is very convenient for modifying files via the command line or a batch file. The purely native script does not require installation of any 3rd party executeable, and it works on any modern Windows version from XP onward. It is also very fast, especially when compared to pure batch solutions. REPL.BAT simply reads stdin, performs a JScript regex search and replace, and writes the result to stdout. Here is a trivial example of how to replace foo with bar in test.txt, assuming REPL.BAT is in your current folder, or better yet, somewhere within your PATH: type test.txt|repl "foo" "bar" >test.txt.new move /y test.txt.new test.txt The JScript regex capabilities make it very powerful, especially the ability of the replacement text to reference captured substrings from the search text. I've included a number of options in the utility that make it quite powerful. For example, combining the Mand X options enable modification of binary files! The M Multi-line option allows searches across multiple lines. The X eXtended substitution pattern option provides escape sequences that enable inclusion of any binary value in the replacement text. The entire utility could have been written as pure JScript, but the hybrid batch file eliminates the need to explicitly specify CSCRIPT every time you want to use the utility. Here is the REPL.BAT script. Full documentation is embedded within the script. add comment up vote7down vote Here's a solution that I found worked on Win XP. In my running batch file, I included the following: The replace.bat file is as below. I did not find a way to include that function within the same batch file, because the %%a variable always seems to give the last value in the for loop. replace.bat: up vote5down vote I have used perl, and that works marvelously. .orig is the extension it would append to the original file For a number of files matching such as *.html