Replace a line in a text file. 
Author Message
 Replace a line in a text file.

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



Mon, 29 Nov 2004 08:51:13 GMT  
 Replace a line in a text file.
Changing the line 2 in c:\x.txt.

SET FSO=CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
SET F=FSO.OPENTEXTFILE("C:\X.TXT")
A=SPLIT(F.READALL,VBCRLF): F.CLOSE
A(1)="SOMETHING"
SET F=FSO.CREATETEXTFILE("C:\X.TXT")
FOR I=0 TO UBOUND(A)
F.WRITELINE A(I)
NEXT

Anyway there are more ways doing that according to your requirement and your
equipment.
--
Han Pohwan, Seoul/Korea

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



Mon, 29 Nov 2004 09:36:41 GMT  
 Replace a line in a text file.

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



Mon, 29 Nov 2004 09:46:47 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Find and replace a line in a text file

2. Replace lines in a text file

3. Replacing a single line from a text file

4. replace line with new line in ini file

5. How to search a text file and replace text

6. Replace text in a text file

7. Replace text sequnec in text files : how ?

8. Reading Line by Line through Text File

9. Replacing text in only the top 13 lines of a document

10. Replace text in an existing text file?

11. Replace text in an existing text file?

12. Replace text in an existing text file?

 

 
Powered by phpBB® Forum Software