The Magic function of the FSO in ASP

Source: Internet
Author: User
Tags file system readline
FSO|FSO Reprint

FSO Model Object
Drive object: Drive objects for access to disks or network drives
FileSystemObject object: File system objects for accessing a computer's file system
Folder object: Folders objects for accessing all properties of a folder
TextStream object: Text Stream objects for accessing file contents

You can use the object above to do anything on the computer, including sabotage;-( Therefore, please use the FSO carefully. In a Web environment, storing information is very important, such as user information, log files, and so on. The FSO provides a powerful and easy way to save data efficiently. In this article, we focus on FileSystemObject and TextStream objects.

FSO is supported by Microsoft and presumably no longer uses ASP for non-Windows systems.

How to use the FSO?

To perform all of the work using the FSO, you first create the object, as in the following code:

<%
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
% >

This creates the FSO and assigns the variable FSO, and then you can use the familiar Object.Method syntax to perform file system operations (view the Visual Basic documentation for more information on object and Object Wizard programming). Here we can use Fso.method or Fso.property, which will be seen in the following example.

The FSO model is located in the Script runtime DLL file provided by Microsoft, which is scrrun.dll. You can refer to this DLL file in any application, such as Ms Access,word. That is, it is not limited to applying it in ASP.

Here is a brief list of FSO methods:

Fso method
CopyFile copy one or more files to a new path
CreateTextFile creates a file and returns a TextStream object
DeleteFile Delete a file
OpenTextFile opens the file and returns the TextStream object to read or append

For full FSO methods and properties, check out Microsoft MSDN. Let's look at a few examples.


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" 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 the" 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


Now there are some user information saved in the file, just like a simple database. Suppose a user wants to know all of the visitors, from the login
Separate the related parts from the information in the note, because there is no structured column like a database.

We know that in the file created, line 1th is user name, line 2nd is their home page, and line 3rd is their e-mail address. subsequently registered with
Households also store their information according to such a structure, so every 3 rows will contain a user's registration information. Knowing these, you can write the following code to display
Show Information:

<%
"Create" FSO Object
Set fso = Server.CreateObject ("Scripting.FileSystemObject")
Path = "C:emp est.txt"

"Open the" file
Set file = Fso.opentextfile (path, 1) <-for
Reading

Next, analyze each row and format the data:

Do until file. AtEndOfStream
Response.Write ("Name:" & file. ReadLine & "")
Response.Write ("Home Page:" & file.) ReadLine & "")
Response.Write ("Email

[1] [2] [3] Next page



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.