I've been googling this problem for a while and I can't find an answer, I'm writing a program that basically looks at a text file, reads a line, parses it out, and writes to another file. I'm VERY new and my knowledge is mostly self-taught, but I can't seem to get out of this one. It works for smaller files, but it appears to reach a limit in characters at some point because in a file of 900 lines, it stops writing about halfway through 800 and there are no errors, the program actually completes and the message box pops up.
here is the code, hopefully someone can help. I'm sure there are a lot of problems with how i wrote this.
Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click
Dim IndexValues As String()
Dim TextPath As String = txtFilePath.Text.ToString
Dim TextFile As StreamReader
Dim FinalFile As StreamWriter
Dim GarbagePath As String()
Dim i As Integer
Dim ID As Integer = 1
If File.Exists(TextPath) Then
TextFile = File.OpenText(TextPath)
End If
FinalFile = File.CreateText(TextPath & ".csv")
FinalFile.WriteLine("ID,Company Number,File Cabinet,Invoice Number,Supplier Code,Filename")
Do Until TextFile.Peek() = -1
IndexValues = TextFile.ReadLine().Split(",")
GarbagePath = IndexValues(IndexValues.GetUpperBound(0)).Split("")
For i = 0 To IndexValues.GetUpperBound(0)
IndexValues(i) = Replace(IndexValues(i), Chr(34), "")
GarbagePath(GarbagePath.GetUpperBound(0)) = Replace(GarbagePath(GarbagePath.GetUpperBound(0)), _
Chr(34), "")
Next
FinalFile.WriteLine(ID & "," & IndexValues(3) & "," & IndexValues(5) & "," & IndexValues(7) & "," & IndexValues(9) _
& "," & GarbagePath(GarbagePath.GetUpperBound(0)))
ID += 1
Erase IndexValues
Loop
MessageBox.Show("Invoices Complete!")
End Sub
Comments
There are some other issues in your code that I noticed also. If you want, I've written a slightly revised version below...
For this code you will want to import System.IO
[code]
Dim TextPath As String = "C:Temp.csv" ' replace me with your control's value
If (File.Exists(TextPath) = False) Then Return ' If no file then don't continue
Dim IndexValues As String()
Dim GarbagePath As String ' Don't really need this...
Dim ID As Integer = 1
Dim TextFile As StreamReader = File.OpenText(TextPath)
Dim FinalFile As StreamWriter = File.CreateText(TextPath & ".result.csv") ' output file
FinalFile.WriteLine("ID,Company Number,File Cabinet,Invoice Number,Supplier Code,Filename") ' header
While (TextFile.EndOfStream = False) ' Was there a reason you were using peek?
IndexValues = TextFile.ReadLine().Replace(Chr(34), String.Empty).Split(",") ' Is there a reason to not strip " from the whole line?
If (IndexValues.Length < 10) Then Continue While ' This will catch blank lines
GarbagePath = Path.GetFileName(IndexValues.Last()) ' System.IO already has this functionality built in...
FinalFile.WriteLine(ID & "," & IndexValues(3) & "," & IndexValues(5) & "," & IndexValues(7) & "," & IndexValues(9) & "," & GarbagePath)
ID += 1
End While
TextFile.Dispose() ' remember to always dispose of these
FinalFile.Flush() ' This is probably where your problem was - you have to flush these little guys
FinalFile.Dispose()
MessageBox.Show("Invoices Complete!")
[/code]