Solved

read data line by line from a textfile

Posted on 2007-03-21
6
159 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel - Save a copy of work book 13 89
Problem to open Excel file 15 178
Not seen Link button 5 55
VBA - Excel, Hide/unhide range of rows on sheet with listbox selection 9 63
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

828 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