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

append to the beginning of a text file

P: n/a
I want to add a new line to the begining of a text file. I dont want to
write over existing data. How do I do this? the file is about 7 mb.
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
* "Eddie Suey" <es***@inBizServices.com> scripsit:
I want to add a new line to the begining of a text file. I dont want to
write over existing data. How do I do this? the file is about 7 mb.


You will have to move the whole contents of the file...

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #2

P: n/a
trick:

(1) copy the existing data of notepad file to a textbox1
(2) delete the file.
(3) create the file with same name and copy the new text (which you want to
put in the beginning)
(4) now append the file and copy the contents of textbox1

"Eddie Suey" <es***@inBizServices.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I want to add a new line to the begining of a text file. I dont want to
write over existing data. How do I do this? the file is about 7 mb.

Nov 20 '05 #3

P: n/a
Eddie,
As the others have suggested you cannot do this.

Largely because of the size of the file. I would consider writing the new
line to a new file, then using Asynchronous File I/O to append the existing
file on the end of this new file, delete the old file, then rename the new
file to the original name...

Similar to:
http://msdn.microsoft.com/library/de...nousfileio.asp

Hope this helps
Jay

"Eddie Suey" <es***@inBizServices.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I want to add a new line to the begining of a text file. I dont want to
write over existing data. How do I do this? the file is about 7 mb.

Nov 20 '05 #4

P: n/a
Cor
Hi Eddie,
That is one of first reasons why people started to make databases.
You can now even do more with it,
Maybe a good idea to look if a simple database is not a better approach for
you.
Cor
Nov 20 '05 #5

P: n/a
* "Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> scripsit:
Largely because of the size of the file. I would consider writing the new
line to a new file, then using Asynchronous File I/O to append the existing
file on the end of this new file, delete the old file, then rename the new
file to the original name...


This will change the creation timestamp of the file.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #6

P: n/a
Herfried,
If the creation timestamp is important, then this would be a downside of the
method.

However! ;-)

I have not played with IO.FileInfo enough, the CreationTime property is read
write, you could 'copy' the attributes from the original file's FileInfo to
the new file's FileInfo and that would update the actual file, thus
preserving the creation timestamp...

Hmm... Touch.NET (simple sample Console Application demonstrating the above)

Public Module MainModule

' args(0) = file to change
' args(1) = properly formatted creation date time

Public Sub Main(ByVal args() As String)
Dim fi As New IO.FileInfo(args(0))
fi.CreationTime = DateTime.Parse(args(1))
End Sub

End Module

Which will set the creation time of the file given to the date given.

Something like:
Touch myfile.txt 10/1/2003

Hope this helps
Jay

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
* "Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> scripsit:
Largely because of the size of the file. I would consider writing the new line to a new file, then using Asynchronous File I/O to append the existing file on the end of this new file, delete the old file, then rename the new file to the original name...


This will change the creation timestamp of the file.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #7

P: n/a
* "Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> scripsit:
If the creation timestamp is important, then this would be a downside of the
method.

However! ;-)

I have not played with IO.FileInfo enough, the CreationTime property is read
write, you could 'copy' the attributes from the original file's FileInfo to
the new file's FileInfo and that would update the actual file, thus


This will work. It was only a little note because most people forget
that the creation date may sometimes be useful...

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #8

P: n/a
Herfried,
Hey! its an important caveat to note!

Not only that it gave me an opportunity to play with System.IO.FileInfo,
something I have not played with a lot.

Thanks
Jay

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
* "Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> scripsit:
If the creation timestamp is important, then this would be a downside of the method.

However! ;-)

I have not played with IO.FileInfo enough, the CreationTime property is read write, you could 'copy' the attributes from the original file's FileInfo to the new file's FileInfo and that would update the actual file, thus


This will work. It was only a little note because most people forget
that the creation date may sometimes be useful...

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.