Board index » Visual Studio » Writeline & Write

Writeline & Write

Visual Studio6
Am I correct in thinking that when I write to a text file using the

writeline or write command that all the data in the text file will be

deleted?



If this is the case can I please have some advise on how to replace

just one line in a 50-line text file?



Thanks Jo


-
 

Re:Writeline & Write

FSO.OpenTextFile(filepath, mode)

mode can be:

1 - open it for reading.

2 - open it for writing; overwrites existing text.

8 - open it for appending; adds new text to end.



To replace one line you have to open it, read it

out into a string, chop it in half, then put it back

together with your new line and write a new file.



You can do that by first opening for reading (1).



Then either use ReadAll to get the whole file into

a string and pick it apart using Instr to find the line

you want to clip and separate that out

OR

use ReadLine, reading all lines in the file and saving

them to a new string, one at a time, until you get to the

one that you want to clip out. Then write your new line

to the new string instead and continue until all lines have

been processed.

OR

Read out the whole file using s = TS.ReadAll and use

Array1 = Split(s, vbcrlf) to get an array of line to work with.



Whichever method you use, close the file after reading.

Then you'll want to put together

the whole edited file as a new string and then overwrite

the old file by opening it a second time with

OpenTextFile(filepath, 2) ' "for writing" mode.



--

--

Jo <jolyon.webster@friendsprovident.co.uk>wrote in message

Quote
Am I correct in thinking that when I write to a text file using the

writeline or write command that all the data in the text file will be

deleted?



If this is the case can I please have some advise on how to replace

just one line in a 50-line text file?



Thanks Jo





-

Re:Writeline & Write

Jo,



Do a bit of checking into the File System Object. This is included in

VBScript and provides you with a great deal of flexibility. You can

use Append if you just want to add lines. You can read the file into

a single variable using ReadAll, replace anything in the file, and

then save it again.



Stan Scott

New York City



jolyon.webster@friendsprovident.co.uk (Jo) wrote in message news:<69087d17.0407080612.53e0a3c9@posting.google.com>...

Quote
Am I correct in thinking that when I write to a text file using the

writeline or write command that all the data in the text file will be

deleted?



If this is the case can I please have some advise on how to replace

just one line in a 50-line text file?



Thanks Jo

-