By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,080 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

read and write to a text file

P: n/a
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

Nov 7 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Keith G Hicks wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Nov 7 '08 #2

P: n/a
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

"Keith G Hicks" <kr*@comcast.netwrote in message
news:Ol**************@TK2MSFTNGP04.phx.gbl...
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

Nov 7 '08 #3

P: n/a
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)
0 Then
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öran Andersson" <gu***@guffa.comwrote in message
news:uw**************@TK2MSFTNGP03.phx.gbl...
Keith G Hicks wrote:
>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.

--
Göran Andersson
_____
http://www.guffa.com

Nov 7 '08 #4

P: n/a
Keith G Hicks wrote:
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).ToStrin g("MM'/'dd'/'yyyy") +
m.Groups(3).Value, RegexOptions.Multiline))

--
Göran Andersson
_____
http://www.guffa.com
Nov 8 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.