The magic of FSO in ASP

Source: Internet
Author: User

From: http://www.HackFree.net Author: Unknown

In ASP, FSO indicates File System Object, that is, File System Object.

The computer file system we want to manipulate is located on the web server. So confirm that you have the appropriate permissions. Ideally, you can create a web server on your own machine to facilitate testing. If you are running on Windows, try Microsoft's free personal Web Server PWS.

FSO model object
Drive Object: Drive Object for disk access or network Drive
FileSystemObject Object: File System Object for accessing the computer's File System
Folder Object: all attributes of a Folder Object for access
TextStream Object: Text Stream Object for accessing File Content

You can use the above objects to do anything on the computer, including destructive activities;-(so please be careful when using FSO. In the web environment, storing information is very important, such as user information, log files, and so on. FSO provides a powerful and simple way to efficiently store data. In this article, we will focus on FileSystemObject and TextStream objects.

FSO is supported by Microsoft and cannot be used for non-Windows systems.

How to Use FSO?

To use FSO to execute all the work, you must first create an object. The Code is as follows:

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

In this way, the FSO is created and the variable fso is assigned, and then the familiar object can be used. method syntax to perform operations on the file system (view the Visual Basic documentation to learn more about 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 run time DLL file provided by Microsoft. It is scrrun. dll. You can reference this DLL file in any application, such as MS Access and Word. That is to say, it is not limited to be applied in ASP.

Here is a brief list of FSO methods:

FSO Method
CopyFile: copy one or more files to the new path.
CreateTextFile: Creates a file and returns a TextStream object.
DeleteFile: delete an object
OpenTextFile open the file and return the TextStream object for reading or appending

For more information about FSO methods and attributes, see Microsoft MSDN. Here are several examples.

 
Suppose you want to create a simple message book, you can create a database to store user information. However, if you do not need the powerful functions of the database, using FSO to store information will save you time and money. In addition, 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 = "formhandler. asp" 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 look at the code for processing 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, nothing is new. It is nothing more than getting the values of form fields and assigning values to variables. An interesting part is shown below:

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


In retrospect, the OpenTextFile method returns a TextStream object, which is another object in the FSO model. TextStream objects reveal methods for operating file content, such as writing, reading, and skipping a row. The VB constant vbcrlf generates a line break.

The value TRUE is defined in the Command Parameter of OpentextFile, which tells the system to create a file if the file 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: empand open test.txt. You can see the following information:

Users name
Users home page
Users email

 
Now some user information is saved in a file, just like a simple database. Assume that a user wants to know all the visitors
Separated from the information, because there is no structured column like a database.

We know that in the created file, row 1st is the user name, row 2nd is their home page, and row 3rd is their email address. Subsequent Registration
Users store their information according to this structure, so every 3 rows contains a user's registration information. With this knowledge, you can compile the following code to display
Display Information:

<%
Create the fso object
Set fso = Server. Createobject ("Scripting. FileSystemObject ")
Path = "c: emp est.txt"

Open the file
Set file = fso. opentextfile (path, 1) <--
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:" & file. ReadLine & "<p> ")
Loop

Close and clean up
File. close
Set file = nothing
Set fso = nothing
%>

Only a simple output is provided here, but you can include the table or DHTML form information as needed.

If a file has been correctly created and written, the above small loop will properly list the information of everyone in the database. ReadLine method read 1
Line content until a line break is encountered, the subsequent ReadLine call will read the next line. AtEndOfStream is the property of the TextStream object. It tells us when
The end of the file.

Assume that, for some reason, the file is not correctly formed. If a user only has two lines of information instead of three lines, some errors will occur. We
Here, the next three lines of information in the file are retrieved cyclically. If there are no more than three lines of information, the following error message will appear:

Server object error ASP 0177: 800a003e

Therefore, you must add some error processing code to prevent unnecessary rows or unnecessary row information from being inserted in the file.

 
I discussed the basic knowledge above and then talked about permission licensing issues. FSO is run with the user account permission to create it, in other words, if someone is from the Internet
To access your page, then this internet account will create FSO. If you log on to the computer as administrator and log on to the page
The administrator account creates FSO. This is very important because a certain account has certain permissions and FSO requires some permissions to complete
Execute the function.

Internet account (IUSER_MachineName, MachineName is the name of the server) generally only has read permission, which means that users cannot write
Statement file. However, there are several options to bypass this issue.

First, it is also very difficult to ask the user to log on to the server before entering the message book. However, the main point of the message book is to collect information from anonymous users, such
If users are required to log on, they must know who they are. Therefore, skip this selection and look at the next one.

The first method is to create a directory or file. The IUSER_MachineName user has the write permission for this. This may expose some potential security vulnerabilities.
Because anyone who knows the correct directory and has certain web skills can fill the content on the server. This is a serious taboo. So you must confirm that
The hidden location stores the information of these writable directories and tries to set these directories out of the web directory structure (for example, in Windows, This Is
Is not in the inetpub directory ).

 
You may think: Well, now I know how to write files. But can we do more? Next, let's try to create a search function for the web site.

The key to building a search engine is recursion. Mainly, write a piece of code to search for files under the directory, and then execute the same code for all directories cyclically. Because
To determine the total number of subdirectories, you must execute the search code over and over again until the end of the search. Recursive call is very good!

Create a search page. Assume that an HTML form has been created and the user enters a search string.

Dim objFolder
Dim strSearchText
Dim objFSO

StrSearchText = Request. Form ("SearchText") <-- The search string
Create the FSO and Folder objects
Set fso = Server. CreateObject ("Scripting. FileSystemObject ")
Set objFolder = objFSO. GetFolder (Server. MapPath ("/"))

Search objFolder


The above code initializes the variable. The Search function executes the Search function, which is described as follows:


Function Search (objFolder)

Dim objSubFolder


Loop through every file in the current
Folder

For Each objFile in objFolder. Files

Set objTextStream = objFSO. OpenTextFile (objFile. Path, 1) <-- For Reading


Read the files contents into
Variable

StrFileContents = objTextStream. ReadAll


If the search

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.