Reading specific line in textfile(skip some lines)

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

45 Replies - 7156 Views - Last Post: 26 October 2011 - 10:23 PM Rate Topic: -----

#1 yusmaiza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 15-September 11

Reading specific line in textfile(skip some lines)

Posted 17 October 2011 - 07:04 PM

Hello, I'm currently use visual basic 2008. The problem is I have try this code but,it produce no output. There is no problem with the code I guess. Here is my code

 Dim i As Integer = 0
        Dim line As String

      
        'Dim reader As StreamReader = New StreamReader("C:\drill.txt")

        Dim fileName As String = "C:\drill.txt"
        '- Set the line counter
        Dim lineNumber As Integer = 0

        '- Open the text file/stream
        Dim sr As StreamReader = New StreamReader(fileName)

        '- Initialize name variables
        Dim x As String = ""
        Dim temp As String = ""
        Dim textline As String = ""

        For Each line In File.ReadAllLines(fileName)

            Dim parts As String() = line.Split(New Char() {","c})
            Dim part As String
            For Each part In parts

                Do

                    textline = sr.ReadLine()
                    
                    If textline <> Nothing Then

                        Try

                            If parts(1).Contains("T") Then
                                If part = "Pin" Then
                                    If textline = parts(1) Then
                                        Using sw As StreamWriter = File.CreateText("C:\test1.txt")
                                            'Dim objWriter As New System.IO.StreamWriter("C:\test1.txt")
                                            'objWriter.WriteLine(parts(1))
                                            sw.WriteLine(parts(1))

                                            Do
                                                textline = sr.ReadLine()
                                                sw.WriteLine(textline)
                                            Loop Until textline = "T12"
                                            sw.Flush()
                                        End Using
                                    End If
                                End If
                            End If
                        Catch ex As Exception
                            MsgBox("No File Exist!")

                        End Try
                    Else
                        Exit Do

                    End If

                Loop Until textline = "T12"
            Next
            i += 1

        Next

        MsgBox("OK")



Is This A Good Question/Topic? 0
  • +

Replies To: Reading specific line in textfile(skip some lines)

#2 DimitriV  Icon User is online

  • Das kann nicht sein
  • member icon

Reputation: 549
  • View blog
  • Posts: 2,645
  • Joined: 24-July 11

Re: Reading specific line in textfile(skip some lines)

Posted 17 October 2011 - 09:05 PM

To me it appears as though you aren't setting the value of the string part anywhere and this could be making your result null.
Was This Post Helpful? 0
  • +
  • -

#3 yusmaiza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 15-September 11

Re: Reading specific line in textfile(skip some lines)

Posted 17 October 2011 - 10:56 PM

View PostjimmyBo, on 18 October 2011 - 12:05 PM, said:

To me it appears as though you aren't setting the value of the string part anywhere and this could be making your result null.

I have try again the same code. But the result is the code can create Ppin2.txt file but there is no MsgBox appear and the file doesn't contain any data.. :helpsmilie:

View Postyusmaiza, on 18 October 2011 - 01:54 PM, said:

View PostjimmyBo, on 18 October 2011 - 12:05 PM, said:

To me it appears as though you aren't setting the value of the string part anywhere and this could be making your result null.

I have try again the same code. But the result is the code can create Ppin2.txt file but there is no MsgBox appear and the file doesn't contain any data.. :helpsmilie:

Im sorry..i mean test1.txt not Ppin2.txt
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Reading specific line in textfile(skip some lines)

Posted 17 October 2011 - 11:22 PM

Ok alot of complication there, but what were you trying to do? can you explain it so may be we may have better way?
What the meaning of having nested loops inside loops,and what this loop doing anywhay?
For Each line In File.ReadAllLines(fileName)
Was This Post Helpful? 0
  • +
  • -

#5 yusmaiza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 15-September 11

Re: Reading specific line in textfile(skip some lines)

Posted 17 October 2011 - 11:36 PM

View Postsmohd, on 18 October 2011 - 02:22 PM, said:

Ok alot of complication there, but what were you trying to do? can you explain it so may be we may have better way?
What the meaning of having nested loops inside loops,and what this loop doing anywhay?
For Each line In File.ReadAllLines(fileName)

Ok. Let me explain step by step..
1. I want to read the text file (drill.txt) line by line
2. Then, If the text file reach the text line that contain "T" and "Pin" (which is in line (M47,T04,=,Scanner,Pin), the text file will create another new text file name test1.txt and write the text file starting from line "T (value)" the value can be any value. But for this drill.txt, the value is T04.
3. here is the input data or drill.txt file

M47,T25,=,39,Mil,Ctr
M47,Probe,Sockets
M47,0.0312in,Long,Flute
M47,T04,=,Scanner,Pin
M47,2.25mm,Long,Flute
M47,T11,=,Small,Tesjet,Plate,Countersink
M47,0.25in.,Long,Flute
M47,T01,=,100,Mil,Ctr
M47,Testjet,Probe,Sockets
M47,1.75mm,Long,Flute
T01
X-04.8753Y+09.3278
X-04.8753Y+08.9341
X-04.8753Y+08.8357
X-04.9125Y+08.2940
T04
X-07.2275Y+01.9611
X-07.5775Y+01.9611
X-07.2275Y+02.1111
X-07.5775Y+02.1111
X-07.2275Y+02.2611
X-07.5775Y+02.2611
X-07.2275Y+02.4111
X-07.5775Y+02.4111
X-07.2275Y+02.5611
T12
X-07.5775Y+03.4611
X-07.2275Y+03.6111
X-07.5775Y+03.6111
X-07.2275Y+03.7611
X-07.5775Y+03.7611
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Reading specific line in textfile(skip some lines)

Posted 17 October 2011 - 11:58 PM

Then this can be simple, if you want to write the text from line containing T & Pin to the end, then just:
Dim sr As StreamReader = New StreamReader("C:\drill.txt")
        Dim sw As StreamWriter
        While sr.Peek <> -1 'try while you have not reached end of the line
            line = sr.ReadLine() ' get each line and store it
            If line.Contains("T") AndAlso line.Contains("Pin") Then ' if the line contains 
                Dim allText As String = sr.ReadToEnd() 'read the remaining part of the file
                'now write line and allText in another file
                sw = File.CreateText("C:\test1.txt")
                sw.WriteLine(line)
                sw.WriteLine(allText)
                sw.Flush()
                Exit While ' noo need to loop again, work done
            End If
        End While

Was This Post Helpful? 1
  • +
  • -

#7 yusmaiza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 15-September 11

Re: Reading specific line in textfile(skip some lines)

Posted 18 October 2011 - 12:11 AM

View Postsmohd, on 18 October 2011 - 02:58 PM, said:

Then this can be simple, if you want to write the text from line containing T & Pin to the end, then just:
Dim sr As StreamReader = New StreamReader("C:\drill.txt")
        Dim sw As StreamWriter
        While sr.Peek <> -1 'try while you have not reached end of the line
            line = sr.ReadLine() ' get each line and store it
            If line.Contains("T") AndAlso line.Contains("Pin") Then ' if the line contains 
                Dim allText As String = sr.ReadToEnd() 'read the remaining part of the file
                'now write line and allText in another file
                sw = File.CreateText("C:\test1.txt")
                sw.WriteLine(line)
                sw.WriteLine(allText)
                sw.Flush()
                Exit While ' noo need to loop again, work done
            End If
        End While


Thanks a lot for ur help. But the problem is, I want to write new the file starting with "T" (value) until it hit the next "T". In this case, the next "T" is "T12". Means that, I want like below output. Actually, the T value can change according to the text file.. dats why, I can't directly read the text line from T04 to T12. Hope u understand
T04
X-07.2275Y+01.9611
X-07.5775Y+01.9611
X-07.2275Y+02.1111
X-07.5775Y+02.1111
X-07.2275Y+02.2611
X-07.5775Y+02.2611
X-07.2275Y+02.4111
X-07.5775Y+02.4111
X-07.2275Y+02.5611
T12

Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Reading specific line in textfile(skip some lines)

Posted 18 October 2011 - 12:55 AM

Ya sure, instead of reading the whole text and exit, continue reading the other lines one by one and checking if it contains T. For example, I can set a variable to check if now, I am working on finding the next value or not. Something like:
Dim next As Boolean = False
Dim sr As StreamReader = New StreamReader("C:\drill.txt")
        Dim sw As StreamWriter
        While sr.Peek <> -1 'try while you have not reached end of the line
            line = sr.ReadLine() ' get each line and store it
            If line.Contains("T") AndAlso line.Contains("Pin") AndAlso next = False Then ' if the line contains and we have not start writing yet
                sw = File.CreateText("C:\test1.txt")
                sw.WriteLine(line)
                next = true
         Else If line.Contains("T") = false AndAlso next = True Then 'if we have our file and writing in it
                sw.WriteLine(line) 'write the line
         Else If line.Contains("T") AndAlso next = True Then  
                sw.WriteLine(line) 'write the current line(if you need it)
                sw.Flush()      
                Exit While ' noo need to loop again, work done
           End If
        End While

Look at comments and see if that is what you need
Was This Post Helpful? 1
  • +
  • -

#9 yusmaiza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 15-September 11

Re: Reading specific line in textfile(skip some lines)

Posted 18 October 2011 - 01:06 AM

View Postsmohd, on 18 October 2011 - 03:55 PM, said:

Ya sure, instead of reading the whole text and exit, continue reading the other lines one by one and checking if it contains T. For example, I can set a variable to check if now, I am working on finding the next value or not. Something like:
Dim next As Boolean = False
Dim sr As StreamReader = New StreamReader("C:\drill.txt")
        Dim sw As StreamWriter
        While sr.Peek <> -1 'try while you have not reached end of the line
            line = sr.ReadLine() ' get each line and store it
            If line.Contains("T") AndAlso line.Contains("Pin") AndAlso next = False Then ' if the line contains and we have not start writing yet
                sw = File.CreateText("C:\test1.txt")
                sw.WriteLine(line)
                next = true
         Else If line.Contains("T") = false AndAlso next = True Then 'if we have our file and writing in it
                sw.WriteLine(line) 'write the line
         Else If line.Contains("T") AndAlso next = True Then  
                sw.WriteLine(line) 'write the current line(if you need it)
                sw.Flush()      
                Exit While ' noo need to loop again, work done
           End If
        End While

Look at comments and see if that is what you need


I try to use that code but, blue line appears under next word. its written that keyword is not valid as an identifier..Do I have to change the keyword?
Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Reading specific line in textfile(skip some lines)

Posted 18 October 2011 - 01:13 AM

Sure change it to another variable name(valid identifier). My bay I didnt realize it, sorry I write code not in IDE
Was This Post Helpful? 0
  • +
  • -

#11 DimitriV  Icon User is online

  • Das kann nicht sein
  • member icon

Reputation: 549
  • View blog
  • Posts: 2,645
  • Joined: 24-July 11

Re: Reading specific line in textfile(skip some lines)

Posted 18 October 2011 - 01:21 AM

Thats right - next can not be used as a variablr as it will "confuse" the ide.
Was This Post Helpful? 1
  • +
  • -

#12 yusmaiza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 15-September 11

Re: Reading specific line in textfile(skip some lines)

Posted 18 October 2011 - 01:24 AM

View PostjimmyBo, on 18 October 2011 - 04:21 PM, said:

Thats right - next can not be used as a variablr as it will "confuse" the ide.

So what keyword should I change? That have the same function as next?
Was This Post Helpful? 0
  • +
  • -

#13 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Reading specific line in textfile(skip some lines)

Posted 18 October 2011 - 01:27 AM

Change the word next to another name. Like nextStep or so. In short in that piece of my code, remove every "next" word and put nextStep
Was This Post Helpful? 1
  • +
  • -

#14 yusmaiza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 15-September 11

Re: Reading specific line in textfile(skip some lines)

Posted 18 October 2011 - 01:37 AM

Why does the output still write "M47,T04,=,Scanner,Pin" at the first line?
I want to write T04 as the first line.Its because "T04" is located the same line as "Pin"..I don't know how to explain..huhuu :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#15 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Reading specific line in textfile(skip some lines)

Posted 18 October 2011 - 01:47 AM

Now post your updated code, if you want the first line to write only that, then split it, something like:
'in this part, you will have some changes:
            If line.Contains("T") AndAlso line.Contains("Pin") AndAlso next = False Then ' if the line contains and we have not start writing yet
                sw = File.CreateText("C:\test1.txt")
dim lineP() As String = line.Split(",")
                sw.WriteLine(lineP(1))
                next = true

The same you will do for the last condition if you need only one part of the line
Was This Post Helpful? 1
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »