read and write to a text file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith G Hicks

    read and write to a text file

    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(te xtFilesLocation & sImportFolder & "\" &
    sFileToImport)

    Do

    srLine = sr.ReadLine()

    If InStr(srLine, "Publicatio n Notice:", CompareMethod.T ext) 0 Then

    If Len(srLine) >= 20 Then

    ImportType = Trim(Mid(srLine , 20))

    End If

    End If

    Loop Until InStr(srLine, "*** End Notice ***", CompareMethod.T ext) <0



    Thanks,

    Keith



  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

    #2
    Re: read and write to a text file

    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.ReadAllLin es method,
    change the line(s) you want, then save the lines to the file using the
    File.WriteAllLi nes 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
    _____
    Göran Anderssons privata hemsida.

    Comment

    • Keith G Hicks

      #3
      Re: read and write to a text file

      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" <krh@comcast.ne twrote in message
      news:Ol8A87PQJH A.1168@TK2MSFTN GP04.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(te xtFilesLocation & sImportFolder & "\" &
      sFileToImport)
      >
      Do
      >
      srLine = sr.ReadLine()
      >
      If InStr(srLine, "Publicatio n Notice:", CompareMethod.T ext) 0 Then
      >
      If Len(srLine) >= 20 Then
      >
      ImportType = Trim(Mid(srLine , 20))
      >
      End If
      >
      End If
      >
      Loop Until InStr(srLine, "*** End Notice ***", CompareMethod.T ext) <0
      >
      >
      >
      Thanks,
      >
      Keith
      >
      >
      >

      Comment

      • Keith G Hicks

        #4
        Re: read and write to a text file

        That did the trick. Thank you.


        Dim textFilesLocati on As String
        Dim fileToImport As String
        Dim fileText()
        Dim i As Int32

        textFilesLocati on = "D:\Data\"
        fileToImport = textFilesLocati on & Dir(textFilesLo cation)
        While fileToImport <"D:\Data\"

        fileText = File.ReadAllLin es(fileToImport )
        For i = 0 To fileText.Length - 1
        If InStr(fileText( i).ToString, "First Pub Date:",
        CompareMethod.T ext) 0 Then
        fileText(i) = "First Pub Date: " &
        Format(DateAdd( DateInterval.Da y, 84, CDate(Mid(fileT ext(i).ToString , 24))),
        "Short Date")
        End If
        If InStr(fileText( i).ToString, "Last Pub Date:", CompareMethod.T ext)
        0 Then
        fileText(i) = "Last Pub Date: " &
        Format(DateAdd( DateInterval.Da y, 84, CDate(Mid(fileT ext(i).ToString , 15))),
        "Short Date")
        End If
        If InStr(fileText( i).ToString, "Sale Date:", CompareMethod.T ext) 0
        Then
        fileText(i) = "Sale Date: " & Format(DateAdd( DateInterval.Da y,
        84, CDate(Mid(fileT ext(i).ToString , 11))), "Short Date")
        End If
        Next

        File.WriteAllLi nes(fileToImpor t, fileText)

        fileToImport = textFilesLocati on & Dir()
        End While

        MsgBox("done")


        "Göran Andersson" <guffa@guffa.co mwrote in message
        news:uwBy7BQQJH A.5064@TK2MSFTN GP03.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.ReadAllLin es method,
        change the line(s) you want, then save the lines to the file using the
        File.WriteAllLi nes 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

        Comment

        • =?ISO-8859-1?Q?G=F6ran_Andersson?=

          #5
          Re: read and write to a text file

          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.WriteAllTe xt(fileName, Regex.Replace(F ile.ReadAllText (fileName),
          "^(Customer Expiration Date: )(\d{2}/\d{2}/\d{4})(\r?)$", Function(m As
          Match) m.Groups(1).Val ue + DateTime.ParseE xact(m.Groups(2 ).Value,
          "MM'/'dd'/'yyyy",
          CultureInfo.Inv ariantCulture). AddMonths(2).To String("MM'/'dd'/'yyyy") +
          m.Groups(3).Val ue, RegexOptions.Mu ltiline))

          --
          Göran Andersson
          _____
          Göran Anderssons privata hemsida.

          Comment

          Working...