I am trying to search a text document, and I am at a stand still.
Example of the document:
11/24 05:05:21.781 T0EA8 [PinRegister Version: PINREG 1.2.0]
11/24 05:05:21.875 T0EA8 [RequestPinPadParamEvent: PR_RegDevice = 0.Exit]
11/25 05:04:38.906 T0FB0 [*************************: ]
11/25 05:04:38.906 T0FB0 [PinRegister Version: PINREG 1.3.0]
Now, in that example document, I want to display the 'PinPegister Version' to textbox 'VersionTextBox' from the 25th.
So I am trying to search for the date, then from the date search for the phrase ('[PinRegister Version: ]') and finaly display the results into the textbox ('PINREG 1.3.0').
I have tried alot of options, with nothing working how I want it.
This is my current code, and I feel close... but I am getting an error 'Object reference not set to an instance of an object.'
Dim strm As IO.Stream = IO.File.OpenRead(fpath)
Dim sr As New IO.StreamReader(strm)
Dim line As String
Dim trimchars() As Char = {" "c}
Dim datelist As ArrayList
Do While sr.Peek <> -1
line = sr.ReadLine()
'If line.TrimStart(trimchars).Contains("[PinRegister Pin Pad Model") Then
If line.TrimStart(trimchars).StartsWith(TDate.Text) Then
' found pattern
datelist.Add(line)
End If
Loop
If datelist.Contains("PinRegister Version:") Then
MsgBox("Found 1")
End If
Thanks in advance for any help
Couldn't you just do something like this?
Public Shared Sub FindStuff()
Dim TextFileLocation = "C:\Test\Test.txt"
Dim srReader As IO.StreamReader = Nothing
srReader = File.OpenText(TextFileLocation)
Do
Dim strInputFileLine As String = srReader.ReadLine()
If strInputFileLine Is Nothing Then Exit Do
If strInputFileLine.Contains(Form1.TextBox1.Text) Then
MessageBox.Show("Found it")
End If
Loop
You didn't say how big your input file is, but if its not too large I would suggest reading in all into a string
My.Computer.FileSystem.ReadAllText
and then using a
yourstring.indexof( ...
to find the text you are looking for and work it from there.
I've done this in the past and its quite fast in most cases.
Related
I am in need of assistance... i am trying to create a textfile with links in it. the code i have..
dim domain as string = "http://www.mywebsite/"
dim name as string = "username"
Dim link As String = New String("domain" & "name")
TextBox1.AppendText(link & Environment.NewLine)
Msgbox(textBox1.lines(0))
The problem is that MsgBox only shows up as "http://www.mywebsite/". the textbox does show "http://www.mywebsite/username" but when copied to text document it is:
Line0: http://www.mywebsite/
Line1:username
any ideas... tried using
Dim link As String = String.Join(domain & name) but that doesnt work nor does
Dim link As String = new String.Join(domain & name)
i need
Msgbox(textBox1.lines(0)) to display "http://www.mywebsite/username" not one or the other.
That was quick got a message saying to use. Dim link As String = String.Concat(domain & name)
i think you should move to StringBuilder first import Imports System.Text
'create a string with multiple lines
Dim a As New StringBuilder
a.AppendLine("hi")
a.AppendLine("there")
a.AppendLine("this")
a.AppendLine("is")
a.AppendLine("a")
a.AppendLine("test")
'read will makes read line by line
Dim read As String() = a.ToString.Split(vbNewLine)
'count has number of lines
Dim count As Integer = a.ToString().Split(vbNewLine).Length - 1
'lines will be added to combobox one by one
For i As Integer = 0 To count - 1
ComboBox1.Items.Add(read(i))
Next
you just should edit it to suits your needs i didnt understand what you needed exactly
I have a tab delimited file which has data like this...
022j<TAB>10.375
023j<TAB>12.365
024j<TAB>15.230
NOTE: this will not let me post as it is... each 02xj is a different line in the text file. It
EG: 023j is input into a textbox.
Need to find the value associated with the input; 12.365 in this case.
There are a few different files (some are encoded 012j, 012#, 012$ etc. which will correspond to different data.)
My head is exploding trying to find a way to take what I have in the textbox then read through and find the data I need.
I know this is easy, please nudge me in the right direction.
Here's your nudge. You should be able to use something like this:
Dim searchValue As Decimal = 023j 'or someTextBox.Text whatever
Dim searchField As Int32 = 0
Dim returnField As Int32 = 1
Dim returnValue As String = ""
Dim returnLineNumber as Int32 = 0
Using fileReader As New FileIO.TextFieldParser(YourFileNameWithPathAsString)
fileReader.TextFieldType = FileIO.FieldType.Delimited
fileReader.SetDelimiters(vbTab)
While Not fileReader.EndOfData
Dim currentLine As String() = fileReader.ReadFields()
If currentLine(searchField) = searchValue Then
returnValue = currentLine(returnField)
returnLineNumber = fileReader.LineNumber
Exit While
End If
End While
End Using
Return returnValue 'or Return returnLineNumber if that is what you need
You should be able to make it a function and if no result is returned then check your next file.
I figured this out... so this is what I ended up doing.
First set up the streamreader
'set reader to read the file
Dim reader As New System.IO.StreamReader(filetoread)
Then loop through the file line by line, use contains to find the string to match. Do whatever trimming/extracting and you're there.
Do While reader.Peek() >= 0
line = reader.ReadLine
If line.Contains(TB_Input.Text) Then
s = Replace(line, TB_Input.Text, "")
s = Replace(s, vbTab, "")
TB_Length.Text = s
End If
Loop
I was leaking brain juice on this. Until someone basically said "just do it"... thanks I needed that.
I know there are things like variables I don't explain... I figured anyone looking will know, if that's a bad assumption let me know.
Thanks again
I have saved written a text file and it currently reads:
"first","surname","pass"
I want to read the password column so the 3rd one and define that as a variable. Its basically for a login, if pass in text file matches the entered pass (from user).
I have searched for about an hour now and no luck. Could someone guide me to a correct path.
Thanks.
Simple example of reading a small file line by line and splitting each one into fields:
' get the values from the user somehow:
Dim first As String = "James"
Dim surname As String = "Bond"
Dim pass As String = "007"
Dim validated As Boolean = False ' assume wrong until proven otherwise
' check the file:
Dim fileName As String = "c:\some folder\path\somefile.txt"
Dim lines As New List(Of String)(System.IO.File.ReadAllLines(fileName))
For Each line As String In lines
Dim values() As String = line.Split(",")
If values.Length = 3 Then
If values(0).Trim(Chr(34)) = first AndAlso
values(1).Trim(Chr(34)) = surname AndAlso
values(2).Trim(Chr(34)) = pass Then
validated = True
Exit For
End If
End If
Next
' check the result
If validated Then
MessageBox.Show("Login Successful!")
Else
MessageBox.Show("Login Failed!")
End If
If this is a CSV file, as seems to be the case, then the easiest way to read it will be with the TextFieldParser class. The MSDN already provides an excellent example for how to use it to read a CSV file, so I won't bother reproducing it here.
I'm trying to delete a selected row, then save the rest into a file. However, when I save it, it totally empties the file.
Console.Write("Please eneter the first name of the student you wish to search for: ")
searchfname = Console.ReadLine
searchfname = StrConv(searchfname, VbStrConv.ProperCase)
Console.Write("Please enter the second name of the student you wish to search for: ")
searchsname = Console.ReadLine
searchsname = StrConv(searchsname, VbStrConv.ProperCase)
Dim foundItem() As String = Nothing
Dim foundline As String = Nothing
Dim fnsearch As String = String.Join(searchfname, searchsname)
Dim lines As New List(Of String)(File.ReadAllLines("F:\Computing\Spelling Bee\stdnt&staffdtls.csv"))
For Each line As String In lines
If searchfname = item(3) And searchsname = item(4) Then
Console.WriteLine(line)
Console.WriteLine()
Console.WriteLine("Are you sure you wish to delete this record? (y/n)")
End If
Dim answer As String
answer = Console.ReadLine
If answer = "y" Or answer = "Y" Then
Console.Clear()
lines.Remove(line)
Using sw As New StreamWriter("F:\Computing\Spelling Bee\stdnt&staffdtls.csv")
sw.WriteLine(lines.ToString)
End Using
ElseIf answer = "n" Or answer = "N" Then
staffmenu()
End If
Next
Look at this line in your code:
sw.WriteLine(lines.ToString)
Extract the lines.ToString expression from that statement. The result of that expression is "System.String". You are telling your stream writer to write the text "System.String" to the file.
To fix it, you need something more like this:
Using sw As New StreamWriter("F:\Computing\Spelling Bee\stdnt&staffdtls.csv")
For Each line As String In lines
sw.WriteLine(line)
Next line
End Using
The method List(Of T).ToString does not produce a value that includes the elements of the collection. Instead it will just return the type name.
The API you are looking for is File.WriteAllLines. Using this instead of StreamWriter and the Using block
File.WriteAllLines("F:\Computing\Spelling Bee\stdnt&staffdtls.csv", lines)
I can see that this issue can be resolved from the given answers and comment, but I would like to add an alternative to use Join function in writing to a file. Try like this may be of help:
Using sw As New StreamWriter(.....)
sw.WriteLine(Join(lines.ToArray(), Environment.NewLine))
End Using
Since using VB.Net, this is a vb.net specific solution can not be used in C#. For C#, use string.join instead.
Hope it helps too!
Hello people as the topic show I want to do in VB.net a script that gets only the numbers from a *.txt file
EXAMPLE:
text file:
asd4lkj5fdl
jklj235
the result:
45235
I have done a research in Google and come up with nothing, I did saw a answer here but only in C
I know that in theory it needs to be like this:
Read every char loop ask if it is an integer add it to a new string if not continue to next char do this till the end of the stream
Thanks for the one how helps !
Try regular expressions, code to read text from file is no included
Dim rgx As New Regex("[^\d]")
Dim result as String = rgx.Replace("asd4lkj5fdl jklj235", "")
Read File into a String
Loop through each char checking if numeric.
Dim strTextFromFile As String = IO.File.ReadAllText("C:\filename.txt")
Dim strResults As String = String.Empty
For Each c As Char In strTextFromFile
If IsNumeric(c) Then
strResults += c
End If
Next
MsgBox(strResults)
Public Sub Test()
Dim contents As String = File.ReadAllText("C:\\temp\\text.txt")
Dim digits As New String(contents.Where(Function(c) Char.IsDigit(c)).ToArray())
MessageBox.Show(digits)
End Sub