Append to the top of a file...

Results 1 to 2 of 2

Thread: Append to the top of a file...

  1. #1
    Karl Guest

    Default Append to the top of a file...

    Anyone know if there&#039s a way to append to a top of a file using the file system object, or any other way?<BR><BR>--<BR>Karl

  2. #2
    Join Date
    Dec 1969

    Default RE: Append to the top of a file...

    It can&#039t be done, as you describe it.<BR><BR>Consider this: Despite the fact that files are stored on disk that can randomly find anything anywhere thereon, each file is really simply a sequence of bytes. Like a videotape or audio tape. <BR><BR>You can use the fast-forward and rewind buttons to get to any spot on the tape and then you can start viewing/listening/reading *OR* you can then start recording/writing. BUT...<BR><BR>If you forward/rewind to any place that is already recorded and then start recording, you will write over the top of what is already there.<BR><BR>*ONLY* if you fast forward to the very end of the already recorded parts can you safely write/record something without mucking up what is already on the tape (or in the file). In computer lingo, this last capability would be called "appending to a file" and is exhibited in the Scripting.FileSystemObject by doing OpenTextFile with the mode "ForAppending" (8) ... which really means "open the file and then fast forward immediately to its end so I can add more on the end."<BR><BR>So, of course, there is no way to "append" to the "top" of a file.<BR><BR>What can you do?<BR><BR>Basically, only one thing: Create a new file (with a temporary name). Write out your new information. And then copy all the info from the old file after that. Delete the old file. Rename the temporary file to the same name as the old file. You are done.<BR><BR>Finally, we might note that this is one major reason to use a database instead of a file. With a database, even if you have 1000 records already in place, you can (usually) shove in another one and put it "ahead" of the ones already there by simply giving it a "primary key" value that is lower than that of any other record. Voila.<BR><BR>So if the create new--write--copy--delete old--rename new scheme doesn&#039t seem viable to you, then maybe it is time to learn about databases?<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts