Hi Dw.

I'm trying to read a text file. This text file has a multiply lines and what I want is that I want to separate each line or should I say I want to split each line (chop it) so that I will be able to assign these fields to variables using the array.

What I first did is to get the lines then for each line I need to break it. The split I'm using the "*" as a separator.

Each line will produce/has 13 fields so I want to get these as a variable. Here is just 1 line example of how each line is:


So that is just one line. There are quite a number of lines in a file with this format but has different data as to this but the format is the same. Ow also some lines ends with # and also there are some blank lines as well within the file.

This is what I have:

 If My.Computer.FileSystem.FileExists("C:\test.txt") Then
 Dim test As String = IO.File.ReadAllText("C:\test.txt")
 Dim lines() As String = Split(test, vbCrLf)
 For Each line As String In lines
 ' Now I have the collection of lines now
 ' What I want to achieve is to split each
 ' Line into array so that I can use some
 ' Thing like MyArray(0), MyArray(1) etc
 ' This is what I have but I don't want it
 ' Like this.

  Dim words As String() = line.Split(New Char() {"*"})
  Dim data As String
  For Each data In words
  data.Trim({"#"c, "D1"c, "D2"c})
  If data = "" Then

  ' For now I just use the listbox to display.
  ListBox1.Items.Add(data.Trim({"#"c, "D1"c, "D2"c})
   End If

This only uses one variable which is data but what I want is to use many variables so I want to maybe add these to an array.

Recommended Answers

All 12 Replies

I'm not clear on what you want. Perhaps you can post a sample input file with all of the possible variants and an example of how that data will look when displayed after filtering. That will probably be clearer than an explnation. In the mean time you can simplify your code considerably. If you use a ListView in Details mode (with 15 columns) you can display your file as follows:

Dim file() As String = System.IO.File.ReadAllLines("D:\temp\test.txt")

For Each line As String In file
    Dim fields() As String = line.Split("*")
    lvwFields.Items.Add(New ListViewItem(fields))

or even simpler

For Each line As String In file
    lvwFields.Items.Add(New ListViewItem(line.Split("*")))

@Jim. Well at the time of reply to this post I used a cellphone. But basically what I want is a bit similar to your first example where if I were to use the ListView with 15 columns I would have done it like that but now what I have is that I have the exactly 15 variables for the example you provided sake. Now I need a way to do this:

 Dim MyFirstVariable As String
 ' This variable should contain field D1
 ' To achieve that is something like this
 MyFirstVariable = Fields(0)
 ' That because Fields(0) will contain the
 ' First field before the * which is where
 ' Each field will be broken.
 Dim MySecondVariable As String
 ' This variable should contain the field
 ' X584635 and that is retrieved like this
 MySecondVariable = Fields(1)
 Dim MyThirdVariable As String
 ' This Variable should contain the field
 ' test.one field and that is retrieved like
 MyThirdVariable = Fields(2)
 ' And so on to 15.

The reason for this is that I want to add this to a database but before its added to a database I need to take Fields(1) and I have a function that tries to open a database record that contains this data, if its finds its then all the database records within that are retrieved so that they will be matched with the record that had been read from the file. Here is how that is done.

Note that MySecondVariable contains X584635 of which is a field read from text file. Also this variable is declared as global variable. This is an example of use of this field for database.

 Dim da As New OleDbDataAdapter(("select * from data where sID ='" & Trim(MySecondVariable) & "'"), conn)
 Dim dt As New DataTable
 If dt.Rows.Count > 0 Then
 ' This means that this data has been
 ' Added to database before so I will
 ' Just retrieve all the fields so that I
 ' Match it with the new data.

 Dim tempMyFirstVariable As String
 ' I will only use one variable
 ' But the process is the same with all
 ' The fields I have many variables but
 ' Will just use one here.
 tempMyFirstVariable = dt.Rows(0).Item("name") & ""
 ' Now let me check if they match
 If tempMyFirstVariable = MyFirstVariable Then
 ' This match 
 ' This doesn't match
 End If
 End If

So I think now you have an idea of how I want to use these fields. I could have just inserted into a database but I wanted to check if I already have the record on my database that's has the identify in this example its X584635 and if there is I want to retrieve that to tempVariables so that all fields will be compared with the new data accordingly if there is one that does not match I then simply update the database with the new data if its doesn't find the record with X584635 it will then proceed to registration of the new data.

Hope this is clear now. Remember that I have many lines but I'm sure the system will handle that as its process each line the variable will be overwritten by the next line fields when done the verification and database insertion to the end of file.

Clear as mud. Why do you need to put the values into discrete variables when they are already in discrete array elements? I also notice that you say

Each line will produce/has 13 fields so I want to get these as a variable.

but your sample line has 15 fields.

Lol @Jim I used the 15 as per the sample you provided where u said suppose I had a ListView with 15 columns, so I didn't want to course confusion. But in my case its 13. As you can see on the sample line I provide on my first post. If you count all the fields within the * they are 13.

Sorry for confusion.

Here I have used only your string in a textbox to sort your conditions out. Also notice that string.trim only trims one character.
So I successivly remove the parts you don'want and at the end move it into a listbox. You can than use the items in the listbox as your variables via selected item.

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Text = "D1*X584635*test.one*12643820000000000?*D2*65496321476=2416317950000000000*(Y@12345)*Date:*05*April*2016*Time:*17*01*32 #"
    End Sub
    Private Sub Work(sender As Object, e As EventArgs) Handles Button1.Click
        Dim modifiedString As String = Replace(TextBox1.Text, "D1", "")
        TextBox1.Text = modifiedString
        modifiedString = Replace(TextBox1.Text, "D2", "")
        TextBox1.Text = modifiedString
        TextBox2.Text = TextBox1.Text.Trim({"#"c})
        TextBox2.Text = TextBox2.Text.Replace("**", "*")
        If TextBox2.Text <> String.Empty Then
            TextBox2.Text = TextBox2.Text.Remove(0, 1)
        End If
        Dim parts() As String = TextBox2.Text.Split("*"c)
        For Each item As String In parts
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    End Sub
End Class
  1. D1
  2. X584635
  3. test.one
  4. 12643820000000000?
  5. D2
  6. 65496321476=2416317950000000000
  7. (Y@12345)
  8. Date:
  9. 05
  10. April
  11. 2016
  12. Time:
  13. 17
  14. 01
  15. 32

@Minimilist. Thanks, but that's not how I want it. The sample code I provided on my first post does that except those two top textboxes. The app I'm developing does not or won't be accessed by human so it won't have those tools like textboxes, listview or so.

I have a file that has many lines its a text file so what I'm trying to do for example, Please don't be confused by this its just an example I'm making for you to understand this let's say my text file is test.txt and its have 3 lines and each line has 13 fields. Now what I want is for each line have these fields read and assigned to variables correctly so that I can call these variables when I'm inserting this data to a database.

@Jim Oops I think I made a mistake when making that sample line but its ok, so that you won't be confused let's keep on working with 15 fields. I will correct it to the fields I have. As an example let's work with that 15.

Your biggest problem is that you've already decided how you want to do it, but how you want to do it is wrong. Why are you so wedded to the idea of discrete variables? It would also help to know if you are using SQLClient or OLEDB.

I'm using at the moment MS Access.

What is the right way? Please guide me.

O.K instead of textboxes you declare some strings. Instead of a listbox(see your line 20 in the original post) you use a list(T) of string and proceed(and I use my post but Jim's is doing the same) but hold the original string in memory. Now you get your field from the database and see if that string from the database is contained in the original string. If it is you loop through your list and upgrade the record, if it is not you loop through the list and add the record to the database.

Thanks everyone. Just woke up this morning and wrote this plan down then I managed to fix this. Here is what I wanted and how I accomplished it. I have 2 declarations that reads the file that because what I found was that in order to do what I wanted I shouldn't have used the ReadAllText but since the variable declared within this which is test works in getting or looping through all lines in a text file I than declared another variable I called cs and this is how I declared it.

Dim cs As New System.IO.StreamReader("c:\test.txt") then under my For Each line As String In lines I put this.

 ' Firstly because my text file also has some blank lines I need to deal with that first.

 Dim cline As String = cs.ReadLine
 If cline <> String.Empty Then
 Dim sArry(12) As String
 Dim xz As String() = cline.Split("*")
 sArry = xz
 ' Now I can assign the variables correctly.
 ' For this demonstration use one at a time
 ' So that you will see what I wanted.
 ListBox1.Items.Add("sArry(0) has: " & sArry(0))
 ListBox1.Items.Add("sArry(1) has: " & sArry(1))
 ' And so on.

That's how I wanted but thanks for your efforts guys. Will mark this thread answered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.