Developers, Developers, Developers! Maksim Sorokin IT Blog

30Jun/10Off

Replacing Parameters in Files with VBScript

Here is a small VBScript that replaces parameters in a file. For example you have file1 with:

Hello, my name is ${name}. I am from ${place}.

Using this VBScript replacer, you can replace those parameters and write all the contents to another file file2:

replacer.vbs file_with_parameters file_to_write key1 value1 key2 value2 ..

And here is replacer itself:

Set args = WScript.Arguments
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(args.item(0), 1)

Set writeToFile = objFSO.CreateTextFile(args.item(1), true)

Set params = CreateObject("Scripting.Dictionary")
For i = 2 To args.Count-1 Step 2
	params.Add args.item(i), args.item(i+1)
Next

Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
  Redim Preserve arrFileLines(i)
  arrFileLines(i) = objFile.ReadLine
  i = i + 1
Loop
objFile.Close

For Each strLine in arrFileLines
	For Each key In params
		strLine = Replace(strLine, "${" & key & "}", params.Item(key))
	Next

  writeToFile.WriteLine(strLine)
Next
writeToFile.Close
Tagged as: , Comments Off
Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.