Batch "find & replace" with text files?

I have an .html file that I want to use as a template for about 50 nearly identical documents - all that needs to change is four or five text strings contained within the document.

eg;; the template has “$address” where an actual address should be filled in, “$name” where the name goes, and and so on.

My initial plan was to use notepad to open the template, do a “find & replace” for each string, and then “Save as…” for each file.

I figure there’s got to be some handy way to automate this, though - particlarly as I’ll also need to produce Rich Text & Plain text versions of the same doc, so this would really amount to ~750 individual operations.

I’d love to bang out a .csv file and feed it to a little batch utility (or something similar) instead of spending a few hours of tedium. There’s gotta be something like this freely available, no?

http://download.cnet.com/Global-Search-And-Replace/3000-2248_4-10703022.html

Caveat: Haven’t tried it.

Are you doing this on a Mac? If so, a few simple unix commands will do the trick. I dunno how you do it on a PC.

Use Python, problem solved. You want something like this:



## data.csv
name,address,phone,breast_size
Sheila,123 Fake St.,425-555-1212,35C
Leslie,456 Bogus Ave.,206-555-1212,36D
Kari,789 Balderdash Ln.,360-555-1212,34A
...

# templatize.py
from re import sub

template=open('template.html').read()
data_file=open('data.csv')
fields=data_file.readline()[:-1].split(",")
for index, row in enumerate(data_file.readlines()):
  f=open("outfile%02d.html"%index, "w")
  f.write(reduce(
    lambda new_file, field: sub("\\$%s" % field[0], field[1], new_file),
      zip(fields, row[:-1].split(",")), template))
  f.close()

Haven’t tested this so it may have bugs, but hopefully you get the idea.

If you want an app, I just downloaded this app to do thousands and thousands of .js and .html files, and it works like a charm. I ended up purchasing a copy but you can do what you want with the free demo - just have to wait 20 seconds between commands.

Another great Mac app for this is TextWrangler. Bare Bones Software | TextWrangler is now BBEdit -- and still free! It's time to switch.

It has multi-file find and replace with a simpler interface for non-programmers that also supports grep-style pattern matching. There’s a free version and a pro version, and it’s built with web designers and programmers in mind.

This is also possible in VBScript, which has the advantages of being free and not requiring you to install anything.

[ol]
[li]Paste the script below into a text file with a .vbs extension. [/li]
[li]Customize the value of the strTemplate variable on line 5 with the location of the template file. [/li]
[li]Customize the value of the strList variable on line 6 with the location of a comma-delimited text file containing the values to substitute into the template. Enter the strings to be replaced (e.g. $address, $name) as the column headers in the first line of the .csv file.[/li]
[li]Customize the value of the strFolder variable on line 7 with the location of the folder where the new files should be created.[/li][/ol]
Running the script will create one new file for each line (other than the headers) in the .csv file. They will be named with the contents of the first field and will have the same extension as the template file.


Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

strTemplate = "c:\files	emplate.txt"
strList = "c:\files\list.csv"
strFolder = "c:\files\output"

On Error Resume Next

Set objFSO = CreateObject("Scripting.FileSystemObject")
strExt = objFSO.GetExtensionName(strTemplate)

If Not objFSO.FolderExists(strFolder) Then
    objFSO.CreateFolder(strFolder)
End If

Set objTemplate = objFSO.OpenTextFile(strTemplate, ForReading, False, TriStateUseDefault)
strText = objTemplate.ReadAll
objTemplate.Close

Set objList = objFSO.OpenTextFile(strList, ForReading, False, TriStateUseDefault)
arrListLines = Split(objList.ReadAll, vbCrLf)
objList.Close

arrFind = Split(arrListLines(0), ",")

For i = 1 To UBound(arrListLines)
    strNewText = strText
    arrReplace = Split(arrListLines(i), ",")

    For j = 0 to UBound(arrFind)
        strNewText = Replace(strNewText, arrFind(j), arrReplace(j))
    Next
    
    strNewFile = strFolder & "\" & arrReplace(0) & "." & strExt
    
    Set objNewFile = objFSO.OpenTextFile(strNewFile, ForWriting, True)
    objNewFile.Write strNewText
    objNewFile.Close
Next

I’m assuming that, since you mentioned Notepad, you’re using Windows. In the future it might be a good idea to specify your platform.

[quote=“Number, post:7, topic:494160”]

This is also possible in VBScript, which has the advantages of being free and not requiring you to install anything.

[ol]
[li]Paste the script below into a text file with a .vbs extension…[/ol]If you’ve forgive the smiley… :eek:[/li]
Thanks so much - that’s way beyond. (Thanks also, JTR.)

I really do need to take the time to become more comfortable with VBscript, it’s awfully practical. Your script works beautifully, just as though it were purpose-built for the task. (Heh.) Just needed a little tweak to read the last line.

You are a genius – where do I send cookies?

I’m glad it worked out. No reward is necessa…say, are those chocolate chip?

Actually I do this kind of thing all the time over on Experts-Exchange. It’s a good way to stay in practice. Once one has a good variety of scripts stockpiled, it’s not too difficult to customize one for a slightly different task. Microsoft even provides a big archive of them to get started with.

Very welcome!

If you don’t mind me saying I would highly recommend that you learn Python (or Ruby) instead. They’re both free and open source, although you will need to download and install them on Windows. (They ship with OSX and most flavors of Linux; no installation necessary.) IMO they’re much slicker, more expressive, and just plain fun to use than VBScript. (I’m pretty sure the program I wrote could be written more concisely and prettily in Ruby but I’m not expert in that language yet.)

UltraEdit has had this capability forever. Best text editor I’ve ever seen, and I’ve written more than a dozen of them myself.

And it’s now available in Mac & Linux, too.

Not needed: Linux already has Emacs, the best text editor in the Universe. :wink:

I think you mean that Linux is the bootloader for Emacs, the best operating system in the Universe.

But the best way to do complex search and replace on multiple files is, of course, with Perl.

And, of course, you tried to post an example of a clean, concise Perl program that does what the OP asked for, but the auto-moderator rejected it as probable line noise.