|
|
| READ TEXTFILES IN ASP |
|
When you want to store some data, either how many viewers your page have registered,
or you want to make a simple guestbook, you have to store the data. Textfiles is
a rather easy way to do this. In this tutorial I will show how to read and manipulate
some data from the textfile.
|
The Textfile
|
|
I have called this textfile, apron.txt. It is a textfile with the content below:
|
This is the first textfile I will read.
Asp has simple methods to read from textfiles.
Is it easy? make your own cool project with textfiles
and asp, and submit it in apron guest developer
section. This is a nice way to show your work.
It is your creativity that result in the project's
quality.
|
Get the content of the textfile
|
|
In order to get the content of the textfile, you have to open it,
and then read it. Under we set the content to a variable, close the file
and then write it out.
|
'give the name to the file
Dim filename, content
filename = "apron.txt"
Set fs=Server.CreateObject("Scripting.FileSystemObject")
'open the file
Set f=fs.OpenTextFile(Server.MapPath(filename), 1)
content = f.ReadAll
'write the content of the textfile
Response.Write(content)
'close the file
f.Close
'empty the objects
Set f=Nothing
Set fs=Nothing
|
|
As you can se in the example above the linebreaks from the textfile does not
appear. We read one line at the time and puts a break at the end of the line.
|
Dim filename, content
filename = "apron.txt"
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath(filename), 1)
do while f.AtEndOfStream = false
content = content & f.ReadLine & " "
loop
Response.Write(content)
f.Close
Set f=Nothing
Set fs=Nothing
|
Get a specific part of the textfile
|
|
If you want to get a specific part of the textfile, you
can use string methods on the $content string.
But if this is neccesary I recommend that you use a
xml files or a database to store your data in. I have made some
methods for this in the wrapper class for textfile. You get get
this class and read more about it here.
|
|
Category
|
Programming with ASP
|
|
Author
|
Tommy Johannessen
|
|
|
|