Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


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

migrated from Aug 12 '11 at 8:04

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

This question would have been more useful if 'search files using wildcard' had been specified. – Eugene van der Merwe May 23 '14 at 8:09

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:



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

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

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

I found an issue with your script. Having $replacementStr = $file + ' changed' adds the sting $file to the changed file. It should be: $replacementStr = ' changed'

I modified this for my own purposes, but now the files names are dynamic, the string is changed correctly, and paths are not broken.

Don't forget to run this first...Set-ExecutionPolicy RemoteSigned

Code follows:

$filenames = @(get-childitem -Path "C:\Program Files\Tivoli\TSM\baclient\*.opt" | % { $_.FullName })

        foreach ($file in $filenames) 
            $replacementStr = 'New String' 
            (Get-Content $file) | 
                Foreach-object { $_ -replace 'Old String' , $replacementStr   } | 
             Set-Content $file
             Write-Host Processed $file


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

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

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

Open multiple files in Notepad++ and can find/replace across all open files.

share|improve this answer
Can Notepad++ be automated in a Batch file? If so, how would OP go about this? If not, this doesn't answer OP's question. – Heptite Apr 29 at 22:44

You must log in to answer this question.