Read random txt line, split strings by : and write in textboxes - vb.net

I've got two textboxes and I want make account generator which will read random line from txt file on website and write it into textboxes. So, I want to read random line(just one) from a text file, where email and password are separated by : so .txt file would look like email#site.com:password , write data before : in textbox1(email) and write data from the same line after : in textbox2.
.txt file looks like this:
email1#example.com:password1
email2#example.com:password2
email3#example.com:password3 etc....
I cannot figure out how to split this strings, any help will be appreciated, thanks anyway :)

There you go.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
tbxEmail.Text = String.Empty
tbxPassword.Text = String.Empty
Dim lines As String() = getData("URL_OF_FILE")
Dim lineCount As Integer = lines.Length
Dim randomValue As Integer = CInt(Math.Floor((lineCount) * Rnd()))
Dim line As String = lines(randomValue)
Dim parts As String() = line.Split(New Char() {":"c})
Dim email As String = parts(0)
Dim password As String = parts(1)
tbxEmail.Text = email
tbxPassword.Text = password
End Sub
Function getData(url As String) As String()
Dim client As System.Net.WebClient = New System.Net.WebClient()
Dim data As String = client.DownloadString(url)
Dim returnValue As String() = data.Split(New String() {Environment.NewLine},
StringSplitOptions.RemoveEmptyEntries)
Return returnValue
End Function
Please not that this is a synchronous request, meaning it will "freeze" your application for the duration of the request.

Related

How many count lines duplicates in text files

please how can I get count of duplicate lines?
Source data: line e.g. user_id;name;surname;3400;44711;30.05.2022 7:00:00;30.05.2022 15:30:00;0;480;0;1;682;10000120;9
Private Sub remove_duplicite(sender As Object, e As EventArgs)
Dim sFiles() As String
sFiles = Directory.GetFiles(filesPath1, remove_dupl)
Dim path As String = String.Join("", sFiles)
'MessageBox.Show(path)
Dim lines As New HashSet(Of String)()
'Read to file
Using sr As StreamReader = New StreamReader(path)
Do While sr.Peek() >= 0
lines.Add(sr.ReadLine())
Loop
End Using
'Write to file
Using sw As StreamWriter = New StreamWriter(path)
For Each line As String In lines
sw.WriteLine(line)
Next
End Using
Close()
End Sub
I try some answers but no success.But I think that will be easy.
Thank you
Dim sList As New List(of String)
sList.Add("1")
sList.Add("2")
sList.Add("2")
sList.Add("3")
Dim sListDistinct As List(Of String) = sList.Distinct().ToList()
Dim iCount as Integer = sList.Count - sListDistinct.Count
But depending on the size of your file, this isn't the best performance way.
Maybe check in your HashSet with .Contains and count if entry already exists

How to combine all csv files from the same folder into one data

I want merge multiple csv files with same layout from the same folder
example :
csv1.csv
ID, PINA,PINB,PCS
1,100,200,450
2,99,285,300
csv2.csv
ID, PINA,PINB,PCS
1,100,200,999
2,99,285,998
out.csv (The file I want make by VB.net)
ID, PINA,PINB,PCS,PCS
1,100,200,450,999
2,99,285,300,998
my problem code :
Dim FileReader As StreamReader
Dim i As Integer = 0
Dim temp As String
For i = 0 To LstFiles.Items.Count - 1
FileReader = File.OpenText(LstFiles.Items.Item(i))
temp = FileReader.ReadToEnd
File.AppendAllText(SaveFileDialog1.FileName, temp)
Next
Please guide me.
Thanks a lot !
Looks to me like each line in the input files has an identifier based on the first value in that row. You want to combine all the numbers after that identifier, from all the files in your ListBox, into one list of numbers that is sorted and has no duplicates. Then you want to generate an output file that has all those identifiers followed by each set of sorted, unique numbers.
If that is correct, then try this out:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
Dim header As String = ""
Dim combinedLines As New SortedList(Of Integer, List(Of Integer))
For Each filename As String In LstFiles.Items
Dim lines = File.ReadLines(filename)
If header = "" Then
header = lines.First
End If
lines = lines.Skip(1)
For Each line As String In lines
Dim strValues = line.Split(",").AsEnumerable
Try
Dim lineNumber As Integer = Integer.Parse(strValues.First)
strValues = strValues.Skip(1)
Dim numbers = strValues.ToList.ConvertAll(Of Integer)(Function(x) Integer.Parse(x))
If Not combinedLines.ContainsKey(lineNumber) Then
combinedLines.Add(lineNumber, New List(Of Integer)(numbers))
Else
combinedLines(lineNumber).AddRange(numbers)
End If
Catch ex As Exception
MessageBox.Show("Error Parsing Line: " & line)
End Try
Next
Next
Using sw As New StreamWriter(SaveFileDialog1.FileName, False)
sw.WriteLine(header)
For Each numberSet In combinedLines
Dim numbers = numberSet.Value.Distinct.ToList
numbers.Sort()
sw.WriteLine(numberSet.Key & "," & String.Join(",", numbers))
Next
End Using
End If
End Sub

Picking out 1 specific string from a file

Ive put in multiple strings in 1 line of a file, so that they are linked together, I have tried multiple ways such as using loops and I was wondering if if anyone could help me, thanks.
in file: swagman (username), samfisher34 (password), sam fisher (fullname)
Private Sub btn_login_Click(sender As Object, e As EventArgs) Handles btn_login.Click
Dim Student As New StreamReader("student.txt")
Dim newline As String
Dim login As Boolean
Dim line1 As String
Dim line2 As String
' Reads the files to a string and write the string to the console.
Dim count As Integer = 20
For i = 1 To count
newline = Student.ReadLine
If txt_login.Text = newline And txt_password.Text = newline Then
'Checks to see if text is the same in the Files.
login = True
End If
If login = True Then
Me.Hide()
StudentMenu.Show()
End If
Next
If login = False Then
MsgBox("login or password is incorrect")
End If
Student.Close()
End Sub
Here is a really basic code example that I made:
First of all you need to import System.IO for the StreamReader
Dim objStreamReader As StreamReader
Dim strLine As String
objStreamReader = New StreamReader(File)
Dim splitresult As Array
'read the file line for line
Do Until objStreamReader.Peek = -1
strLine = objStreamReader.ReadLine
'split the results (user,pwd,name)
splitresult = strLine.Split(",")
If splitresult(0) = user Then
If splitresult(1) = pwd Then
MsgBox("Welcome " & splitresult(2))
End If
Loop
Also please don't store your password as plain text.

Extracting Portion of Url using VB.net

I have this URL
https://www.google.com/maps/place/Aleem+Iqbal+SEO/#31.888433,73.263572,17z/data=!3m1!4b1!4m5!3m4!1s0x39221cb7e4154211:0x9cf2bb941cace556!8m2!3d31.888433!4d73.2657607
I am trying to Extract 31.888433,73.263572 from the URL
and send 31.888433 to TextBox 1
and 73.263572 to TextBox 2
Can you give me an example how can i do this with regex or anything else
You can use string.split(). This method takes an array of chars which are the discriminants for the splitting. The better solution is to split by '/', take the string that starts with '#' and then split it by ','. You'll have an array with two string: first latitude, second longitude.
Should be immediate using LINQ
The explanation is in the code comments.
Dim strURL As String = "https://www.google.com/maps/place/Aleem+Iqbal+SEO/#31.888433,73.263572,17z/data=!3m1!4b1!4m5!3m4!1s0x39221cb7e4154211:0x9cf2bb941cace556!8m2!3d31.888433!4d73.2657607"
'Find the index of the first occurance of the # character
Dim index As Integer = strURL.IndexOf("#")
'Get the string from that the next character to the end of the string
Dim firstSubstring As String = strURL.Substring(index + 1)
'Get a Char array of the separators
Dim separators As Char() = {CChar(",")}
'Split the string into an array based on the separator; the separator is not part of the array
Dim strArray As String() = firstSubstring.Split(separators)
'The first and second elements of the array is what you want
Dim strTextBox1 As String = strArray(0)
Dim strTextBox2 As String = strArray(1)
Debug.Print($"{strTextBox1} For TextBox1 and {strTextBox2} for TextBox2")
Finally Made a working Code Myself
Private _reg As Regex = New
Regex("#(-?[\d].[\d]),(-?[\d].[\d])", RegexOptions.IgnoreCase)
Private Sub FlatButton1_Click(sender As Object, e As EventArgs) Handles FlatButton1.Click
Dim url As String = WebBrowser2.Url.AbsoluteUri.ToString()
' The input string.
Dim value As String = WebBrowser2.Url.ToString
Dim myString As String = WebBrowser2.Url.ToString
Dim regex1 = New Regex("#(-?\d+\.\d+)")
Dim regex2 = New Regex(",(-?\d+\.\d+)")
Dim match = regex1.Match(myString)
Dim match2 = regex2.Match(myString)
If match.Success Then
Dim match3 As String = match.Value.Replace("#", "")
Dim match4 As String = match2.Value.Replace(",", "")
Label1.Text = match3
Label2.Text = match4
End If
End Sub
Dim url As String = "www.google.com/maps/place/Aleem+Iqbal+SEO/#31.888433,73.263572,17z/data=!3m1!4b1!4m5!3m4!1s0x39221cb7e4154211:0x9cf2bb941cace556!8m2!3d31.888433!4d73.2657607"
Dim temp As String = Regex.Match(url, "#.*,").Value.Replace("#", "")
Dim arrTemp As String() = temp.Split(New String() {","}, StringSplitOptions.None)
Label1.Text = arrTemp(0)
Label2.Text = arrTemp(1)

Replacing strings in multiple text files using regular expressions on vb.net

I'm looking to build a simple text string replacement tool using visual studio 2015 community tool, which will do the below replacements on all *.txt files whose path is given in a textbox:
Find: \<figure (\d+)\>
Replace: <a href id="fig\1">figure \1</a>
Find: \<table (\d+)\>
Replace: <a href id="tab\1">table \1</a>
Find: \<section (\d+)\>
Replace: <a href id="sec\1">section \1</a>
I have coded a little portion of the programme but struggling to complete it. I'm completely new in programming and in visual basic is well. Can anyone help complete this programme
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If FBD.ShowDialog = DialogResult.OK Then
TextBox1.Text = FBD.SelectedPath
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim targetDirectory As String
targetDirectory = TextBox1.Text
Dim Files As String() = Directory.GetFiles(targetDirectory, "*.txt")
For Each file In Files
Dim FileInfo As New FileInfo(file)
Dim FileLocation As String = FileInfo.FullName
Dim input As String = file.ReadAllLines(FileLocation)
Dim pattern1 As String = "\<figure (\d+)\>"
Dim pattern2 As String = "\<table (\d+)\>"
Dim pattern3 As String = "\<section (\d+)\>"
Dim rep1 As String = "<a href id= \""fig\1\"" > figure \1</a>"
Dim rep2 As String = "<a href id= \""tab\1\"" > table \1</a>"
Dim rep3 As String = "<a href id= \""sec\1\"" > section \1</a>"
Dim rgx1 As New Regex(pattern1)
Dim rgx2 As New Regex(pattern2)
Dim rgx3 As New Regex(pattern3)
Dim result1 As String = rgx1.Replace(input, rep1)
Dim result2 As String = rgx2.Replace(result1, rep2)
Dim result3 As String = rgx3.Replace(result2, rep3)
Next
End Sub
End Class
The errors I'm getting are given below
Error BC30456 'ReadAllLines' is not a member of 'String'
For the replace button, once you have the link to the folder directory, you need to read in all files that say "*.txt". The below line does this
targetDirectory = TextBox1.text
Dim txtFilesArray As String() = Directory.GetFiles(targetDirectory,"*.txt")
You can then loop through this array and do your replace logic.
For each txtFile in txtFilesArray
'here we grab the files information as we need the files directory
Dim FileInfo As New FileInfo(txtFile)
Dim FileLocation As String = FileInfo.FullName
Dim input() as string = File.ReadAllLines(FileLocation)
'now you have read in your text file you can edit each file as it goes through the loop
'you can now use your regex here to edit each file,
'then once done editing the file, dont forget to write back to your file or it wont save
'you will need to loop through the input array now to change the line
For x as integer = 0 to (input.length - 1)
Dim pattern1 As String = "\<figure (\d+)\>"
Dim pattern2 As String = "\<table (\d+)\>"
Dim pattern3 As String = "\<section (\d+)\>"
Dim rep1 As String = "<a href id= \""fig\1\"" > figure \1</a>"
Dim rep2 As String = "<a href id= \""tab\1\"" > table \1</a>"
Dim rep3 As String = "<a href id= \""sec\1\"" > section \1</a>"
Dim rgx1 As New Regex(pattern1)
Dim rgx2 As New Regex(pattern2)
Dim rgx3 As New Regex(pattern3)
Dim result1 As String = rgx1.Replace(input(x), rep1)
Dim result2 As String = rgx2.Replace(result1, rep2)
Dim result3 As String = rgx3.Replace(result2, rep3)
input(x) = result3
Next
'now you can write your corrected file back to the file
File.WriteAllLines(FileLocation, input)
Next
MsgBox("process complete")
#Tamal Banerjee, try this for progress bar, put the below code after the last Next in the coding
Next
ProgressBar1.PerformStep()
ProgressBar1.Value = 100
MessageBox.Show("Process complete")
End Sub
I'm not sure whether this is the write way to do it, but it worked when tried this on my computer with your coding :)