How can you find and replace text in a file using the Windows command-line environment?
How can you find and replace text in a file using the Windows command-line environment?
Question
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?
Accepted Answer
A lot of the answers here helped point me in the right direction, however none were suitable for me, so I am posting my solution.
I have Windows 7, which comes with PowerShell built-in. Here is the script I used to find/replace all instances of text in a file:
powershell -Command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File -encoding ASCII myFile.txt"
To explain it:
powershell
starts up powershell.exe, which is included in Windows 7-Command "... "
is a command line arg for powershell.exe containing the command to run(gc myFile.txt)
reads the content ofmyFile.txt
(gc
is short for theGet-Content
command)-replace 'foo', 'bar'
simply runs the replace command to replacefoo
withbar
| Out-File myFile.txt
pipes the output to the filemyFile.txt
-encoding ASCII
prevents transcribing the output file to unicode, as the comments point out
Powershell.exe should be part of your PATH statement already, but if not you can add it. The location of it on my machine is C:\WINDOWS\system32\WindowsPowerShell\v1.0
Update
Apparently modern windows systems have PowerShell built in allowing you to access this directly using
(Get-Content myFile.txt) -replace 'foo', 'bar' | Out-File -encoding ASCII myFile.txt
Read more… Read less…
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.
(Get-Content test.txt) | ForEach-Object { $_ -replace "foo", "bar" } | Set-Content test2.txt
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 on SourceForge.
Usage example:
fart.exe -p -r -c -- C:\tools\perl-5.8.9\* @@[email protected]@ C:\tools
will preview the replacements to do recursively in the files of this Perl distribution.
Only problem: the FART website icon isn't exactly tasteful, refined or elegant ;)
Update 2017 (7 years later) jagb points out in the comments to the 2011 article "FARTing the Easy Way – Find And Replace Text" from Mikail Tunç
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.
set str=teh cat in teh hat
echo.%str%
set str=%str:teh=the%
echo.%str%
Script Output:
teh cat in teh hat
the cat in the hat
ref: http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace
Create file replace.vbs:
Const ForReading = 1
Const ForWriting = 2
strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.Write strNewText 'WriteLine adds extra CR/LF
objFile.Close
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 "
BatchSubstitute.bat
on dostips.com is an example of search and replace using a pure batch file.
It uses a combination of FOR
, FIND
and CALL SET
.
Lines containing characters among "&<>]|^
may be treated incorrectly.