VB.NET - how to add lines at the top of text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mishaluba
    New Member
    • Jul 2007
    • 2

    VB.NET - how to add lines at the top of text file

    Hello,

    I need to add a few lines at the TOP of the existing file (create missing file headers). How can I do this?

    Thank you!

    vb.net newby
  • cyberdaemon
    New Member
    • Jul 2007
    • 38

    #2
    Originally posted by mishaluba
    Hello,

    I need to add a few lines at the TOP of the existing file (create missing file headers). How can I do this?

    Thank you!

    vb.net newby
    Ok I have been working on this, it was fun. I was real close to not have to brute force the add, but it was still cutting part of the file out. So this is probably the worst way to do this but:

    [CODE=vbnet]Private Sub AddHeader()
    ' Append to TOP
    Dim _FStream As New System.IO.FileS tream("C:\Heade rfile.txt", IO.FileMode.Ope n, IO.FileAccess.R eadWrite, IO.FileShare.Re adWrite, 32, IO.FileOptions. RandomAccess)
    Dim _Reader As New System.IO.Strea mReader(_FStrea m)

    Dim AppendVal As String = ""
    Dim CurVal As String = ""

    AppendVal = "APPENDED HEADER LINE 1"

    ' Read old file in, then Place the Header at the beginning of the stream
    _FStream.Seek(0 , IO.SeekOrigin.B egin) ' Set the stream to the beginning of the file
    _Reader = New IO.StreamReader (_FStream)
    While Not _Reader.EndOfSt ream
    CurVal &= Convert.ToChar( _Reader.Read)
    End While
    AppendVal &= vbNewLine & CurVal
    _Reader.Close()

    ' Write file with header
    _FStream = New System.IO.FileS tream("C:\Heade rfile.txt", IO.FileMode.Ope n, IO.FileAccess.W rite, IO.FileShare.No ne, 32, IO.FileOptions. None)
    Dim _Writer As New System.IO.Strea mWriter(_FStrea m)
    _FStream.Seek(0 , IO.SeekOrigin.B egin) ' Set the stream to the beginning of the file
    _Writer = New IO.StreamWriter (_FStream)
    _Writer.WriteLi ne(AppendVal)

    _Writer.Close()
    End Sub[/CODE]

    it can be easily modified to accept a parameter for the header string.
    To Use: Simply add the lines to AppendVal, if multiple line then

    [CODE=vbnet]Appendval = "Line 1" & controlchars.ne wline & "Line 2" & controlchars.ne wline & "Line 3"[/CODE]

    it works. Play with it, let me know if you shorten it. I had it shorter, then I received an error so I added a few lines to make it more robust.

    also change the path to where the file is, this can become a parameter as well. Anyway thanks for the challenge. Hope this works for you good luck and happy coding

    Cyberdaemon

    Comment

    • vanc
      Recognized Expert New Member
      • Mar 2007
      • 211

      #3
      Make it simpler:
      1. Create a new file called NewFileWithHead er
      2. Write headers into new file
      3. Open original file
      4. Append the content of original file to new file
      5. Close original file
      6. Close new file
      7. Delete original file
      8. Rename new file to the same name original file

      cheers.

      Comment

      • mishaluba
        New Member
        • Jul 2007
        • 2

        #4
        OK, I finally found the time to do this. Cyberdaemon, thank you very much for the code, it got me started in the right direction. Here is what is working for me -- basically the same approach, maybe a bit more streamlined:

        Dim sHeaderFile As String = "c:\MyHeaderFil e"
        Dim sMyBatchFile As String = "c:\myBatchFile "
        Dim sHeaderText As String
        Dim sSplitBatchText As String
        Dim sResult As String = ""

        ' Declare Stream object to read header file and point it to our header file
        Dim fStream_Header As New System.IO.FileS tream(sHeaderFi le, IO.FileMode.Ope n, IO.FileAccess.R ead)
        ' Declare Stream reader object and at the same time read the header file in
        Dim fsStreamReader_ Header As New System.IO.Strea mReader(fStream _Header)

        ' Read the header
        sHeaderText = fsStreamReader_ Header.ReadToEn d

        ' Declare Stream object to read batch file and point it to our batch file
        Dim fStream_BatchFi le As New System.IO.FileS tream(sMyBatchF ile, IO.FileMode.Ope n, IO.FileAccess.R eadWrite)
        ' Declare Stream reader object and at the same time read the batch file in
        Dim fsStreamReader_ BatchFile As New System.IO.Strea mReader(fStream _BatchFile)

        ' Read the batch file
        sSplitBatchText = fsStreamReader_ BatchFile.ReadT oEnd

        ' Append the header in front of the batch file text
        sResult = sHeaderText & vbNewLine & sSplitBatchText

        ' Point our stream back to the beginning of the file
        fStream_BatchFi le.Seek(0, IO.SeekOrigin.B egin)
        ' Declare Stream writer object and point it to the batch file, we are now looking at the beginning of the file
        Dim fsStreamWriter_ BatchFile As New System.IO.Strea mWriter(fStream _BatchFile)

        ' Write our result back to the batch file
        fsStreamWriter_ BatchFile.Write (sResult)

        ' Clean up
        fsStreamReader_ Header.Close()
        ' Since both StreamReader and StreamWriter point to the same file, we only need to close one of them
        ' fsStreamReader_ BatchFile.Close ()
        fsStreamWriter_ BatchFile.Close ()

        Dts.TaskResult = Dts.Results.Suc cess

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          hmm
          If you have a file called "a.txt" and "b.txt" then run the system command
          "copy ?.txt both.txt"

          both.txt will then contain the contents of a.txt followed by b.txt

          Comment

          • sudhakarkintala
            New Member
            • Nov 2015
            • 1

            #6
            Thanks... This script is very helpful for me.. The script is throwing error if the file size is more than 500MB .. Can you please help me on how to handle big files for more than 500 Mb.. i am getting out of memory exception error
            Last edited by Niheel; Nov 10 '15, 10:41 AM. Reason: Merged, please don't bump threads with similar posts. Use one thought-out post.

            Comment

            Working...