Results 1 to 10 of 10

Thread: [RESOLVED] Drop text depending on type?

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Resolved [RESOLVED] Drop text depending on type?

    hello all,

    i have a little loop what reads in a text file
    the text layout is

    12 <---- drop
    1212121 <--- keep
    text <-- drop
    blank line <---- drop
    12 <---- drop
    1212121 <--- keep
    text <---- drop
    blank <---- drop
    12 <---- drop
    1212121 <--- keep
    text <---- drop
    blank <---- drop

    i just dont know how to go about dropping individual lines since the Isnumeric function is redundent since i can delete some numeric values but not all.

    i cant compear it against other text either because the text files change every time

    the code for my loop is this atm

    Code:
    Using ofd As New OpenFileDialog
                ofd.Filter = "all Files|*.srt"
                ofd.Title = "Select File"
                ofd.InitialDirectory = lbldir.Text
                If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    Dim fileReader As String
                    Dim srFileReader As System.IO.StreamReader
                    Dim sInputLine As String
    
                    ListBox1.Items.Clear()
                    lbltimecodes.Text = "0"
                    fileReader = ofd.FileName
                    srFileReader = System.IO.File.OpenText(fileReader)
                    lbldir.Text = fileReader.ToString
                    sInputLine = srFileReader.ReadLine()
    
                    Do Until sInputLine Is Nothing
                        ListBox1.Items.Add(sInputLine)
                        sInputLine = srFileReader.ReadLine()
                    Loop
    
                    lbltimecodes.Text = ListBox1.Items.Count
                    srFileReader.Close()
                    errormsg = True
                End If
    now this code does what i want it to do but i also want the same text file to be stripped of the data i mentioned before so we have the origional and the stripped to compear

    any help with this would be great

    thanks in advanced to any help i recive
    beattie282

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Drop text depending on type?

    Use something like this:
    vb.net Code:
    1. Dim data As New List(Of String)
    2.  
    3. Using reader As New StreamReader("file path here")
    4.     Do Until reader.EndOfStream
    5.         Dim line = reader.ReadLine()
    6.  
    7.         If LineShouldBeKept(line) Then
    8.             data.Add(line)
    9.         End If
    10.     Loop
    11. End Using
    Your logic for testing each line, whatever that might be, goes in the LineShouldBeKept method. You haven't explained to us what the criteria for keeping or discarding a line are so we can't tell you what that code should be.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Re: Drop text depending on type?

    Thanks for your reply

    i have implimented your code ish ive got it stripping text from the list but i have run into another problem.

    Code:
     
                    Do Until sInputLine Is Nothing
                        ListBox1.Items.Add(sInputLine)
                        sInputLine = srFileReader.ReadLine()
                        If IsNumeric(sInputLine) Then
                            lststrippedtext.Items.Add(sInputLine)
                        End If
                    Loop
    This is what i did but this just simply removes text and since the numbers i need to pull have --> in the middle it just gets treated as text

    09876 --> 09876 but i want this to be pulled

    also their are other numbers in the document. hopefuly i am clear on what i mean if notthen i am sorrry

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Drop text depending on type?

    What do you mean by this one ?

    09876 --> 09876 but i want this to be pulled
    Please mark you thread resolved using the Thread Tools as shown

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Re: Drop text depending on type?

    Quote Originally Posted by danasegarane View Post
    What do you mean by this one ?
    in my text file which i need to read i have several peices of info in there which includes

    1 <-- line number
    09876 --> 09876 <---- code
    text text <--- some text

    so to answer your question is it the code i need from the text document with the "--->" also

  6. #6
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Drop text depending on type?

    Some thing like this one ?

    Code:
    Do Until sInputLine Is Nothing
                        ListBox1.Items.Add(sInputLine)
                        sInputLine = srFileReader.ReadLine()
                        If IsNumeric(sInputLine) or  sInputLine.Indexof("--->") > -1 Then
                            lststrippedtext.Items.Add(sInputLine)
                        End If
                    Loop
    Please mark you thread resolved using the Thread Tools as shown

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Re: Drop text depending on type?

    i just tryed that line out it keeps saying the

    Object reference not set to an instance of an object.

    but it is as far as i can tell

    Code:
    Using ofd As New OpenFileDialog
                ofd.Filter = "Subtitle Files|*.srt"
                ofd.Title = "Select File"
                ofd.InitialDirectory = lbldir.Text
                If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    Dim fileReader As String
                    Dim srFileReader As System.IO.StreamReader
                    Dim sInputLine As String
                    Dim data As New List(Of String)
                    
                    ListBox1.Items.Clear()
                    lbltimecodes.Text = "0"
                    fileReader = ofd.FileName
                    srFileReader = System.IO.File.OpenText(fileReader)
                    lbldir.Text = fileReader.ToString
                    sInputLine = srFileReader.ReadLine()
                    Do Until sInputLine Is Nothing
                        ListBox1.Items.Add(sInputLine)
                        sInputLine = srFileReader.ReadLine()
    
                        If sInputLine.Indexof("-->") > -1 Then
                            lststrippedtext.Items.Add(sInputLine)
                        End If
                    Loop
                    lbltimecodes.Text = ListBox1.Items.Count
                    srFileReader.Close()
                    errormsg = True

  8. #8
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Drop text depending on type?

    Did you debug the code. You will get the error if the sInputLine is nothing. Means that you are trying to read after the file contents.


    Hope this helps


    Code:
    Using ofd As New OpenFileDialog
                ofd.Filter = "Subtitle Files|*.srt"
                ofd.Title = "Select File"
                ofd.InitialDirectory = lbldir.Text
                If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    Dim fileReader As String
                    Dim srFileReader As System.IO.StreamReader
                    Dim sInputLine As String
                    Dim data As New List(Of String)
                    
                    ListBox1.Items.Clear()
                    lbltimecodes.Text = "0"
                    fileReader = ofd.FileName
                    
    
    		Using reader As New StreamReader(fileReader )
    		             Do Until reader.EndOfStream
    	                     Dim line = reader.ReadLine()
              	
                    	 ListBox1.Items.Add(line)
    			If line.Indexof("-->") > -1 Then
                               lststrippedtext.Items.Add(line)
                            End If
      
      		        Loop
      
    	      End Using
    Please mark you thread resolved using the Thread Tools as shown

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Thumbs up Re: Drop text depending on type?

    Thank you!!!

    I appreciate everything you helped with me mate!

    You have just made my day hahaha

    Thanks

  10. #10
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [RESOLVED] Drop text depending on type?

    Happy to see its working
    Please mark you thread resolved using the Thread Tools as shown

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width