VBScript to change a line of text.

July 31, 2009 at 12:22:04
Specs: Windows XP, Custom
I'm looking for suggestions on how to stream-line a VBScript a little better. I wrote a Script that does what I want it to, but it seems really long and I don't like having to use the loop's I was hoping there was a way to say that I want it to Read the file to memory, replace line 6 and then re-write the file.
This is what I have, it replaces the host that an Oracle client is looking at, which is located on line 6 of an Oracle TNSNames.ora file.

dim StrFileName, StrContents, strLineCount, strShop
'File to be read and re-wrote
StrFileName = "c:\TNSNames.ora"

'Set the shop that I want the client to connect to.
strShop = "Host-computer"

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTS = objFS.OpenTextFile(StrFileName, 1)

'Reads contents of file into strContents variable
strContents = objTS.ReadAll
objTS.close

'Splint's file contents down to lines
StrContents = Split(StrContents, vbNewLine)
'Counts how many lines are in the contents
strLineCount = UBound(StrContents)

'Writes the first few lines back to the file
Set objTS = objFS.OpenTextFile(strFileName, 2)
i=0
do while i<6
objTS.WriteLine (strContents(i))
i = i+1
loop

'Writes new line of content
objTS.WriteLine (" (ADDRESS = (PROTOCOL = TCP)(HOST = ") _
& strShop & (")(PORT = 1562))")

'Writes the remaining contents to the file
i = i+1
do while i<= strLineCount
objTS.WriteLine (strContents(i))
i = i+1
loop


See More: VBScript to change a line of text.

Report •


#1
July 31, 2009 at 14:08:55
Problem 1: No Option Explicit

StrContents = Split(StrContents, vbNewLine)
'Counts how many lines are in the contents
strLineCount = UBound(StrContents)

Problem 2: These are not strings.

Problem 3: Using Do . . . Loop instead of For . . . Next

That said, you're right; loops aren't required for this problem. (Untested)

Option Explicit
Dim objFS, lines
Const StrFileName = "c:\TNSNames.ora" 'File to be read and re-wrote
Const strShop = "Host-computer"       'Set the shop that I want the 
                                      'client to connect to.
Set objFS = CreateObject("Scripting.FileSystemObject")

lines = Split(objFS.OpenTextFile(StrFileName).ReadAll, vbNewLine)
lines(6) = " (ADDRESS = (PROTOCOL = TCP)(HOST = " & strShop & ")(PORT = 1562))"
objFS.OpenTextFile(StrFileName, 2).WriteLine Join(lines, vbNewLine)


Report •

#2
July 31, 2009 at 14:44:05
That is beautiful, it actually wrote over line 7 becuase of the whole starting with line 0 thing, but that is exactly what I was looking for. When you came up with the solution I was actually trying it the following way which works also, but your's is way easier to read, thank you very much.

set objWS = CreateObject("Wscript.Shell")
Set fsob=CreateObject("Scripting.FileSystemObject")

strNewLine = "New Line to Write"
strFileName = "c:\TNSNames.ora"

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTS = objFS.OpenTextFile(strFileName, 1)
strContents = objTS.ReadAll
objTS.Close
arrLines = Split(strContents, vbNewLine)
Set objTS = objFS.OpenTextFile(strFileName, 2)
For Each strLine In arrLines
If strline = arrLines(5) then
objTS.WriteLine strNewLine
else
objTS.WriteLine strLine
End If
Next
Wscript.quit


Report •

Related Solutions


Ask Question