StreamReader/Writer Problem

HungGar

Member
Joined
Jul 9, 2009
Messages
5
Programming Experience
Beginner
I've been googling this problem for a while and I can't find an answer, I'm writing a program that basically loads an entire text file into a streamreader variable, then reads this variable line by line and parses and writes a line into a new text 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.

I appreciate that there are probably better ways to do this, I'd like to learn how, but this is all I know so far. There are a few things with this code I already know I should fix, such as creating the new text file name, it's messy, I just don't know how. Hopefully someone can help point me in the right direction.

VB.NET:
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
 
Last edited by a moderator:
StreamWriter help said:
You must call Close to ensure that all data is correctly written out to the underlying stream.
Close method flushes remaining buffered content, closes the stream, and dispose the writer.
VB.NET:
finalFile.Close()
Similar with the reader, call Close method when done.
VB.NET:
textFile.Close()
 
One more question to fix one of the things that's been pestering me about this code. I save the original textpath by opening a filedialog and writing the path to a textbox, it grabs this and puts it in the variable, I'd like to create the new file in the same location as the old without replacing the file, and renaming it from a .txt to a .csv, ive done this by just copying the textpath and adding a .csv, so the final file is actually data.txt.csv, I'd like it to just be data.csv, is there a quick way to do this?
 
VB.NET:
Dim filePath As String = "C:\MyFolder\MyFile.txt"

filePath = IO.Path.ChangeExtension(filePath, ".csv")

MessageBox.Show(filePath)
Do you really need that TextBox? Can't you just get the FileName from the SaveFileDialog and put it straight into a variable?
 
No, but I do want people to see the filepath in the program, so I like the textbox. Also, the command you've given me changes the file extension but really what I'm doing is taking a "data.txt" and creating a new file in the same location called "data.csv". Does the code you showed me create a new file?

Thanks,
Martin
 
You have to write out the new file just as always. My code simply provides a simple way to get a new path that is exactly the same as the old path with just the file extension changed.
 
It worked, thanks again. One more question if you get the chance, if I wanted to create a file with a new name in that filepath, would it be a similar command?
 
Simpler/nicer version utilising more of the built in functionality for a more reliable result (like your simplistic CSV reader is ignoring the purpose of quote characters - this doesnt):
VB.NET:
Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click

Using tfp As New Microsoft.VisualBasic.FileIO.TextFieldParser(txtFilePath.Text)
  tfp.TextFieldType = FileIO.FieldType.Delimited
  tfp.SetDelimiters(",")
  tfp.HasFieldsEnclosedInQuotes = True

   Dim lineNumber as Integer = 1
   Dim sb as New StringBuilder
   While Not tfp.EndOfData
      Try
         Dim fields() as String = tfp.ReadFields()
         sb.AppendFormat("{0},{1},{2},{3},{4},{5}{6}", lineNumber, fields(3), fields(5), fields(7), fields(9), Path.GetFileName(fields(fields.Length-1)), Environment.NewLine)
         lineNumber+=1
      Catch ex As _
         Microsoft.VisualBasic.FileIO.MalformedLineException
         MsgBox("Line " & ex.Message & _
         "is not valid and will be skipped.")
     End Try
  End While

  File.WriteAllText(Path.ChangeExtension(txtFilePath.Text, ".csv"), sb.ToString())

  MessageBox.Show("Invoices Complete!")
End Sub
 
It worked, thanks again. One more question if you get the chance, if I wanted to create a file with a new name in that filepath, would it be a similar command?

Path.Combine( Path.GetDirectoryName(txtFilePath.Text), "YOUR NEW FILE NAME.TXT")
 
Back
Top