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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
Sounds good to me.

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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.

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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stewmic1Author Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.