Appending data to file in ASP

We have seen how we can write data to a text file. While writing the old data of the file is always deleted, so to add new data ( fresh ) every time we can use write method. Some time we may need to keep the old data and add new date to the file. Here we have to use the append mode to write to a file. In append mode new data is written or added at the end of the file.

Please read the tutorial on writing data to a file using FSO. In the same ASP ( VBScript) script if the mode is changed to append mode then the data will be written at the end of the file without deleting the old data. So just change the constant value and the mode of the file opening. Here are the changes required.

Const Appending=8
Set OpenFileobj = FSOobj.OpenTextFile(FilePath, Appending)

You can easily locate the above two changes in the script given at write data tutorial and replace it.

Now let us try something different for our understanding using this file appends method. We will develop one form to store the email submitted by the users. Each time one visitor submits an email address it has to be added at the end of the file. Note that this is not a newsletter script and many other features like checking the email address etc are not discussed here. This is to be used only to understand how the file appends system works.

We will display one simple form which submits to itself and if the length of the data submitted is more than 2 char length then by using one if condition we will execute the append script.

Please create a blank file as text.txt in the same directory and execute the script. Here is the full code.

<form method=post action=''>
<input type=text name=email><input type=submit value=Submit>
Dim email
email = Request("email")
if len(email) > 2 Then
Const Appending=8
Dim OpenFileobj, FSOobj,FilePath
FilePath=Server.MapPath("text.txt") ' located in the same directory
Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")
if FSOobj.fileExists(FilePath) Then
Set OpenFileobj = FSOobj.OpenTextFile(FilePath, Appending)
Set OpenFileobj = Nothing
Response.Write "File does not exist"
End if
Set FSOobj = Nothing
End if

Be the first to post comment on this article :

Post your comments , suggestion , error , requirements etc here .

We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2022 All rights reserved worldwide Privacy Policy Disclaimer