Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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)

share|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
add comment

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

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

5 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

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

Powergrep can do what you ask.

share|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
add comment

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.

share|improve this answer
add comment
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

share|improve this answer
add comment

You can use eclipse - simply load the files into a project, the follow the instructions on the following page:

http://www.avajava.com/tutorials/lessons/how-do-i-do-a-find-and-replace-in-multiple-files-in-eclipse.html?page=1

Basically do a search on the folder containing the files. The results will show up in a search tab. Right click on the file containing the files you want to change and select 'Replace'.

This will change all the files you want. Added bonus of having your files in an eclipse project, which can be helpful in many different ways. (Source control, syntax highlighting, unit testing, etc, etc.)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.