SoFunction
Updated on 2025-04-14

The magical function of FSO in ASP - Write files

Author: Gan Jiping;


Suppose you want to create a simple guestbook where you can create a database where you store user information. However, using FSO to store information will save you time and money if the power of the database is not required. And, some ISPs may restrict database applications on the web.

Suppose you have collected some user information in a form, here is a simple form HTML code:

< html>
< body>

< form action="" method="post">
< input type="text" size="10" name="username">
< input type="text" size="10" name="homepage">
< input type="text" size="10" name="Email">
< /form>
< /body>
< /html>

Let's take a look at the code that processes the form:

< %
' Get form info
strName = ("username")
strHomePage = ("homepage")
strEmail = ("Email")

' create the fso object
Set fso = ("")

So far, nothing new is nothing more than getting the value of the form field and assigning values ​​to variables. An interesting part appears below - Write a file:

path = "c: emp "
ForReading = 1, ForWriting = 2, ForAppending = 3

' open the file
set file = (path, ForAppending, TRUE)

' write the info to the file
(strName) & vbcrlf
(strHomePage) & vbcrlf
(strEmail) & vbcrlf

' close and clean up

set file = nothing
set fso = nothing


Recall that the OpenTextFile method returns a TextStream object, which is another object in the FSO model. The TextStream object reveals ways to manipulate file content, such as writing, reading, and skipping a line. The VB constant vbcrlf produces a newline character.

TRUE is defined in the command parameter of OpentextFile, which tells the system that if the file does not exist, create it. If the file does not exist and the TRUE parameter is not defined, an error occurs.

Now go to the directory c:emp, open it, you can see the following information:

User's name
User's home page
User's email

Of course, these words can be replaced by anything entered into the form.