How can I find and replace text in multiple files?
March 16, 2007 2:07 PM   Subscribe

I have recently been looking for free tools, which can replace multiple lines in plain text- and Word-files. However, I could not find any effective freeware. What would you recommend?
posted by techfreak to Computers & Internet (11 answers total) 2 users marked this as a favorite
Textpad. Supports regular expression search/replacement.
posted by Four Flavors at 2:10 PM on March 16, 2007

Oops, sorry, it onlt works with text documents, not word docs.
posted by Four Flavors at 2:11 PM on March 16, 2007

For doc - don't bother. Copy the text into a plain text file, process that, then paste the result back into the Word doc.

So Textpad could work for you, then.

I code for a living. I bought UltraEdit ( a few years ago and I update it regularly. If I divided the total cost by the number of hours I've used it, my cost per hour is approx $0.03/hour.
posted by Artful Codger at 2:53 PM on March 16, 2007

for plain text, you can't beat perl or sed.

perl -pi -e "s/foo/bar/g" *.txt

sed -e "s/foo/bar/g" -i ".bak" *.txt

Also operating system?
posted by KirkJobSluder at 2:56 PM on March 16, 2007

There are dozens of command-line utilities that come standard with all Linux distributions that make effective text-editing a breeze. You can use them on windows - just install Cygwin.

If you make it that far, post back with more information on your problem and I will give you a little sed tutorial.
posted by reflection at 2:59 PM on March 16, 2007

For text files, I use BK ReplaceEm. You would need to use the regular expression search to specify multiple lines.

For Word files, I record a macro of me doing all the search and replaces once and plug that into another macro that cycles through all the Word files in a directory (and/or subdirectories), running the macro on each Word file in the directory. Here's the VBA code for the macro. Hopefully, it'll post OK. You would need to use Word's special "code" for the return character to search for a string longer than one line (I think it's ^p).

Disclaimer: It can take a long time to run and I didn't write this code. I found it online somewhere, but I couldn't tell you where. I've only used it with Word 2000 and 2003 on a PC.

Public Sub BatchReplaceAll()
Dim FirstLoop As Boolean
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As Long
Dim i As Long

'Enter the directory you want to search here.
PathToUse = "c:\files\WordFiles"

On Error Resume Next

'Closes all open documents before beginning
Documents.Close savechanges:=wdPromptToSaveChanges

FirstLoop = True

'Set the directory and type of file to batch process
With Application.FileSearch
.LookIn = PathToUse
.SearchSubFolders = True
.FileName = "*.doc"
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles

If .Execute() Then
For i = 1 To .FoundFiles.Count

'Open document
Set myDoc = Documents.Open(.FoundFiles(i))

'Here are two examples of the search and replace code.
'The first searches for text, the second searches for a link.
'You have to hit Alt-F9 to show field codes before starting the
'macro to search for links.
With Selection.Find
.Text = "Replace Me"
.Replacement.Text = "This was replaced."
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

With Selection.Find
.Text = "/fakedirectory/fakesubdirectory/fakefile.htm"
.Replacement.Text = "/fakedirectory2/fakesubdirectory2/fakefile2.htm"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

'These two lines update the field codes you just updated. They
'aren't necessary if you're not searching for links.

'Close the modified document after saving changes
myDoc.Close savechanges:=wdSaveChanges

Next i
End If
End With
End Sub

posted by hootch at 3:26 PM on March 16, 2007

KirkJobSluder's answer is good stuff for replacing text on a single line, or for searching for something on a single line and replacing it with multiple lines. If your search regexp spans more than one line though, you have to slurp the whole file; the most common way of doing this is just unsetting the EOL character, $/. Example.
posted by Rhomboid at 3:46 PM on March 16, 2007

For my find are replace I use the Useful File Utilities, they have a Word find and replace plugin. It's 30 bucks, but totally worth it. You can try it for like 6 months or something like that before you have to buy it.
posted by bigmusic at 11:21 PM on March 16, 2007

Notepad++ does this. From the menu bar, choose Plugins->TextFX Edit->Delete Blank Lines. There's also a Delete Surplus Blank Lines option.

I know this works for plain text documents, but I don't know about Word docs.
posted by hooray at 8:17 AM on March 17, 2007 [1 favorite]

Oh, I just realized that the TextFX plugin might not come packaged with Notepad++. You can get it from the download section.
posted by hooray at 8:21 AM on March 17, 2007

Thank you! I am using Windows. I'll definitely check out some tools mentioned in the comments!
posted by techfreak at 5:55 AM on March 20, 2007

« Older Looking for a connector plug for a Thinkpad T60   |   Where to buy a bicycle in Toronto? Newer »
This thread is closed to new comments.