When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Tuesday, April 06, 1999

Reading/Writing Text Files Using ASP
By Ryan S.


Ahh.. That feeling that comes from writing another article. You have to experience to believe it. Anyways, this tutorial is for the beginner to intermediate developer who wants to write to a text file. This purpose can be anywhere from storing text in a nice global format, to writing batch files to execute on the server. Now, belive it or not, I will NOT be linking this to a real world example. Why? Because the only one's I can think of involve ideas I plan to market. ;-) So now we are left with this.

- continued -

Now, I will show you the basic commands for reading and writing, with a little bit of code snippits to keep everyone happy. It is very very simple. First, writing:

set FSO = Server.CreateObject("scripting.FileSystemObject")

This line creates an object that is used for File Access

set myFile = fso.CreateTextFile("C:\test.txt", true)

This creates a blank text file object for us to use.The CreateTextFile object creates a text file, based on what we specified. The first option specifies the location of the text file, while the second one states whether or not to create the file if one doesn't exist. Very simple methods.

Next, we do something simple

myFile.WriteLine("my text here")
myFile.WriteLine("more text here")

.WriteLine writes the text to the text file. What a novel idea! =D

myFile.Close

This closes the file, and makes it accessible to all (for the good old days of File Sharing). It also frees up memory.

Now for Reading:

First, we create an object, but not after defining some constants

Const ForReading = 1, ForWriting = 2, ForAppending = 8

This just allows us the different methods of working with text, when we have an open text file.

set fso = server.CreateObject("Scripting.FileSystemObject")

Here we create our object again.

set f = fso.GetFile("C:\test1.txt")

Here we state that we will be manipulating the file C:\test1.txt . Moderately simple. Now we need to open it as a stream. Get ready, here comes the hard stuff ;-)

set ts = f.OpenAsTextStream(ForReading, -2)

This creates a text stream with our file. It is like a continous flow of information. Now we jsut read a line

TextStreamTest = ts.ReadLine

This code reads a line of information, and puts it into TextStreamTest. Pretty simple, isnt it?! I hope so. Anyway, I've discussed the code, and I shall let you go.

One last note. Let's say you wanted to read all the contents out a text file. Then you would do this

Do While not ts.AtEndOfStream
     myText = myText & ts.ReadLine & vbCrLf
Loop

This tells it do a loop while there is still text, then read a line and add a line break. Very easy ;-)

God Bless from Ryan S


This article was written by Ryan S. Ryan has been a computer programmer in the loosest sense since the age of 8. He has been working with ASP since the age of 13, when it first came out (that he knows of), and is somewhat advanced at it.


ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article