[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
Re: Drop text depending on type?
Use something like this:
vb.net Code:
Dim data As New List(Of String)
Using reader As New StreamReader("file path here")
Do Until reader.EndOfStream
Dim line = reader.ReadLine()
If LineShouldBeKept(line) Then
data.Add(line)
End If
Loop
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.
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 :(
Re: Drop text depending on type?
What do you mean by this one ?
Quote:
09876 --> 09876 but i want this to be pulled
Re: Drop text depending on type?
Quote:
Originally Posted by
danasegarane
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
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
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
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
Re: Drop text depending on type?
Thank you!!! :):):):):D:D:D:D:D:D
I appreciate everything you helped with me mate!
You have just made my day hahaha
Thanks :)
Re: [RESOLVED] Drop text depending on type?
Happy to see its working :)