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?
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.
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.
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.
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.
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.)
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.