Hello everyone,

I have a txt file with data which I want to import to an excel file, by button click.
The first couple of lines of the txt file contains general information which I don't want to export.
The program should only read the lines which contains a word with the first character A and a thirth character G. When it does: every line with a match has to be saved in a variable and then exported to a excel file.

Can anybody give me a solution to do this?

Thanx in advanced.

Sneaky Pete

Can anybody give me a solution to do this?

Sounds like homework and nobody is going to do it for you. However, if you show us what you have so far we couuld offer help. What parts are you having problems with? Do you have your algorithm (pseudo-code) written out?

It's no homework, I'am looking for a way to make something easy.
I was just hoping for an example.

I could do it for you.

The point is not to do all the work for them but to help them learn to do it themselves.

Ok, I've tried to manage it, but it still dont work.

I've got a textfile which contains several lines. On top of the file there is general information, then some enters between and in the middle of the text file there is numeric information with in the end of the line a code which always starts with an A followed bij 3 digits.

The code should search for the lines that contains an A with 3 digits, and then get all these lines and display it, so I will only get the specific lines with numeric information.
I've tried it with a regular expression, but it won't work. I only get the general informatie at the top of the file.
In the past I did a little bit programming, but thats been a while.
Below is the code, can anybody give some held?

Thanx in advanced.

Sneaky Pete

Private Sub cmdClick_Click(sender As System.Object, e As System.EventArgs) Handles cmdClick.Click
    Dim FileName As String = "C:\test.txt"

    If System.IO.File.Exists(FileName) = True Then
        Dim objReader As New System.IO.StreamReader(FileName)
        Dim ChkTextLine As New Regex("[A]\d{3}")
        Dim TextLine As String

        Do While objReader.Peek() <> -1
            TextLine = TextLine & objReader.ReadLine() & vbNewLine
        Loop
        If ChkTextLine.IsMatch(TextLine) Then
            MsgBox(TextLine)
        Else
            MsgBox("File does not Exist")
        End If
    End If
End Sub

Hello Pete, the is not completely right. You must check inside the loop the condition if match and not outside how you are doing.
Test the version below. I edited here so it could be a mistake. I am a programmer especialist but currently not use Visual Basic. So, anyway hope it helps you.

Private Sub cmdClick_Click(sender As System.Object, e As System.EventArgs) Handles cmdClick.Click
    Dim FileName As String = "C:\test.txt"
    If System.IO.File.Exists(FileName) = True Then
        Dim objReader As New System.IO.StreamReader(FileName)
        Dim ChkTextLine As New Regex("[A]\d{3}")
        Dim TextLine As String
        Dim MatchingLines As String
        Do While objReader.Peek() <> -1
             TextLine = objReader.ReadLine()             
             If ChkTextLine.IsMatch(TextLine) Then
                 MatchingLines = TextLine & vbNewLine            
            End If
        Loop
        //do some output here, probably MsgBox(MatchingLines)
    Else
            MsgBox("File does not Exist")
    End If
End Sub

Thanx Paul,

One step foreward thans to you.
This will give me a single line of information that I want.
Thing is, it has to read multiple lines.
Now I only get the last line, not the lines above.

Hope you can help

Ok, that only needs concatenation with the previous stored value of MatchingLines. Here it goes:

Private Sub cmdClick_Click(sender As System.Object, e As System.EventArgs) Handles cmdClick.Click
    Dim FileName As String = "C:\test.txt"
    If System.IO.File.Exists(FileName) = True Then
        Dim objReader As New System.IO.StreamReader(FileName)
        Dim ChkTextLine As New Regex("[A]\d{3}")
        Dim TextLine As String
        Dim MatchingLines As String
        Do While objReader.Peek() <> -1
             TextLine = objReader.ReadLine()             
             If ChkTextLine.IsMatch(TextLine) Then
                 MatchingLines = MatchingLines & TextLine & vbNewLine            
            End If
        Loop
        //do some output here, probably MsgBox(MatchingLines)
    Else
            MsgBox("File does not Exist")
    End If
End Sub

Great!, thanx for your help!

What is the solution when I only want all the strings in the txt file that match the ChkTextLine?
So I get the result A000, A001, A005, etc?

Thanx in advanced

Be more accurate on the answer because I think the code is doing that right now.

Be a part of the DaniWeb community

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