Delete line in TXT file but keep everything else - vb.net

Hey all i am trying to figure out a way to delete something inside a text file but keep everything around it.
An example of this would be:
SDfmifgn349234024jn4tnge0b04tnEFGm34tmn34t0egonkerglnk
318erg4nergpERGhmboergn4t34tmg054
fg94t34tskmsdglnEGgjr894ERG94mrg34tSDFS$45352ty
GGreerkg0gm4m505556g0fdg6555fbd105f1g
And say i wanted to delete the 318erg4nergpERGhmboergn4t34tmg054 and therefore it would turn out to be saved as:
SDfmifgn349234024jn4tnge0b04tnEFGm34tmn34t0egonkerglnk
fg94t34tskmsdglnEGgjr894ERG94mrg34tSDFS$45352ty
GGreerkg0gm4m505556g0fdg6555fbd105f1g
But i am unable to find out how to go about doing that!
I've tried this code below that i found:
Dim dir As New DirectoryInfo(defaultNetworkDrive)
For Each file As FileInfo In dir.GetFiles()
If file.Extension = ".txt" Then
Dim ioFile As New StreamReader(defaultNetworkDrive & file.Name)
Dim ioLine As String ' Going to hold one line at a time
Dim ioLines As String ' Going to hold whole file
ioLine = ioFile.ReadLine
ioLines = ioLine
While Not ioLine = ""
ioLine = ioFile.ReadLine
ioLines = ioLines & vbCrLf & ioLine
End While
MsgBox(ioLines) 'SHOWS all the lines in the TXT file
If InStr(1, ioLines, encryptedText, vbTextCompare) <> 0 Then
MsgBox("True")
Else
MsgBox("False")
End If
'Dim sw As StreamWriter = file.CreateText("input.txt")
'sw.Write(ioLines)
'sw.Close()
ioFile.Close()
End If
Next
I do find it and its TRUE but i am not sure how to go about just deleting that line then saving it!
Any help would be great! :o)
David

You have to rewrite the file. Use StreamWriter to create a temporary file. Read one line at a time and write it to the output file. Skip the write if you don't want the line. Clean up by renaming the original, renaming the temporary then deleting the renamed original.
Use something better than a text file, like a dbase, if this is too slow.

You need to check for the string to exclude inside the loop where you read the lines:
While Not ioLine = ""
ioLine = ioFile.ReadLine
If ioLine <> "318erg4nergpERGhmboergn4t34tmg054" then
ioLines = ioLines & vbCrLf & ioLine
End
End While

If ioLines.StartsWith(encryptedText) Then
MsgBox("True")
Else
MsgBox("False")
End If
You are checking if the 'file' (ioLines) starts with the encrypted text, not the line (ioLine). I suspect that's why it isn't finding it.
I'm not sure if you are intending it, but I think adding the vbCrLf to ioLines will cause an empty line between each line. So your file output would be
LINE
[empty line]
LINE
[empty line]
...
To save the file without that line, you would need to write the lines you want to save to a new file.
Read line from file
If not excluded line
write line
Something like that (obviously off the top of my head) :)

Hmm, maybe I found the answer.
You are connecting all lines together:
ioLines = ioLines & vbCrLf & ioLine
Later you are trying to find the search string, but only at the start of the combined line!
ioLines.StartsWith(encryptedText)
This would only find the encryptedText if it be appear on the very first line of the file.
In your example the relevant string is somewhere in the middle of the iolines string.
So you should try to use
ioLines.Contains(encryptedText)
instead.
Edit:
You could try to do the following (just a though, I haven't tested it)
Dim fileLines as List(Of String)
fileLines = new List(Of String)(File.ReadAllLines(yourFileNameHere))
For i as Integer = fileLines.Count To 0 Step -1
If fileLines(i).Contains(encryptedText) Then
fileLines(i).RemoveAt(i)
End If
Next
File.WriteAllLines(yourFileNameHere, fileLines.ToArray)
HTH

Related

Trying to close textfile after line is read

Im trying to output the data from the second line of my textfile to a datagridview but when doing so it is also outputting every line after the the second line. This is what I have tried. Thanks
Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
For index = 1 To lines.Length - 1
Dim cells = lines(index).Split(","c)
dgvOutput.Rows.Add(cells)
FileClose()
It's outputting every line after the second line, because that's what you're telling it to do when you iterate through the array of strings returns from ReadAllLines.
IO.File.ReadAllLines does not leave an output stream open. The file is closed. What it does do, is return a zero-based (by default) array of the contents of the file, with line breaks being the delimiter for the split.
To just get the contents of the second line, using ReadAllLines, this is what you need:
Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
If lines.length >= 2 Then
Dim cells = lines(1).Split(","c)
dgvOutput.Rows.Add(cells)
End If
Now, that does have the overhead of reading the entire file in. If you open the file using a reader object, then you only need to read the first and second lines of the file to get that second line.
That would be something like this:
Dim reader as StreamReader = My.Computer.FileSystem.OpenTextFileReader(OrderId & ".txt")
Dim a as String
' This reads the first line, which we throw away
reader.ReadLine()
a = reader.ReadLine()
reader.Close()
Dim cells = a.Split(","c)
dgvOutput.Rows.Add(cells)
You would need to test your explicit circumstances to determine which is better for what you're trying to do.
Your loop is executed over all lines skipping just the first line.
While I cannot see what happen in the FileClose call it seems to not have any sense because ReadAllLines has already closed the file.
You can get the second line of your file with a single line of code
Dim line as String = File.ReadLines(OrderID & ".txt").Skip(1).Take(1).FirstOrDefault()
' this check is required to avoid problems with files containing 0 or 1 line
if line IsNot Nothing Then
Dim cells = line.Split(","c)
dgvOutput.Rows.Add(cells)
End If
Notice that I have replaced the ReadAllLines with ReadLines. This is better because using this method you don't read all lines when you need only the second one (if it exists). More info at ReadLines vs ReadAllLines
Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
Dim SecondLine = lines(1)
File.ReadAllLines opens and closes the file for you so there is not need to add code to close it.

Dynamically edit Contents of a CSV file using VB.net

I have this code
Dim fileReader As System.IO.StreamReader
fileReader =
My.Computer.FileSystem.OpenTextFileReader("Filepath")
Dim stringReader As String
'read csv file from first to last line
While fileReader.ReadLine <> ""
'get data of line
stringReader = fileReader.ReadLine()
'check the number of commas in the line
Dim meh As String() = stringReader.Split(",")
If meh.Length > 14 Then
My.Computer.FileSystem.WriteAllText("Filepath", "asd", True)
ElseIf meh.Length < 14 Then
My.Computer.FileSystem.WriteAllText("Filepath", "asd", True)
ElseIf meh.Length = 14 Then
MsgBox("This line of the file has " & stringReader & meh.Length & "commas")
End If
End While
End
End Sub
To explain, the above code would check EACH line of a CSV file to check weather the contents has 14 commas('). Then if that line has more commas, the code will reduce it to 14, and if not, it would write commas so that it would be equal to 14. The above conditions are not created yet, so the code is just for testing. I read something about WriteAllText and this code gives me the error :
The process cannot access the file 'filepath' because it is being used by another process.
Which, I think, means that I cant edit the CSV file because I'm currently using its data.
My question is, how could I edit the contents of the CSV file even when I am checking its contents?
Please do disregard this code
My.Computer.FileSystem.WriteAllText("Filepath", "asd", True)
as I use this code just for testing, if ever I could manage to write it to the CSV file.
I Thank you for all your help.
If your CSV is not too big, you can read it in memory and work with it. When you have finish you can write it again on disk.
'Declare 2 List (or you can work directly with one)
Dim ListLines As New List(Of String)
Dim ListLinesNEW As New List(Of String)
'Read the file
Using MyCSVread As New IO.FileStream("C:\MyCSV.csv", IO.FileMode.Open, IO.FileAccess.ReadWrite)
'Read all the lines and put it in a list of T (string)
Using sReader As IO.StreamReader = New IO.StreamReader(MyCSVread)
Do While sReader.Peek >= 0
ListLines.Add(sReader.ReadLine)
Loop
End Using
End Using
'Your code for work with the line. Here you can write the new lines in the NEW list of string or work directly in the first
For L As Integer = 0 To ListLines.Count - 1
Dim meh As String() = ListLines(L).Split(",")
If meh.Length > 14 Then
'your code ...
ElseIf meh.Length < 14 Then
'your code ...
ElseIf meh.Length = 14 Then
MessageBox.Show("The line " & (ListLines(L) + 1) & " of the file has " & meh.Length & "commas", "MyApp", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Next
'Open again the file for write
Using MyCSVwrite As New IO.FileStream("C:\MyCSV.csv", IO.FileMode.Open, IO.FileAccess.ReadWrite)
'Write back the file with the new lines
Using sWriter As IO.StreamWriter = New IO.StreamWriter(MyCSVwrite)
For Each sLine In ListLinesNEW.ToArray
sWriter.WriteLine(sLine)
Next
End Using
End Using
The "Using" auto close the filestream or a streamreader/write or what you use. Then you will not have problem like "file already in use".
Hope this help.

Using an array to search a file VB

I have a program that needs to look through a text file line by line, the lines look like this:
10-19-2015 Brett Reinhard All Bike Yoga Run the Studio Design Your Own Strength
These are separated by tabs in the text file.
What I want to do is look at the second value, in this case "Brett Reinhard" and move the full line to another textfile called "Brett Reinhard"
I was thinking of using an array to check to see if the second 'column' in the line matched any value within a given array, if it does I want to perform a specific action.
The way I am thinking of doing this is with a For/next statement, now while it will work it will be a laborious process for the computer that I will be using it on.
The code I am thinking of using looks like this:
For intCounter=0 to Whatever Number is the last number of the array
If currentfield.contains(array(intCounter)) Then
Open StreamWriter(File directory & array(intcounter) & ".txt")
Streamwriter.Writeline(currentfield)
End IF
Is there a better way of doing this, such as referencing the second 'column' in the line, similar to the syntax used in VBA for excel.
Name=Cells(1,2).Value
If you can guarantee that a line will only use the tab characters as field separators, you can do something along this:
Open the stream for reading text
Open a stream for writing text
Read a line of text
Use the Split method to break the incoming line into an array of fields
If the second element in the array is your sentinel value, write the original line to the writer
Repeat yourself until you have reached the end of file (ReadLine will return Nothing, or null for those c# folk).
Close and dispose of your stream objects.
If you aren't sure of the format, you will want to take the hit and use the TextFieldParser as mentioned in an earlier comment.
So while its not using an array to search a file, what I ended up doing works just as well. I ended up using the split method thanks to #Martin Soles.
Here is what I came up with:
Sub Main()
Dim intCount As Integer = 1
Dim words As String
Dim split As String()
Using MyReader As New Microsoft.VisualBasic.
FileIO.TextFieldParser(
"I:\Games, Events, & Promotions\FRP\Back End\Approved.txt")
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
words = currentField
split = words.Split(New [Char]() {CChar(vbTab)})
For Each s As String In split
If intCount = 2 Then
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("I:\Games, Events, & Promotions\FRP\Back End\" & s & ".txt", True)
file.WriteLine(currentField)
file.Close()
End If
intCount = intCount + 1
Next s
intCount = 1
Next
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
End Using
End Sub 'Main
Thank you guys for the suggestions.
For right now the split method will work for what is needed.

Better way to check and update line in file VB.NET

I am trying to check a bunch of files that should have a leading | delimiter but sometimes don't. If the first line doesn't have the |, the rest of the file won't, if it does have it, then the rest of the file will.
So I'm checking the first line which works and enters the update logic, which advances to the next row once I read the line in. So I added a writer.Write before I go into the Do While loop and it works, but it seems like there would be a better way of doing this.
Here is my code:
Dim fileList As FileInfo() = dirList.GetFiles
For Each fiName In fileList
sFullName = fiName.FullName
sFileName = Path.GetFileNameWithoutExtension(fiName.ToString)
sDir = fiName.DirectoryName
Dim reader As New StreamReader(sFullName)
Dim line As String = reader.ReadLine()
If Not line.StartsWith("|") Then
Dim sNewFullName As String = sDir + "\" + sFileName + "_Temp.txt"
Dim writer As New StreamWriter(sNewFullName)
Dim sLine As String
sLine = "|" + line + System.Environment.NewLine
writer.Write(sLine)
Do While reader.Peek() <> -1
sLine = "|" + reader.ReadLine() + System.Environment.NewLine
writer.Write(sLine)
Loop
writer.Close()
reader.Close()
File.Delete(sFullName)
File.Move(sNewFullName, sFullName)
End If
reader.Close()
Next fiName
Any help or guidance would be much appreciated, Thank you in advance...
This way works just fine. There are several other ways to do it, but it's a matter of opinion on which is better, based on simplicity, portability, performance, maintainability, etc.
For example, you could read the entire file into a string using file.readalltext, then change all VBCRLF (or whether the line end character is) to VBCRLF & "|", then write it all out with file.writealltext.
If it was me, and if it works, I'd leave it unless I just wanted to learn some other methods. Some would argue for hours for another method that is better, but it may be more productive to move on to the next project.

How to format my output in text box same as inside the txt file in vb.net?

I'm using vb.net. Below is my code on read a txt file and my txt file is quite large. I successfully read and display my Test.txt in txt2.Text, I want my output inside the Test.txt same line by line to be display inside the txt2.Text, I have set up my txt2.Text as multiple line. So when i enlarge my txt2.Text, my output will be the same as inside the Test.txt file. how do i used VbCrLf inside my code?
Dim filename As String = "C:\Users\user\Documents\Test.txt"
Dim Line As String = ""
Dim sb As New StringBuilder
Using sr As StreamReader = File.OpenText(filename)
Line = sr.ReadLine
Do
If Line = "*" Then
Line = sr.ReadLine
Do
sb.Append(Line)
Line = sr.ReadLine
Loop Until Line = "**"
End If
Line = sr.ReadLine
Loop Until Line = ""
End Using
txt2.Text = sb.ToString
End Sub
Your loop remove the CarriageReturn and LineFeed characters from the input line.
You could readd them changing this line
sb.Append(Line)
to
sb.AppendLine(Line)
But you will find soon that you have a bigger problem.
Inside the loop you execute various ReadLine without checking if you have really another line available to read. I don't know the structure of your file, but some kind of error checking is advisable.
With a text file like this:
testdata10
*
testdata1
testdata2
testdata3
testdata4
testdata5
**
testdata11
Assuming that there are no more * before the ** is reached, this code will add the appropriate lines to the textbox as is:
Using sr As New IO.StreamReader("textfile1.dat")
Dim line As String = ""
While Not sr.EndOfStream
line = sr.ReadLine
If line = "*" Then
Do
TextBox1.AppendText(sr.ReadLine & vbNewLine)
Loop Until sr.Peek = Asc("*"c)
End If
End While
End Using