469,306 Members | 1,628 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

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

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)
  5.  
  6.         Dim AppendVal As String = ""
  7.         Dim CurVal As String = ""
  8.  
  9.         AppendVal = "APPENDED HEADER LINE 1"
  10.  
  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()
  19.  
  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)
  26.  
  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

Cyberdaemon
Jul 19 '07 #2
vanc
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

cheers.
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
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.Success
Aug 30 '07 #4
Plater
7,872 Expert 4TB
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
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
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.