SQL PHP HTML ASP JavaScript articles and free scripts to download
 

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>
</form>
<%
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)
OpenFileobj.WriteLine(email)
OpenFileobj.Close
Set OpenFileobj = Nothing
Else
Response.Write "File does not exist"
End if
Set FSOobj = Nothing
End if
%>
Further readings
Server.MapPath to convert virtual path to physical path of files and directory.
FileSystemObject:Checking file or folder exist by using File system object .
Getting the file name from full path of the current running script in ASP.
Getting the file extension and file name by using FSO
Deleting file by using DeleteFile in FSO
Getting the Last updating and creation date and time of a file.
Listing all files present inside a folder using FileSystemObject
Reading data from a text file using File System Object
Writing data to a text file using File System Object
Read & Writing data to a text file: Developing a page hit counter
Appending data to a text file: Storing emails in a text file
 
Scripts
PHP
JavaScript
All ASP Tutorials
File Management
MapPath
FileSystemObject
append
name
Updating Time
Listing all files
Reading file
Writing file
Read & Write
File type
Delete File
Popular Tutorials
Managing two drop downs
ASP Tutorials
Date and time
Declaring array
Form in ASP
Server.MapPath
Date Time & MSSQL
Select Query
File System Object
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.