Link to home
Start Free TrialLog in
Avatar of mustish1
mustish1

asked on

read data line by line from a textfile

Hi: Can any one please tell me how to i read data line by line from a textfile and then write into another textfile?

Thanks.
Avatar of VBRocks
VBRocks
Flag of United States of America image

Are you using VB6 or VB.NET?

VB6:  Add a reference to the Microsoft Scripting Runtime Library (scrrun.dll) Project / References.  

Then, code like this:
Dim fso as new FileSystemObject
Dim ts as TextStream
Dim inLine as string

'To Read:
set ts = fso.OpenTextFile("c:\temp\myfile.txt", ForReading)
Do
     inLine = ts.ReadLine
     'Process with your line.
Loop Until ts.AtEndOfStream = True
ts.close

'To Write:
set ts = fso.OpenTextFile("c:\temp\myfile.txt", ForWriting)
ts.WriteLine "Cool!"
ts.Close

Do you need it in VB.NET?

Avatar of mustish1
mustish1

ASKER

Thanks only in VB6
Here's VB.NET:
Dim inLine as String

'To read
Dim sr As New System.IO.StreamReader("c:\temp\myfile.txt")
Do
     inLine = sr.ReadLine
Loop until sr.EndOfStream = True
sr.Close

'To write
Dim sw as New System.IO.StreamWriter("c:\temp\myfile.txt")
sw.WriteLine("Cool!")
sw.Close()
Thanks can you please tell me how to i read one line from firstfile.txt and write into second file.txt until firstfile.txt is at end.

Dim fso as new FileSystemObject
Dim ts as TextStream
Dim inLine as string

'To Read:
set ts = fso.OpenTextFile("c:\temp\firstfile.txt", ForReading)
Do
     inLine = ts.ReadLine
     'Process with your line.
Loop Until ts.AtEndOfStream = True
ts.close

'To Write:
set ts = fso.OpenTextFile("c:\temp\secondfile.txt", ForWriting)
ts.WriteLine "Cool!"
ts.Close
ASKER CERTIFIED SOLUTION
Avatar of VBRocks
VBRocks
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Don't forget to add the Project Reference, or you won't be able to use the FileSystemObject:

Add a reference to the Microsoft Scripting Runtime Library (scrrun.dll) Project / References.