I'm stuck a little here, what I want to do is read a text file find the lines that contain X= and Y= and change X= to X=1 and Y= to Y=1
So far I got this code
Dim MyPath As String = "C:\Users\kiko4\Desktop\text.txt"
Dim Rdr As New StreamReader(MyPath)
Dim ln As String
Dim NewFile As New StringBuilder
ln = Rdr.ReadLine
Do Until ln Is Nothing
If ln.StartsWith("X=", "Y=") And ComboBox1.Text = "1" Then ln = "X=1" & "Y=1"
NewFile.AppendLine(ln)
ln = Rdr.ReadLine
Loop
Rdr.Close()
File.WriteAllText(MyPath, NewFile.ToString)
MsgBox("Successfully changed the resolution.", MsgBoxStyle.Information)
End Sub
So this results to only changing X and leaving Y like it is, is it possible to change the two lines at the same time?
Try going for a different approach when you're stuck... try this :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim SR As New StreamReader("C:\Users\kiko4\Desktop\text.txt")
Dim Result As String = SR.ReadToEnd()
SR.Close()
Dim Lines As String() = Result.Split(Environment.NewLine)
Dim TestLine As String
Dim ReplacedLine As String
For Each line As String In Lines
TestLine = line
If TestLine.Contains("X=") Then
ReplacedLine = TestLine.Replace("X=", "X=" & ComboBox1.Text)
Result = Result.Replace(TestLine, ReplacedLine)
ElseIf TestLine.Contains("Y=") Then
ReplacedLine = TestLine.Replace("Y=", "Y=" & ComboBox1.Text)
Result = Result.Replace(TestLine, ReplacedLine)
End If
Next
Dim SW As New StreamWriter("C:\Users\kiko4\Desktop\text.txt")
SW.Write(Result)
SW.Close()
End Sub
Please feel free to ask about anything you didn't understand.
Hope that was useful
Using StreamReaders/Writers is nice and all, and it surely is the best approach
when you are trying to iterate over multiple lines, one by one.
However, in this case this really isn't neccessary, since OP's just trying to replace all occurences within the file, lines do not matter at all.
From a readability/length perspective, something like this would be preferable:
Dim inPath As String = "C:\Users\kiko4\Desktop\text.txt"
Dim trText As String = File.ReadAllText(inPath)
If trText.Contains("X=") AndAlso trText.Contains("Y=") Then File.WriteAllText(inPath, trText.Replace("X=", "X=1").Replace("Y=", "Y=1"))
(all text in the file is read and saved as a single string to trText, if trText contains the string to be replaced write the contents of trText with replaced values to the file)
Or, if you are 100% sure that every file passed will contain the search-values, you can even do it in one Line:
File.WriteAllText("C:\Users\kiko4\Desktop\text.txt", File.ReadAllText("C:\Users\kiko4\Desktop\text.txt").Replace("X=", "X=1").Replace("Y=", "Y=1"))
This one will blow up if the file doesn't contain "X=" AND "Y=".
This is how I fixed it, works as i intended to, which changes both values, thanks everyone for their help :)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim MyPath As String "C:\Users\kiko4\AppData\Local\FortniteGame\Saved\Config\WindowsClient\text.txt")
Dim Rdr As New StreamReader(MyPath)
Dim ln As String
Dim ac As String
Dim NewFile As New StringBuilder
ln = Rdr.ReadLine
ac = Rdr.ReadLine
Do Until ln Is Nothing
If ln.StartsWith("ResolutionSizeX=") Then ln = "ResolutionSizeX=" + TextBox1.Text
NewFile.AppendLine(ln)
ln = Rdr.ReadLine
If ac.StartsWith("ResolutionSizeY=") Then ac = "ResolutionSizeY=" + TextBox2.Text
NewFile.AppendLine(ac)
ac = Rdr.ReadLine
Loop
Rdr.Close()
File.WriteAllText(MyPath, NewFile.ToString)
MsgBox("Successfully changed the resolution.", MsgBoxStyle.Information)
End Sub
End Class
Related
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
My problem
Index was outside the bounds of the array. when i try to run the code , it generates this error
i have two forms : SIGN IN and SIGN UP , my problem is they don't work together and generates the error attached below
Dim fs As New FileStream("C:\Users\Selmen\Desktop\vb\logs.txt", FileMode.Open, FileAccess.ReadWrite)
Dim sr As New StreamReader(fs)
Dim sw As New StreamWriter(fs)
Dim s As String
Dim t() As String
Dim trouve As Integer = 0
Dim tt() As String
Dim ch As String
ch = TextBox1.Text + "#" + TextBox2.Text + "#" + TextBox3.Text + "#" + TextBox4.Text + "#" + TextBox5.Text
tt = ch.Split("#")
Do While (trouve = 0) And (sr.Peek > -1)
s = sr.ReadLine
t = s.Split("#")
If String.Compare(t(2), tt(2)) = 0 Then
trouve = 1
End If
Loop
If (trouve = 1) Then
MsgBox("user existant")
Else
sw.WriteLine(ch)
Me.Hide()
Form4.Show()
End If
sw.Close()
sr.Close()
fs.Close()
End Sub
If String.Compare(t(2), tt(2)) = 0 Then I get:
IndexOutOfRangeException was unhandled / Index was outside the bounds of the array.
Streams need to be disposed. Instead of using streams you can easily access a text file with the .net File class.
File.ReadAllLines returns an array of lines in the file. We can loop through the lines in a For Each. The lower case c following the "#" tells the compiler that you intend a Char not a String. String.Split expects a Char. Normally, String.Compare is used to order strings in alphabetical order. You just need an =. As soon as we find a match we exit the loop with Exit For.
We don't actually need the array of the text boxes Text property unless there is no match. Putting the elements in braces intializes and fills the array of String.
File.AppendAllLines does what it says. It is expecting an array of strings. As with the text boxes, we put our line to append in braces.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim p = "path to file"
Dim lines = File.ReadAllLines(p)
Dim trouve As Integer
For Each line In lines
Dim thirdField = line.Split("#"c)(2)
If thirdField = TextBox3.Text Then
trouve = 1
Exit For
End If
Next
If trouve = 1 Then
MsgBox("user existant")
Else
Dim tt = {TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text}
File.AppendAllLines(p, {String.Join("#", tt)})
Me.Hid3e()
Form4.Show()
End If
End Sub
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
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.
i was kindly helped before with this code, however I have hit a stumbling block, and im not sure the correct way to go. I have the code below which does a find and replace on over 120k of find and replaces. The problem is the text file is HUGE easily over 5 gig of log files so i get a memory issue which is not surprise. So do i load the data in blocks if that is even possible?, if so how.
Private Sub CmdBtnTestReplace_Click(sender As System.Object, e As System.EventArgs) Handles CmdBtnTestReplace.Click
Dim fName As String = "c:\backup\logs\master.txt"
Dim wrtFile As String = "c:\backup\logs\masterUserFormatted.txt"
Dim strRead As New System.IO.StreamReader(fName)
Dim strWrite As New System.IO.StreamWriter(wrtFile)
Dim s As String
s = strRead.ReadToEnd()
'runs through over 120k of find and replaces
For Each row As DataGridViewRow In DataGridView1.Rows
If Not row.IsNewRow Then
Dim Find1 As String = row.Cells(0).Value.ToString
Dim Replace1 As String = row.Cells(1).Value.ToString
Cursor.Current = Cursors.WaitCursor
'replace using string from 1st column and replaces with string from 2nd column.
s = s.Replace(Find1, Replace1)
End If
Next
strWrite.Write(s)
strRead.Close()
strWrite.Close()
Cursor.Current = Cursors.Default
MessageBox.Show("Finished Replacing")
End Sub
If the input file is a simple multi-line text file, where no individual line is too big to load into memory at once, and the search string is never going to span multiple lines, then reading only one line at a time will be the simplest solution. For instance:
Dim fName As String = "c:\backup\logs\master.txt"
Dim wrtFile As String = "c:\backup\logs\masterUserFormatted.txt"
Dim strRead As New System.IO.StreamReader(fName)
Dim strWrite As New System.IO.StreamWriter(wrtFile)
Cursor.Current = Cursors.WaitCursor
While True
Dim line As String = strRead.ReadLine()
If line IsNot Nothing Then
For Each row As DataGridViewRow In DataGridView1.Rows
If Not row.IsNewRow Then
Dim Find1 As String = row.Cells(0).Value.ToString
Dim Replace1 As String = row.Cells(1).Value.ToString
line = line.Replace(Find1, Replace1)
End If
Next
strWrite.WriteLine(line)
Else
Exit While
End If
End While
strRead.Close()
strWrite.Close()
Cursor.Current = Cursors.Default
MessageBox.Show("Finished Replacing")
It's worth mentioning that the StreamReader and StreamReader implement IDisposable. As such, it would be preferable to enclose them in a Using block rather than explicitly calling Close yourself.