Hello,

I have a question about converting text file elements to arrays in VB. I am generally not a .Net programmer, and have really basic question about parsing array elements in VB.

I have a text file that is formatted as the following...

"Item1","Item2","Item3"
"LineItem1","LineItem2","LineItem3"

What I would like to do is open this file and add the Items into an array element so that the array is formatted as a String Array

itemArray(0)= Item1
itemArray(1)= Item2
itemArray(2)= LineItem1
itemArray(3)= LineItem2
... and so on...

I have used some coding examples that I found earlier...

Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim strFileName As String

        OpenFileDialog1.Title = "Open Text File"
        OpenFileDialog1.Filter = "Text Files|*.txt"

        Dim DidWork As Integer = OpenFileDialog1.ShowDialog()

        If DidWork = DialogResult.Cancel Then
            MsgBox("Cancled")
        Else
            strFileName = OpenFileDialog1.FileName
            'Dim tempString As String = IO.File.ReadAllLines(strFileName)
            Dim tempString As String = IO.File.ReadAllText(strFileName)
            Dim x As Integer = tempString.Length - 1 '//Array Size Integer 
            Dim itemArray(x) As String   '//Array that the text file is stored into. 
            Dim i As Integer = 0  '//Counter Integer

            For Each myChar As Char In tempString
                itemArray(i) = myChar
                i += 1
            Next

            MsgBox(strFileName + " Opened Sucessfully")

            RichTextBox1.Lines = itemArray
        End If

Which parses the array elements of each character onto a single line, but as you can see I am searching for a string element per line to be contained in the file. I am not sure how to make that happen in VB. Also you will notice that the text file is , delimited in between the quote's which is also causing me some trouble.

Oh yes, and one more question. What is the difference between the
IO.File.ReadAllLines(strFileName)
IO.File.ReadAllText(strFileName)
function?

Thanks :)

Last question first. ReadAllLines reads the entire file into a stringt array where each element is one line of the file. ReadAllText reads the entire file into one long string.

First question next

Dim lines As String() = IO.File.ReadAllLines("d:\test.txt")
Dim data(lines.Count - 1)
For i As Integer = 0 To lines.Count - 1
    Dim line As String = lines(i).Replace("""", "")
    data(i) = line.Split(",")
Next

if test.txt contains

"Item1","Item2","Item3"
"LineItem1","LineItem2","LineItem3"
"NetxItem1","NextItem2","NextItem3"

then

data(0)(0) = "Item1"
data(2)(2) = "NextItem2"
etc

This assumes

1) all lines contain 3 fields
2) no fields contain a comma

If you can't rely on #2 then you have to massage line to replace the comma with a separator that does not appear in the data.

Actually, the lines can have a variable number of fields. If the fields contain embedded commas then I suggest you do the replacement as

line = line.Replace(""",""",newdelim)    'replace "," with new delimiter
line = line.Replace("""","")             'remove leading and trailing "

Juse set newdelim to some character not used in the text

Hey thanks! :)

The following code was exactly what I was going for :

Dim lines As String() = IO.File.ReadAllLines("d:\test.txt")
    Dim data(lines.Count - 1)
    For i As Integer = 0 To lines.Count - 1
    Dim line As String = lines(i).Replace("""", "")
    data(i) = line.Split(",")
    Next

This was a machine generate file that I wanted to quickly parse so its formatting is consistent. So I was able to get this code working enough for my testing purpose. The above block with some modifications did the trick.

There are not elements that contain ,'s that are not delimiting the field. The formatting of this file is pretty strict and easy to maneuver through.

I moved it to C# to make the processing of the file a little bit faster.

Not the best code but it got my file parsed in a way that I could work with it. Its enough of a format now where I can inspect the elements and make comparisons with some expressions to extrapolate the information I am looking for here. The double element array works great for a sanity check and keeping the data lined up by line number.

String openedFile = openFileDialog1.FileName;
                int i = 0;
                try 
                {
                
                    using (myStream) 
                    {
                        //string text = File.ReadAllText(openedFile);
                        //richTextBox1.Text = text;
                        
                        foreach (String lineItem in File.ReadAllLines(openedFile))
                        {
                            //string text = File.ReadAllText(openedFile);
                            String[] splitItem = lineItem.Split(',');
                            foreach (String data in splitItem)
                            {

                                Console.WriteLine("{0}:{1}", i, data);
                                //code here ...

                            } //end nested for each

                            i++;
                        }//end for each
                        
                        MessageBox.Show("I am done processing the split");

                    }  //end using
                }//end try
                catch (Exception ex) 
                {
                    MessageBox.Show("Error: Could not read file: " + ex.Message);
                }//end catch
            } //end if

You can see that I used pretty much the same format from your previous code block, but I added a few things. I guess I will have to learn more about streams and locking threads. My .Net skills are a little rusty (IE non existent). :D

Thanks for giving me the push I needed to get started.

I could have mentioned this earlier as well, but because the fields are comma delimited AND quoted, they are suitable for reading as a data source via ADO. This would allow you to read the records as if they were in a database. The ADO interface would parse the fields for you. That method looks like

Dim con As New ADODB.Connection
Dim rec As New ADODB.Recordset

con.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\;Extended Properties=""text;HDR=NO;FMT=Delimited""")
rec.Open("select * from test.txt", con, CursorTypeEnum.adOpenForwardOnly)

Do Until rec.EOF
    Debug.WriteLine(rec(0).Value & " " & rec(1).Value & " " & rec(2).Value)
    rec.MoveNext()
Loop

rec.Close()
con.Close()
This question has already been answered. Start a new discussion instead.