Quote:
> I know how to add lines to a text file but how does one replace a line. my
> object is to read a file which containes lines like below and replace
> certain lines with replacement strings.For instance, how would I replace
> line 2 with lets say "textD"
> 1...textA
> 2...textB
> 3...textC
If you you want to change lines, you must read all the lines from the original
file and change the lines you want. This can be done by creating a new file
structure in memory or in a new temorary file. Then you must overwrite the
originale file completely.
Here is a couple of examples. The first one does a global replace (substitute)
over the hole content of the file. The second example changes only line 2.
' **** Code start ****
' Replace globally
Const ForReading = 1, ForWriting = 2
Const FailIfNotExist = 0, CreateIfNotExist = -1
Const OpenAsASCII = 0, OpenAsUnicode = -1, OpenAsDefault = 2
Dim i, sSearchString, sSubstString, oFSO, sMyFile, fMyFile
' String to search for
sSearchString = "textB"
sSubstString = "textD"
' File to update
sMyFile = "c:\temp\test.txt"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set fMyFile = oFSO.OpenTextFile(sMyFile, ForReading, _
FailIfNotExist, OpenAsASCII)
' Get file content into an variable:
Dim sContents
sContents = fMyFile.ReadAll
fMyFile.Close
Set fMyFile = Nothing
' Using the Replace function to replace all text containing the
' text defined in sSearchString anywhere in the file
' Change vbTextCompare to vbBinaryCompare to make it case sensitive
Dim sContentsNew
'sContentsNew = Replace(sContents, sSearchString, sSubstString)
sContentsNew = Replace(sContents, sSearchString, sSubstString, _
1, -1, vbTextCompare)
' Overwrite the old file with the new content,
' but only if content has changed
If sContents <> sContentsNew Then
' Content has been changed, continuing
Set fMyFile = oFSO.OpenTextFile(sMyFile, ForWriting, _
CreateIfNotExist, OpenAsASCII)
fMyFile.Write sContentsNew
fMyFile.Close
End If
WScript.Echo "Finished!"
' **** Code stop ****
' **** Code start ****
' Replace only line 2
Const ForReading = 1, ForWriting = 2
Const FailIfNotExist = 0, CreateIfNotExist = -1
Const OpenAsASCII = 0, OpenAsUnicode = -1, OpenAsDefault = 2
Dim i, sSubstString, iLineNumber, oFSO, sMyFile, fMyFile
' String to be put in
sSubstString = "textD"
' The line number that is to be updated
iLineNumber = 2
' File to update
sMyFile = "c:\temp\test.txt"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set fMyFile = oFSO.OpenTextFile(sMyFile, ForReading, _
FailIfNotExist, OpenAsASCII)
' Get file content into an array:
' One way of doing it:
Dim aContents
aContents = Split(fMyFile.ReadAll, vbCrLf)
' Other way of doing it (the complicated way ;-):
'Dim aContents()
'i = 0
'Do Until fMyFile.AtEndOfStream
' ReDim Preserve aContents(i)
' aContents(i) = fMyFile.ReadLine
' i = i + 1
'Loop
fMyFile.Close
Set fMyFile = Nothing
'Update line specified in iLineNumber
' Subtracting 1 because arrays are 0-based
aContents(iLineNumber - 1) = sSubstString
' Overwrite the old file with the content
Set fMyFile = oFSO.OpenTextFile(sMyFile, ForWriting, _
CreateIfNotExist, OpenAsASCII)
' Join the array and write it to the file:
' One way of doing it:
fMyFile.Write Join(aContents, vbCrLf)
' Other way of doing it (the complicated way ;-):
'For i = 0 To UBound(aContents)
' fMyFile.WriteLine aContents(i)
'Next
fMyFile.Close
WScript.Echo "Finished!"
' **** Code stop ****
--
torgeir