FSO Author: Gan Ganping;
Suppose you want to create a simple guest book, you can build a database in which to store the user's information. However, if you don't need the power of the database, using the FSO to store the information will save you time and money. Also, some ISPs may limit database applications on the Web.
Suppose you collected some user information in a form, here is a simple form HTML code:
< html>
< body>
< form action= "formhandler.asp" method= "POST" >
< input type= "text" size= "ten" name= "username" >
< input type= "text" size= "ten" Name= "homepage" >
< input type= "text" size= "ten" Name= "Email" >
</form>
</body>
And look at the code that handles the form in formhandler.asp:
<%
' Get form info '
StrName = Request.Form ("username")
Strhomepage = Request.Form ("homepage")
Stremail = Request.Form ("Email")
' Create ' the FSO object
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
So far, there's nothing new, just getting the value of a form field and assigning it to a variable. Here's the interesting part-write the file:
Path = "C:emp est.txt"
ForReading = 1, ForWriting = 2, ForAppending = 3
' Open ' file
Set file = Fso.opentextfile (path, ForAppending, TRUE)
' Write the info to the ' file
File.write (strName) & vbCrLf
File.write (strhomepage) & vbCrLf
File.write (stremail) & vbCrLf
' Close and clean up
File.close
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 the contents of a file, such as writing, reading, and skipping a line. The VB constant vbCrLf produces a newline character.
True is defined in the OpenTextFile command argument, which tells the system to create a file if it does not exist. If the file does not exist and the true parameter is not defined, an error occurs.
Now go to the directory c:emp, open test.txt, you can see the following information:
User ' s name
User ' s home page
User ' s Email
Of course, these words can be replaced by any content entered in the form.