471,577 Members | 1,586 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,577 software developers and data experts.

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


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
Jul 19 '07 #1
5 10085

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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub AddHeader()
  2.         ' Append to TOP
  3.         Dim _FStream As New System.IO.FileStream("C:\Headerfile.txt", IO.FileMode.Open, IO.FileAccess.ReadWrite, IO.FileShare.ReadWrite, 32, IO.FileOptions.RandomAccess)
  4.         Dim _Reader As New System.IO.StreamReader(_FStream)
  6.         Dim AppendVal As String = ""
  7.         Dim CurVal As String = ""
  9.         AppendVal = "APPENDED HEADER LINE 1"
  11.         ' Read old file in, then Place the Header at the beginning of the stream
  12.         _FStream.Seek(0, IO.SeekOrigin.Begin) ' Set the stream to the beginning of the file
  13.         _Reader = New IO.StreamReader(_FStream)
  14.         While Not _Reader.EndOfStream
  15.             CurVal &= Convert.ToChar(_Reader.Read)
  16.         End While
  17.         AppendVal &= vbNewLine & CurVal
  18.         _Reader.Close()
  20.         ' Write file with header
  21.         _FStream = New System.IO.FileStream("C:\Headerfile.txt", IO.FileMode.Open, IO.FileAccess.Write, IO.FileShare.None, 32, IO.FileOptions.None)
  22.         Dim _Writer As New System.IO.StreamWriter(_FStream)
  23.         _FStream.Seek(0, IO.SeekOrigin.Begin) ' Set the stream to the beginning of the file
  24.         _Writer = New IO.StreamWriter(_FStream)
  25.         _Writer.WriteLine(AppendVal)
  27.         _Writer.Close()
  28.     End Sub
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

Expand|Select|Wrap|Line Numbers
  1. Appendval = "Line 1" & controlchars.newline & "Line 2" & controlchars.newline & "Line 3"
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

Jul 19 '07 #2
211 Expert 100+
Make it simpler:
1. Create a new file called NewFileWithHeader
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

Jul 20 '07 #3
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:\MyHeaderFile"
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.FileStream(sHeaderFile, IO.FileMode.Open, IO.FileAccess.Read)
' Declare Stream reader object and at the same time read the header file in
Dim fsStreamReader_Header As New System.IO.StreamReader(fStream_Header)

' Read the header
sHeaderText = fsStreamReader_Header.ReadToEnd

' Declare Stream object to read batch file and point it to our batch file
Dim fStream_BatchFile As New System.IO.FileStream(sMyBatchFile, IO.FileMode.Open, IO.FileAccess.ReadWrite)
' Declare Stream reader object and at the same time read the batch file in
Dim fsStreamReader_BatchFile As New System.IO.StreamReader(fStream_BatchFile)

' Read the batch file
sSplitBatchText = fsStreamReader_BatchFile.ReadToEnd

' 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_BatchFile.Seek(0, IO.SeekOrigin.Begin)
' 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.StreamWriter(fStream_BatchFile)

' Write our result back to the batch file

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

Dts.TaskResult = Dts.Results.Success
Aug 30 '07 #4
7,872 Expert 4TB
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
Aug 30 '07 #5
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
Nov 10 '15 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Jesse Noller | last post: by
22 posts views Thread by Ling Lee | last post: by
18 posts views Thread by Vasilis Serghi | last post: by
7 posts views Thread by TheGanjaMan | last post: by
6 posts views Thread by ivan.perak | last post: by
3 posts views Thread by Barkingmadscot | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.