I have the following code which takes a file's encrypted contents and decrypts it into a string. This is simply just a comma Delimited string like the following:
Garry, 001, 0006
Ben, 002, 00
I want to check the values in column 0 and 1 match the contents of a textbox, but It has to be on the same row hence why I need to read each row/line. I've got most of it to work, I just dont know how to read the string line by line and check it's columns, could someone help?
My commented code is just pseudo code for what I'm trying to do.
Using TextReader As New IO.StreamReader("C:\Users\Garry\Desktop\test.txt")
Dim EncryptedString As String = TextReader.ReadToEnd
Dim DecryptedString As String = AESD(EncryptedString, Password)
Dim strReader As New StringReader(DecryptedString)
Dim line As String = strReader.ReadLine
'If Line.column(0).contains(txtName.text) AND Line.column(1).contains(txtEnteredKey.text) then
'Else
'Go to next line
'End If
End Using
You can split the string by the , and then compare the columns to your desired value. For example, if I wanted to find Ben in the list, I could do this:
Dim s As String = "Garry, 001, 0006
Ben, 002, 00"
Dim strReader As New StringReader(s)
Dim line = strReader.ReadLine
While String.IsNullOrEmpty(line) = False
Dim parts = line.Split(","c)
If (parts(0).Trim() = "Ben" And parts(1).Trim() = "002") Then
Console.WriteLine("Found ben!")
Exit While
End If
line = strReader.ReadLine
End While
Console.ReadLine()
You could also do:
Dim DecryptedString As String = AESD(System.IO.File.ReadAllText("C:\Users\Garry\Desktop\test.txt"), Password)
Dim values() As String
For Each line As String In DecryptedString.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
values = line.Split(",")
If values.Length >= 2 AndAlso values(0) = txtName.Text AndAlso values(1) = txtEnteredKey.Text Then
' ... do something ...
Exit For
End If
Next
Related
in my File Text I have the following things:
I have to make it start from somewhere and stop at a certain point.
but he only starts from that point, but he does not know how to stop at one point.
[Letters]
A
B
C
D
E
[Loop]
[Words]
Fish
Facebook
Google
Youtube
I should display Expected Output:
A
B
C
D
E
Then I should make it display
Fish
Facebook
Google
Youtube
but it shows me:
[Letters]
A
B
C
D
E
[Loop]
[Words]
Fish
Facebook
Google
Youtube
Code
Dim line As String
Using reader As StreamReader = New StreamReader(My.Application.Info.DirectoryPath & "\TestReader.txt")
line = reader.ReadLine
Dim sb As New StringBuilder
Do
Do
If reader.Peek < 0 Then 'Check that you haven't reached the end
Exit Do
End If
line = reader.ReadLine
If line.StartsWith("[Letters]") AndAlso line.EndsWith("[Loop]") Then 'Check if we have reached another check box.
Exit Do
End If
sb.AppendLine(line)
Loop
TextBox1.Text = sb.ToString
sb.Clear()
Loop Until reader.Peek < 0
End Using
This assumes that startPrefix and endPrefix will always be present in the file:
Dim startPrefix As String 'Set as required
Dim endPrefix As String 'Set as required
Dim lines As New List(Of String)
Using reader As New StreamReader("file path here")
Dim line As String
'Skip lines up to the first starting with the specified prefix.
Do
line = reader.ReadLine()
Loop Until line.StartsWith(startPrefix)
line = reader.ReadLine()
Do Until line.StartsWith(endPrefix)
lines.Add(line)
line = reader.ReadLine()
Loop
End Using
'Use lines here.
Are you really sure that you want to look for lines that start with those markers though? Wouldn't you really prefer to look for lines that are equal to those markers?
EDIT:
You could - and probably should - encapsulate that functionality in a method:
Private Function GetLinesBetween(filePath As String, startPrefix As String, endPrefix As String) As String()
Dim lines As New List(Of String)
Using reader As New StreamReader(filePath)
Dim line As String
'Skip lines up to the first starting with the specified prefix.
Do
line = reader.ReadLine()
Loop Until line.StartsWith(startPrefix)
line = reader.ReadLine()
'Take lines up to the first starting with the specified prefix.
Do Until line.StartsWith(endPrefix)
lines.Add(line)
line = reader.ReadLine()
Loop
End Using
Return lines.ToArray()
End Function
I have a notepad with some unscrambled data as shown below and would like to read data from that text file and populate data to individual textboxes.
This is how my input looks likes:
========================================================
Modified Date : 5/20/2019 8:45:56 AM Modified by : 123
ID : 18677544
OLD Values:
First Name Last Name Middle Initial
-------------- -------------- -----------------
John Humpty
NEW Values:
First Name Last Name Middle Initial
-------------- -------------- -----------------
George Louis
========================================================
This is my code
Dim path As String = "C:\Users\XXX\Desktop\123.txt"
Dim searchTarget = "Modified Date"
For Each line In File.ReadAllLines(path)
If line.Contains(searchTarget) Then ' found it!
Dim toBeSearched As String = "Modified Date : "
Dim code As String = line.Substring(line.IndexOf(toBeSearched) + toBeSearched.Length)
txtModifiedDate.Text = code// Here I'm getting Modified By value also but I need only the Modified Date in this textbox similarly for others
Exit For ' then stop
End If
Next line
As per preciousbetine getting overload exception error
Updated :
If the sample text file you gave is how it always looks like, then this should work.
I hardcoded most of the values but for the text file above, the below code works:
Sub GetInfo()
Dim path As String = "C:\Users\XXX\Desktop\123.txt"
Dim lines As New List(Of String)
lines.AddRange(IO.File.ReadAllLines(path))
Dim tmpArray = lines(1).Split(" "c)
'******Update*******************
Dim tmplist As New List(Of String)
tmplist.Add(tmpArray(3))
tmplist.Add(tmpArray(4))
tmplist.Add(tmpArray(5))
Dim strModifiedDate As String = String.Join(" ", tmplist.ToArray) 'Get the date by joining the date, time
'************************
strModifiedDate.Text = strModifiedDate
Dim strModifiedBy As String = tmpArray(UBound(tmpArray))
strModifiedBy.Text = strModifiedBy
tmpArray = lines(2).Split(":"c)
strID.Text = tmpArray(1).Trim
Dim tmpStr As String = lines(7).Split(" "c)(0)
strOldFirstName.Text = tmpStr
tmpStr = lines(7).Substring(strOldFirstName.Text.Length).Trim
strOldLastName.Text = tmpStr
tmpStr = lines(14).Split(" "c)(0)
strNewFirstName.Text = tmpStr
tmpStr = lines(14).Substring(strNewFirstName.Text.Length).Trim
strNewLastName.Text = tmpStr
End Sub
I would like to make a "Tolerance-calculator"
The user gives an input as string. For example:"D6" now i have to search this in the .txt file and read the next line.
I read the file like this:
Dim Findstring = IO.File.ReadAllText("....\Toleranz0.txt")
How can i find the string an the next line after the string?
Maybe:
Findstring.contains("D6") 'gives a Boolean
How does i get the correct line?
Convert your string to an array using String.Split() and find the next index or 2 indexes after "D6":
Private Sub Funcy()
Dim Findstring As String = IO.File.ReadAllText("....\Toleranz0.txt")
Dim MyCollection() As String = Findstring.Split()
Dim result As String = MyCollection(Array.IndexOf(MyCollection, "D6") + 2)
MessageBox.Show(result)
End Sub
Here's an example using ReadAllLines() as suggested by Blorgbeard:
Dim lines() As String = IO.File.ReadAllLines("....\Toleranz0.txt")
Dim index As Integer = Array.IndexOf(lines, "D6")
If index <> -1 AndAlso index <= lines.Count - 2 Then
Dim targetLine As String = lines(index + 1)
' ... do something with "targetLine" ...
Else
' either the line was not found, or there was no line after the found line
End If
working on a project where I input a text file and write a sentence in an "English" text box and with a click of a button the program translates it into "textese" type of language. It will only change words listed in the text file and ignore everything else. Below is the code I have, and wondering what is going wrong, it fails to run and highlights my If Then statement, so I think my issue is there but not sure what. A few examples of what is in the text file and how they are ordered / separated by a comma.
anyone,ne1
are,r
ate,8
band,b&
be,b
before,b4
busy,bz
computer,puter
Public Class frmTextese
Private Sub btnTranslate_Click(sender As Object, e As EventArgs) Handles btnTranslate.Click
Dim inputData() As String = IO.File.ReadAllLines("Textese.txt")
Dim english As Integer = 0
Dim englishSentence As String = txtEnglish.Text
Dim result() As String = englishSentence.Split(" "c)
Do While english < (result.Length - 1)
Dim line As String
Dim data() As String
Dim englishArray As String
Dim texteseArray As String
For i As Integer = 0 To (inputData.Length - 1)
line = inputData(i)
data = line.Split(","c)
englishArray = line.Split(","c)(0)
texteseArray = line.Split(","c)(1)
If result(i).StartsWith(englishArray(i)) Then
If englishArray(i).Equals(texteseArray(i)) Then
result(i) = texteseArray(i)
End If
End If
txtTextese.Text = result(i)
Next
Loop
End Sub
End Class
You only need to compare result(i) against englishArray. Also, your while loop was an endless loop. When searching the textese array, once you find a match, you can quit searching and go on to the next english word. Finally, you should take care to use variable names that describe their purpose.
Look at this code (untested). I made the string comparison case insensitive, but that is not required.
Private Sub btnTranslate_Click(sender As Object, e As EventArgs) Handles btnTranslate.Click
'Get all the textese definitions
Dim inputData() As String = IO.File.ReadAllLines("Textese.txt")
Dim english As Integer = 0
Dim englishSentence As String = txtEnglish.Text
Dim result() As String = englishSentence.Split(" "c)
Do While english < (result.Length - 1)
Dim line As String
Dim data() As String
Dim englishArray As String
Dim texteseArray As String
For i As Integer = 0 To (inputData.Length - 1)
'Split the textese entry into two parts
line = inputData(i)
data = line.Split(","c)
englishArray = data(0)
texteseArray = data(1)
'Compare the word in the english sentence against the word in the textese array
'using a case insensitive comparison (not required)
If result(i).Equals(englishArray, StringComparison.CurrentCultureIgnoreCase) Then
'Replace the word in the sentence with its textese version
result(i) = texteseArray
'If we found the word, there is no need to continue searching, so
'skip to the next word in the english sentence
Exit For
End If
Next
'Increment the loop counter to avoid an endless loop
english = english + 1
Loop
'Take the elements of the result array and join them together, separated by spaces
txtTextese.Text = String.Join(" ", result)
End Sub
i'm trying to importe a csv file after creating on the same application, the only probleme is that even if a string matchs a line from the file, comparing them on vb.net dosn't give a true boolean i know i didn't make any mistakes because of the line with the commentary 'THIS LINE , i use a pretty small list to do my test and in that loop, there is always one element that matches exactly the string, but comparing them on vb.net dosn't return a true. the result of this is that just after saving the file to my computer while still having it on my listview1 on the application, i load it to the application again and it duplicates everything
Dim open As New OpenFileDialog
open.Filter = "CSV Files (*.csv*)|*.csv"
If open.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim File As String = My.Computer.FileSystem.ReadAllText(open.FileName)
Dim lines() As String = File.Split(Environment.NewLine)
For i = 1 To (lines.Count - 1)
Dim line As String = lines(i)
Dim Columns() As String = line.Split(";")
Dim addLin As New ListViewItem(Columns)
Dim Alr As Boolean = False
Dim st as string=listView1.Items.Item(k).SubItems.Item(0).Text
For k = 0 To (ListView1.Items.Count - 1)
Dim st as string=listView1.Items.Item(k).SubItems.Item(0).Text
MsgBox(Columns(0)& Environment.NewLine & st) 'THIS LINE
If ListView1.Items.Item(k).SubItems.Item(0).Text = Columns(0) Then
Alr = True
MsgBox("true") 'never showsup even when the previous one show the match
End If
next
If Alr = False Then
MsgBox("false") 'the msgbox is only to check
ListView1.Items.Add(addLin)
End If
next
end if