Solved

read data line by line from a textfile

Posted on 2007-03-21
6
156 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
  • 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now