Windows – Find and replace text in multiple files

find and replacewindows 7

I’m hoping someone might lend their time in helping me create a batch file or similar for finding and replacing text in several files.
I have tried many “search and replace” utilities but have not found something that does what I need.

The requirements are as follows:
Find and replace the SAME text in multiple files with different text FOR EACH FILE.

Example:

File1.txt, file2.txt, file3.txt all have a text string “change me please”

for file1.txt replace text string “change me please” to “file1 changed” save as original filename (file1.txt)
for file2.txt replace text string “change me please” to “file2 changed” save as original filename (file2.txt)
for file3.txt replace text string “change me please” to “file3 changed” save as original filename (file3.txt)

Best Answer

Using PowerShell commands:

Get-Content c:\file1.txt | ForEach-Object { $_ -replace "change me please", "file1 changed" } | Set-Content c:\changed1.txt

Get-Content c:\file2.txt | ForEach-Object { $_ -replace "change me please", "file2 changed" } | Set-Content c:\changed2.txt

leaves me with the 2 files:

changed1.txt

changed2.txt

Could some kind person tell me how I would then rename these files and overwrite the originals:

changed1.txt to file1.txt

changed2.txt to file2.txt

Related Question