Solved

read data line by line from a textfile

Posted on 2007-03-21
6
161 Views
Last Modified: 2010-04-30
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.
0
Comment
Question by:mustish1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 27

Expert Comment

by:VBRocks
ID: 18765986
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?

0
 

Author Comment

by:mustish1
ID: 18766001
Thanks only in VB6
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 18766028
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()
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:mustish1
ID: 18766035
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
0
 
LVL 27

Accepted Solution

by:
VBRocks earned 500 total points
ID: 18766063
Sure, it's the same, just create 2 variables:

Dim fso as new FileSystemObject
Dim tsRead as TextStream
Dim tsWrite as TextStream
Dim inLine as String

set tsRead = fso.OpenTextFile("c:\temp\firstfile.txt", ForReading)
set tsWrite = fso.OpenTextFile("c:\temp\secondfile.txt", ForWriting)

Do
     'Read the line from the firstfile
     inLine = tsRead.ReadLine

     'Write the line to the secondfile
     tsWrite.WriteLine inLine

Loop Until tsRead.AtEndOfStream = True

tsRead.close
tsWrite.close

Let me know if you have any other questions!
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 18766099
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question