-
StreamReader Help...
I'm not sure the best way to perform this task. I'm trying to read through a text file, parsing out certain lines. However, my Do...While loop looks for the StreamReader.ReadLine <> Nothing when it closes the loop.
The text files I'm parsing have lines with nothing on them, which will cause it to quit early not reading in the whole file. Is there anyway to have it read the number of lines and loop through that way? Or does anyone see another way to do this? My code follows... (just for reference, I'm deleting any line containing a certain string, and the following line)
Code:
Function ParseMe(ByVal strFileName As String, ByVal searchString _
As String)
Dim objReader As StreamReader
Dim objWriter As StreamWriter
Dim noWrite As Integer
Dim fileDelete As File
Dim fileCopy As File
' Open the file
objReader = New StreamReader(strFileName)
objWriter = New StreamWriter(strTempFile, False)
' Declare variable
Dim strLine As String, strData As String
' Loop through, compare, write temp...
Do
strLine = objReader.ReadLine
If strLine <> Nothing Then
If strLine.IndexOf(searchString) > 0 Then
objReader.ReadLine()
noWrite = 1
Else
noWrite = 0
End If
End If
If noWrite = 0 Then
objWriter.WriteLine(strLine)
End If
Loop While strLine <> Nothing
objReader.Close()
objReader = Nothing
objWriter.Close()
objWriter = Nothing
fileCopy.Copy(strTempFile, strFileName, True)
fileDelete.Delete(strTempFile)
End Function
-
I dont think a string can be nothing..check for the string = ""
-
Hmm, well that doesn't seem to be my problem. The problem is that if I have a line with nothing on it, it's returned as nothing thus ending the loop. I don't want it to do that. I want the loop to end at the end of the file, not at the blank line.
-
try this
Code:
Do Until streamreader.Peek = -1
' stuff
Loop
-
Excellent! Thanks for your help.