Your source for ASP-related frequently asked questions and answers! : ASP FAQS : FileSystemObject

Question: How do I insert more information into the middle of a text file? How do I delete something from a text file? How do I add to the front of a text file?

Answer: Short answer: YOU CAN NOT DO THAT.

Longer answer: First of all, we need to note that this is not a limitation of ASP, of the FileSystemObject, of VBScript, or of the Windows operating system. There is no system in the world, on any kind of computer, that allows any of those operations.

To understand why, you have to understand how data is stored on a disk, especially data in the form of a text file.

A directory entry (a file name) simply points to a starting point of the disk. (Perhaps something in the form of "start at block number 77123".) And then the data starts at that spot and continues, without interruption, to the end of the block. If the data is larger than a block, then again the directory (typically...there are other schemes possible, though none that I know of used in modern operating systems) points to the next block to be used. Again, the data continues at the start of the next block and continues to the end of it. The process is repeated until all the data is written to the disk.

Now, can you dream up a scheme whereby you could insert or remove some data from the middle of that file???

Effectively, you'd have to find a way to "stretch" or "shrink" a block on the disk, wouldn't you? But blocks are always a fixed size (in fact, excepting for a few mainframe operating systems, they are always the same size throughout a given disk drive). You can't change their size! So you can't insert or delete data. Period.

The only way to alter the contents of a text file (other than by appending data to the end of it--which is allowed by Windows) is to read all the data (the entire text file) into memory, change the data, erase the original file, and then write the new data back to the same file.

Period. Honest. That's the only way.

FAQ posted by Bill Wilkinson at 2/25/2001 2:28:52 PM to the FileSystemObject category. This FAQ has been viewed 48,185 times.

Copyright 2018 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers