read and write to a text file

K

Keith G Hicks

I'm trying to read a text file and alter the contents of specific lines in
the file. I know how to use streamreader to read each line of a file. I'm
doing that already to get the data into a database. What I need help with is
on how to locate a specific line in the file, change it and then save the
updated text file. Can anyone help me out or point me to a site that
explains this clearly?

Here's part of my code that reads the contents of one of the lines into a
variable so I can post it to the db later on in the code.
Dim sr As StreamReader

sr = New StreamReader(textFilesLocation & sImportFolder & "\" &
sFileToImport)

Do

srLine = sr.ReadLine()

If InStr(srLine, "Publication Notice:", CompareMethod.Text) > 0 Then

If Len(srLine) >= 20 Then

ImportType = Trim(Mid(srLine, 20))

End If

End If

Loop Until InStr(srLine, "*** End Notice ***", CompareMethod.Text) <> 0



Thanks,

Keith
 
G

Göran Andersson

Keith said:
I'm trying to read a text file and alter the contents of specific lines in
the file. I know how to use streamreader to read each line of a file. I'm
doing that already to get the data into a database. What I need help with is
on how to locate a specific line in the file, change it and then save the
updated text file. Can anyone help me out or point me to a site that
explains this clearly?

Files are not line based, so you can't change a line directly in the file.

Read the file into a string array using the File.ReadAllLines method,
change the line(s) you want, then save the lines to the file using the
File.WriteAllLines method.

If the file is too large to read into memory, read the lines using a
StreamReader, and write the changed lines to a temporary file using a
StreamWriter. Then delete the original file and rename the temporary
file to replace it.
 
K

Keith G Hicks

Just to clarify, I'm not trying to do a global search and replace. There are
3 date lines in each file. An example of one of the dates would be:

<stuff in line 1>
<stuff in line 2>
Customer Expiration Date: 05/18/2008
<stuff in line 4>
<stuff in line 5>
<stuff in line 6>
<stuff in line 7>

There is text in each of the lines in brackets (just did that for
simplicity's sake). I need to find the line that starts with "Customer
Expiration Date" and update the date by adding 2 months to it.

Keith
 
K

Keith G Hicks

That did the trick. Thank you.


Dim textFilesLocation As String
Dim fileToImport As String
Dim fileText()
Dim i As Int32

textFilesLocation = "D:\Data\"
fileToImport = textFilesLocation & Dir(textFilesLocation)
While fileToImport <> "D:\Data\"

fileText = File.ReadAllLines(fileToImport)
For i = 0 To fileText.Length - 1
If InStr(fileText(i).ToString, "First Pub Date:",
CompareMethod.Text) > 0 Then
fileText(i) = "First Pub Date: " &
Format(DateAdd(DateInterval.Day, 84, CDate(Mid(fileText(i).ToString, 24))),
"Short Date")
End If
If InStr(fileText(i).ToString, "Last Pub Date:", CompareMethod.Text)
fileText(i) = "Last Pub Date: " &
Format(DateAdd(DateInterval.Day, 84, CDate(Mid(fileText(i).ToString, 15))),
"Short Date")
End If
If InStr(fileText(i).ToString, "Sale Date:", CompareMethod.Text) > 0
Then
fileText(i) = "Sale Date: " & Format(DateAdd(DateInterval.Day,
84, CDate(Mid(fileText(i).ToString, 11))), "Short Date")
End If
Next

File.WriteAllLines(fileToImport, fileText)

fileToImport = textFilesLocation & Dir()
End While

MsgBox("done")
 
G

Göran Andersson

Keith said:
Just to clarify, I'm not trying to do a global search and replace. There are
3 date lines in each file. An example of one of the dates would be:

<stuff in line 1>
<stuff in line 2>
Customer Expiration Date: 05/18/2008
<stuff in line 4>
<stuff in line 5>
<stuff in line 6>
<stuff in line 7>

There is text in each of the lines in brackets (just did that for
simplicity's sake). I need to find the line that starts with "Customer
Expiration Date" and update the date by adding 2 months to it.

Keith

You can actually do that with a global search and replace. :)

Here's a one-liner that uses a regular expression to fint the lines, and
a lambda expression to parse the date, add two months to it and format
it back into a string:

File.WriteAllText(fileName, Regex.Replace(File.ReadAllText(fileName),
"^(Customer Expiration Date: )(\d{2}/\d{2}/\d{4})(\r?)$", Function(m As
Match) m.Groups(1).Value + DateTime.ParseExact(m.Groups(2).Value,
"MM'/'dd'/'yyyy",
CultureInfo.InvariantCulture).AddMonths(2).ToString("MM'/'dd'/'yyyy") +
m.Groups(3).Value, RegexOptions.Multiline))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top