Write to a file

An important technique to learn in ASP is writing to files this can be the basis for more complex scripts such as counters , guestbooks and simple link management systems . Here we will show you how to write a couple of lines of text to a text file :

Instructions :

Create a text file called samplefile.txt , do not enter any data in it and place it in the same folder as the script .

Now enter the following code.

<%
'our constant
Const ForWriting = 2
'declare our variables
Dim objFSO , objTextStream , file
'path to our file
file = Server.MapPath("samplefile.txt")
'create an instance of file object
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'open the text file
Set objTextStream = objFSO.OpenTextFile(file , ForWriting , True)
'write a line of text
objTextStream.WriteLine "We are writing a line of text to our text file"
'and another
objTextStream.Write "This is our second line of text" & VBCrLf
'display a message to confirm message written
Response.Write "we have written to our text file"
'close textstream
objTextStream.Close
'destroy objects
Set objTextStream = Nothing
Set objFSO = Nothing
%>

Books