Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27206

Reading 2 text files to get different information to output in one line of outputfile

$
0
0
I am writing a program that reads a tab delimited file and pulls out information that we want to put in a bulk insert file for a database. No problem. Got that.
The program also reads from another file where there are two other items of information needed for the bulk insert file.
I have the program working and producing the output file that I want however, I am just wondering if there is a better way since I am new to vb.net as of six months ago.
I have two separate methods reading and extrapolating data from each file and I used some code twice, which the books say is bad practice.
As it is now, I am reading in one file and adding items to a dictionary. I am then reading the other file and using the user id in this file to pull out the username and user poistion from the items I have in the dictionary. I am only attaching the beginning of the code for each sub, because my main question is: should I have 2 methods that contain identical code for the first six lines (only difference is the file being read)
Private Sub ReadReferenceFile()
Try
Dim sr As New StreamReader(ujob.sWorkPath & ujob.sReferenceFile) ' this is essentially a path to a file \\mypath\referencefile.txt
Dim sData As String = String.Empty

sData = sr.ReadToEnd

If sData.Length > 0 = True Then
Dim sLine As String
Dim sLines() As String = Split(sData, vbCrLf)

For Each sLine In sLines
Dim sFields() As String
sFields = Split(sLine, vbTab)
If sLine.Length > 0 Then

Dim clsUser As New UserInfo
clsUser.nUserId = CInt(sFields(Fields.UserNum))
clsUser.sUserFirstName = sFields(Fields.UserFirst)
clsUser.sUserLastName = sFields(Fields.UserLast)
clsUser.sUserPosition = sFields(Fields.UserPosition)

dUsers.Add(CInt(sFields(Fields.UserNum)), clsUser)
'rest of the code doesn't pertain to the question.

Next file Private Sub ReadActivityLog()

Try
Dim sr As New StreamReader(ujob.sWorkPath & ujob.sInputFile) ' \\mypath\activity.txt
Dim sData As String = String.Empty
Dim nLineCur As Long = 0

sData = sr.ReadToEnd

If sData.Length > 0 = True Then

Dim sLine As String
Dim sLines() As String = Split(sData, vbCrLf)

For Each sLine In sLines

nLineCur += 1
cLogLines = New StarsRecord
cLogLines.nInsertFlag = 1
Dim sFields() As String
sFields = Split(sLine, vbTab)
If sLine.Length > 0 Then
' rest of code just pulls in the data I want, uses a pre-written class to format and all that and it loops through the dictionary to find a match to pull in the data we need for that record.

Viewing all articles
Browse latest Browse all 27206

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>