Building WebApps  «Prev  Next»

Lesson 7Writing a text file
ObjectiveUse the FileSystemObject to create and write a file.

Writing a Text File

As you learned in the previous lesson, The FileSystemObject object has several methods. Of these, we will need to use the CreateTextFile method to create a file.
The CreateTextFile method returns a TextStream object, which has its own methods, including WriteLine and Close. The WriteLine method writes a string to the text file plus a new line character.

Set fileSys = Server.CreateObject("Scripting.FileSystemObject")
textFileName = "c:\testfile.txt" 
Set textStream = fileSys.CreateTextFile(textFileName, True)
textStream.WriteLine("Just writin' a little text.")
textStream.Close

Here is the same code segment with an explanation:
The diagram shown above describes how to create a text ffile.

Writing ASP Text File
The text file that you create could be written to a protected directory that users cannot access, or it could become an order confirmation that is displayed to the user or inserted into an email using stored user information.
Be aware that writing files for multiple users or writing multiple files for the same user over multiple sessions can significantly increase the complexity of your task.