Lesson 16: Writing to a text file

In the previous lesson, we learned to read from a text file. In this lesson, we will learn to write to a text file.

The two methods are very similar, but there is one very important difference: You must have write permissions to the file. This means that the file will have to be located in a folder where you have the necessary permissions.

With most web hosts, you will normally have one folder with write permissions. It's often called something like "cgi-bin", "log", "databases" or something similar. Read more on your web host's support pages. If you work locally on your own computer, you can set the permissions yourself: right-click on the folder and choose "Properties".

Note that it is the text file that needs to be in the folder with write permissions - not the ASP file.

Open the text file for writing

In the same way as when reading from a text file, the DocumentationOpenTextFile method is used for writing, but this time we set the mode to 2 (ForWriting) or 8 (ForAppending).

The difference between ForWriting and ForAppending is where the 'cursor' is located - either at the beginning or at the end of the text file.

In the examples in this lesson we use an empty text file called textfile.txt - but you can, of course, make your own text file if you like.

First, let us try to open the text file for writing:


	<%
	' Variables
	Dim fso, f, filespec

	' FileSystemObject

	Set fso = CreateObject("Scripting.FileSystemObject")

	' Find the physical location of the text file
	filespec = Server.Mappath("/tutorials/asp/textfile.txt")

	' Open the text file
	Set f = fso.OpenTextFile(filespec,2)

	'Close the text file
	f.Close

	%>
	
	

Example 1: Write a line to the text file

To write a line we must use the method DocumentationWriteLine, like this:


	<html>

	<head>
	<title>Reading from text files</title>
	</head>
	<body>
	<%
	Dim fso, f, filespec

	Set fso = CreateObject("Scripting.FileSystemObject")
	filespec = Server.Mappath("/tutorials/asp/textfile.txt")

	Set f = fso.OpenTextFile(filespec,2)

	' Write a text line

	f.WriteLine "VBScript and ASP is fun!" 

	 'Open file for reading, and read the line 
	Set f = fso.OpenTextFile(filespec,1)
	Response.write f.ReadLine 

	f.Close
	%>

	</body>
	</html>
	
	

Since we opened the file "ForWriting", the line is added at the top, and thus overwrites the existing line. If we instead open the file "ForAppending", the line is added at the bottom of the text file, which then will increase by one line each time it's written to.

Example 2: Adding a text block to a text file

Of course, it is also possible to add an entire text block, instead of just a single line. This is done with the method DocumentationWrite, like this:


	<html>
	<head>
	<title>Reading from text files</title>
	</head>
	<body>
	<%

	Dim fso, f, filespec

	Set fso = CreateObject("Scripting.FileSystemObject")
	filespec = Server.Mappath("/tutorials/asp/tekstfil.txt")
	Set f = fso.OpenTextFile(filespec,2)

	' Write text line

	f.Write Request.Form("TextBlock") 

	' Open file for reading and read line by line
	Set f = fso.OpenTextFile(filespec,1)

	Do While Not f.AtEndOfStream 
	   Response.write f.ReadLine & "<br />" 

	Loop 
	f.Close
	%>
	</body>

	</html>
	
	

In the next lessons, we look at another way of storing data: databases.



<< Lesson 15: Reading from a text file

Lesson 17: Databases >>