Solved

read data line by line from a textfile

Posted on 2007-03-21
6
158 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access Object Property from VBA Module in Excel 2010 2 31
Problem to open text file 11 111
Zip Folders Using Chilkat Routines 1 41
Capture logon name 13 73
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

810 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