Active Server Pages: Open, Read and Create files



Open and Read content from a text file

Example 1: This one will be the basic code we need to open a text file:
 

1
2
3
4
5
6
7
8
9
10
11
12
<%
Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt")
filecontent = wfile.ReadAll

wfile.close
Set wfile=nothing
Set fs=nothing

response.write(filecontent)
%>

Line 2 will create the appropriate environment which allows to perform the operations involving files in the server. We have defined a variable named "fs" to do it  (we may change the name of this variable).

In line 4 we have create a new variable named "wfile" and we have apply the method OpenTextFile to variable "fs". We have also define which is the exact location of the file we want to open in this line (the complete path is necessary).

In line 5 we have read all the content of the file to a variable named "filecontent" using the instruction "ReadAll".

Lines 7 to 9 are use to let the server know we have finished all operations involving files.

In line 11 we have response to the client with the content in the variable "filecontent".
 

Example 2: Let's suppose we have a file with different kind of information in each line (a name in the first line, the last name in the second one, and the age in the third one), and we want to use them separately. This one will be the script we may use:
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%
Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt")
firstname = wfile.ReadLine
lastname = wfile.ReadLine
theage = wfile.ReadLine

wfile.close
Set wfile=nothing
Set fs=nothing

%>

Your first name is <% =firstname %><BR>
Your last name is <% =firstname %><BR>
Your are <% =firstname %> years old<BR>

This example is very similar to the previous one, but in this case each line we have read from "myfile.txt" has been saved to a different variable (lines 5 to 7), and they have been used in lines 15 to 17 to respond to the client.

Example 3: This example will read all lines in the file, and the response page will include the content of each line with its line number.
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%
Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt")

counter=0
do while not wfile.AtEndOfStream
 counter=counter 1
 singleline=wfile.readline
 response.write (counter & singleline & "<br>")
loop

wfile.close
Set wfile=nothing
Set fs=nothing

%>

In line 6 we will define the variable "counter", and in line 7 to 11 we will repeated instructions within the Do_While _Loop until the file does not reach the end of the file (the condition is  "not wfile.AtEndOfStream").

Example 4: Let's suppose we have a file with a number in line 1 and a second number in line 2.
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%
Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt")

number1 = Clng(wfile.ReadLine)
number2= Clng(wfile.ReadLine)

number1and2 = number1 number2
response.write (number1and2)

wfile.close
Set wfile=nothing
Set fs=nothing

%>

In the previous examples we were able to save the value in a line to a variable, but that variable was a string class variable. In this example we have saved the content of line 1 and line 2 to variables "number1" and number2" by using the function "Clng".  This function has allow us to add both numbers in line 9 and send the result to the client (line 10).
 

Create and Write a text file

Example 1: The basic code we need to create a file is very similar to that one we have used to open a file:
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%
thetext="Write this text in the file"

Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.CreateTextFile("c:\Mydir\myfile.txt", True)
wfile.Write (thetext)

wfile.close
Set wfile=nothing
Set fs=nothing

response.write("Text created")
%>

The differences to instructions when opening a file are line 6 and line 7:

The method used in line 6 is "CreateTextFile"; it is necessary to indicate the complete path to the file we want to create; in line 6 we may use the instruction True (to allow over-writing an existing file) or False (if the file exits, it is not over-written).

Line 7 will write in the file the string in variable "thetext".
 
 

We may also use this instruction to add content to the file

wfile.WriteLine (thetext1)
wfile.WriteLine (thetext2)
...

In this case we will write the content in variable "thetext1" in line 1, content in "thetext2" in line 2 etc.
 
 

Example 2: Let suppose we want to record the IP address of all visitor to our page to a file named "mylog.txt".
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%
VisitorsIP=Request.ServerVariables ("REMOTE_ADDR")

Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.OpenTextFile("c:\Mydir\mylog.txt", 8,false,0)
wfile.WriteLine (VisitorsIP)

wfile.close
Set wfile=nothing
Set fs=nothing

response.write("IP registered")
%>

The IP address is requested in line 2 (check Functions and Procedures). In this case we have open the file "mylog.txt" in line 6 with the instruction "forappending". this instruction will allow us to open the file and add at the end of it the IP address of our last visitor.

 

Tutorial home page