Sorry, very casual programmer, haven't coded in many years ..(primarily in VB6) I have googled, and read .net details on each file class, seen lots of examples, but have no idea how to get modified output to match source formatting.
I have existing file: (This is 2 lines of file: Header, and 1 detail)
"BATCHNBR","JOURNALID","TRANSNBR","DESCOMP","ROUTE","ACCTID","COMPANYID","TRANSAMT","TRANSQTY","SCURNDEC","SCURNAMT","HCURNCODE","RATETYPE","SCURNCODE","RATEDATE","CONVRATE","RATESPREAD","DATEMTCHCD","RATEOPER","TRANSDESC","TRANSREF","TRANSDATE","SRCELDGR","SRCETYPE","COMMENT","VALUES","PROCESSCMD"
"000004","00001","0000000020","",0,"60000000000000000ZZZZZ","GMN",817.31,0,"2",817.31,"USD","SP","USD",20180511,1,0,"1","1","100123","10444-Kampwerth, Brooke E",20180511,"UP","SC","",0,0
I have the sub complete but it outputs each field on it's own line, and without double quotes. TextFieldParser does not have write methods. The msgbox embeds indicate I have replaced the data correctly,
I am Truncating a certain field(if Required), and The msgbox embeds indicate I have replaced the data correctly.
I just need to output in the same strings as source.
Private Sub cmdProcess_Click(sender As Object, e As EventArgs) Handles cmdProcess.Click
Using sw = New IO.StreamWriter(NewFile)
Using MyReader As New FileIO.TextFieldParser(oFile)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
'MyReader.HasFieldsEnclosedInQuotes
'Dim Expression As String
Dim Replacement As String
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
If Strings.Right(currentField, 3) = "ZZZ" Then
'MsgBox(MyReader.LineNumber & " " & currentField)
Replacement = Strings.Left(currentField, 6)
'MsgBox(MyReader.LineNumber & " " & Replacement)
currentField = Replacement
End If
sw.WriteLine(String.Join(",", currentField))
Next
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
End Using
End Using
MsgBox("Done")
Dispose()
Application.Exit()
End Sub
If I replace the currentfield with currentrow in sw.writeline, it the outputs the same line 27 time (27 rows in source), then does the next line. And without double quotes around each field.
Current Output: (Single Spaced)
BATCHNBR
JOURNALID
TRANSNBR
DESCOMP
ROUTE
What am I missing .. ??
You need to move the WriteLine outside of For...Next loop so it only writes one line per row. I used a list to accumulate the strings in each row. The the list is used in the Join.
Dim lstRow as New List(Of String)
For Each currentField In currentRow
If Strings.Right(currentField, 3) = "ZZZ" Then
'MsgBox(MyReader.LineNumber & " " & currentField)
Replacement = Strings.Left(currentField, 6)
'MsgBox(MyReader.LineNumber & " " & Replacement)
currentField = Replacement
End If
lstRow.Add(currentField)
Next
sw.WriteLine(String.Join(",", lstRow))
lstRow.Clear()