Editing text files with FSO

I'm stuck trying to find a way to edit a specific line in a text file with FSO.  No problems with reading and appending... but how do I overwrite just one specific line?  This is what I have so far:

Set fso = CreateObject("Scripting.FileSystemObject")
Set objFileTextStream = fso.OpenTextFile(txtfile, 1, True)
Do While NOT objFileTextStream.AtEndOfStream
  strLine = Server.HTMLEncode(objFileTextStream.ReadLine)

'Strip off id and check against record to be edited

findtab =instr(strLine,chr(9))
'response.write "findtab is: " & findtab
id= Mid(strLine, 1, findtab-1)
'If it matches then set the new line.
if id = request("id") then
newLine = id & chr(9) & Request("aDate") & chr(9)& Request("title")& chr(9)& Request("asp_file")& chr(9) & Request("doc")
strLine = newLine
end if

'  NOW WHAT???      

Who is Participating?
robbertConnect With a Mentor Commented:

the method OpenTextFile allows the iomodes ForReading, and ForAppending, not ForWriting.

Instead, use CreateTextFile.

What I'm doing with my counter, is;

Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile("CntRedir.txt")
n = 0
n = ts.ReadLine
n = n + 1
Set ts = Nothing
Set ts = fso.CreateTextFile("CntRedir.txt") 'implicitly Overwrite = True
ts.Write n
Set ts = Nothing
Set fso = Nothing

You have to read all lines into memory, parse them there, build a new string, and overwrite the old file.

You won't save ressources by creating a temporary file, because you have to put the whole contents into the memory, either.

Hmmm.  I'm going to throw out a radical guess here.  What you might need to do is to rewrite the entire file.

The way you would do that is to create a string.  Loop through the fie.  If the line is NOT the one you want to change, add it to the string.  If it is the one you want to change, add the changed text to the string.  Once you have the entire new string set up, close the file and rewrite it with the new text string.

It's a bit clunky, but I don't know that there is a way in the FSO to delete a line from a file (which is the long way of getting to what you need to do).

TTom: just a comment, but wouln't it be better (as in, for the server) to use another temporary text file to output the values to, rather than a string, which uses the system memory (I know it's not much, but..... )

Read a line, if it's ok, output it to temp.txt
if you need to change it, output the new line.
read the next line

once this is completed, delete the origional file and rename the new (temp.txt) file to whatever the old one was called.
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Sounds good to me.

stewmic1Author Commented:
Edited text of question.
stewmic1Author Commented:
I appreciate the responses.  I believe that SiM99's solution will probably work.  I'm trying it now.

Normally, I would use a database to handle this type of tool, however because of the type of account this customer has, it's not an option.
stewmic1Author Commented:
All Courses

From novice to tech pro — start learning today.