Open a text file and edit line items

With the file system object, you can't read and write at the same time. So if you want to edit the content of each line in a text file, it's a little tricky, because you first read the current content into a variable, then you write that variable to the same file.

In this example, the goal is to stick a "," at the end of each line.

STEP 1 -- read the data

Dim mydata, f

Set f = fso.OpenTextFile(newfile)

Do Until f.AtEndOfStream

mydata=mydata&rtrim(f.ReadLine) & "," & vbcrlf

Loop

f.close


STEP 2 -- write the data (this will overwrite whatever was previously in the file)/

Set f = fso.OpenTextFile(newfile, 2, true)

f.writeline mydata

f.Close