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)

link|improve this question
I could have been of more help if this was linux. – mtahmed Aug 12 '11 at 7:01
@mtahmed .. thankyou still for taking the time to read my question .. cheers – pckeys Aug 12 '11 at 7:33
feedback

migrated from stackoverflow.com Aug 12 '11 at 8:04

This question came from our site for professional and enthusiast programmers.

4 Answers

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

link|improve this answer
thanks sgmoore... exactly what I needed – pckeys Aug 12 '11 at 11:01
feedback

Powergrep can do what you ask.

link|improve this answer
thanks for answering, I had a look at powergrep and although I'm sure its wonderfull.. I cant justify the %159.00 price tag for the few files I would want to do every so often :( – pckeys Aug 12 '11 at 7:58
Or download activeperl . . . – surfasb Aug 12 '11 at 8:46
feedback

If the files are not too big then you can use

$filenames = @("file1.txt", "file2.txt", "file3.txt")

foreach ($file in $filenames) 
{
    $replacementStr = $file + ' changed' 
    (Get-Content $file) | 
        Foreach-object { $_ -replace 'change me please' , $replacementStr   } | 
     Set-Content $file
}

Note the brackets around (Get-Content $ file) which means the file is read into memory (hence the requirements that the files are small), but this means that you the file is no longer in use when you go to write it back.

if the files are too big for memory you can write it to a temporary file and then use something like

Cp $tempfilename $file 
rm $tempfilename

to copy the temporary file over the original and delete the temporary file.

link|improve this answer
feedback
perl -p -i -e 's/change me please/$argv changed/' file1 file2 subdir/*.txt foo/*.xyz

You can download Perl (for Windows) from Activestate or Strawberry etc

link|improve this answer
feedback

Your Answer

 
or
required, but never shown