How to read text file line by line?

J

JenHu

Hi,

I want read line by line and characters. The characters are fix length
text file, no specific delimited method between each fields. The first
line is header line, the last line is footer. Between the first line
and last line is the content, and I will have to load the content to
database.

How to read line by line? I use For lineNum = LBound(lines) To
(UBound(lines) - 1) which generate error because it does read the
array.
Can someone show me how?

Thx

sample file:
------------------------------------------

H2004/07/2709:52:484.0TEST123MARKETING800302096510
D009084826009084826397000763333WITOWSKIDICK 41000620000520000010
F000W20040727FR3970007122228000000124444NWITOWSKIDICK 20040727250USD
D.........
F.........
T11 0 0 11 0 4250 0 0

my code:
-------------------------------------------------
Public Function ProcessRET(ByRef strFilepath As String)
Dim EpayConnection As New SqlConnection
Dim cmdBatchName As New SqlCommand
Dim dtrBatchName As SqlDataReader
EpayConnection.ConnectionString = strEpayDBConn
With cmdBatchName
.Connection = EpayConnection
.CommandText = "SELECT File_Name FROM EPay_Batch_Table Where
File_Status=2"
End With
EpayConnection.Open()
dtrBatchName = cmdBatchName.ExecuteReader()
dtrBatchName.Read()
Dim strRetFileName As String
strRetFileName = dtrBatchName("File_Name")
Dim ReturnFileName As String = strRetFileName.Replace(".in", ".RET")
Dim flRetBatch As File
Dim stRetBatch As FileStream
Dim smRetBatch As StreamReader
Try
Dim sr As New StreamReader(strFilepath &
ReturnFileName)
'Open the text file into a stream reader
Dim sRetLine As String
Dim sBatchNum As String
Dim sRecordType As String
Dim sEmpID As String
Dim sSSN As String
Dim sCardNum As Int32
Dim sErrorCode As String
Try
Dim lines() As String
Dim contents As String
Dim lineNum As Integer
sRetLine = sr.ReadLine
lines = Split(contents, vbCrLf)
If Left$(sRetLine, 1) = "H" Then
sBatchNum = (Mid$(sRetLine, 73, 13))
Dim BatchNum As Int32
BatchNum = Convert.ToInt32(sBatchNum)
End If
sRetLine = sr.ReadLine
For lineNum = LBound(lines) To (UBound(lines) - 1)
'sRetLine = sr.ReadLine
If Left$(sRetLine, 1) = "D" Or "R" Then
sBatchNum = RTrim(Mid$(sRetLine, 73, 85))
End If
Next lineNum
Catch err As Exception
MsgBox("File retrieve fail", err.Message)
End
End Try
Catch
End Try
End Function

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
C

Chris, Master of All Things Insignificant

This is the idea. Code not tested, but it should work. It will write out
every line to the output window.

try
Dim lineNum As Integer
sRetLine = sr.ReadLine
If Left$(sRetLine, 1) = "H" Then
sBatchNum = (Mid$(sRetLine, 73, 13))
Dim BatchNum As Int32
BatchNum = Convert.ToInt32(sBatchNum)
End If
Do While not sRetLine is nothing
Debug.WriteLine(sr)
sRetLine = sr.ReadLine
Loop
Catch

Chris
 
H

Herfried K. Wagner [MVP]

JenHu said:
I want read line by line and characters. The characters are fix length
text file, no specific delimited method between each fields. The first
line is header line, the last line is footer. Between the first line
and last line is the content, and I will have to load the content to
database.

Reading a text file line-by-line or blockwise with a progress indicator
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=readfile&lang=en>
 
M

Mike Labosh

I want read line by line and characters. The characters are fix length
text file, no specific delimited method between each fields. The first
line is header line, the last line is footer. Between the first line
and last line is the content, and I will have to load the content to
database.

Both of the other suggestions are pretty goo, but I'd through in an
alternative. You mention that you're loading this into a database. If it
happens to be a SQL Server, you could investigate the BULK INSERT T-SQL
statement. It does *blistering fast* imports.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top