Advanced ASP programming (18): File Operations of the FSO component (I)

Source: Internet
Author: User

In FSO, apart from the operations on the drive and folder, the most powerful function is the operations on the file. It can be used to count, manage content, search, and generate dynamic HTML pages.

I. fso. OpenTextFile
Needless to say, fso. OpenTextFile is used to open a file, which is generally a txt text file. Therefore, first create a txt file and then read the content through FSO.

1. info.txt

Name: cnbruce
Sex: male

After this file is created, create an ASP page. Of course, it is best that the two files are in the same directory.

2. opentxt. asp

<%
Whichfile = server. mappath ("info.txt ")
Set fso = CreateObject ("Scripting. FileSystemObject ")
Set txt = fso. OpenTextFile (whichfile, 1)
Rline = txt. ReadLine
Rline = rline & "<br>" & txt. ReadLine
Response. Write rline
Txt. Close
%>

Note: Whether you use FSO to open a drive, open a folder, open a file, or open a database to be accessed later, you can only openAbsolute physical path address. But it is generally uploaded to the space service provider, so you cannot directly learn the location of your file, soServer. mappath is strongly recommended.Method: The platform has high portability and applicability.

CreateObject ("Scripting. FileSystemObject ")Established a connection to the FSO component,Fso. OpenTextFile (whichfile, 1)Open info.txt. Parameter "1" indicates "ForReading: open the file in read-only mode. This file cannot be written .", The other parameter "2" indicates "ForWriting: open the file in write mode", and parameter "8" indicates "ForAppending: open the file and start writing from the end of the file ".

After opening the file, do you want to display the content in the file? Then passTxt. ReadLineMethod to readOne lineTo continue reading the next row, use the txt. ReadLine method. Of course, there are other reading methods at the beginning, suchTxt. Read (7)Reads a specified number of characters,Txt. ReadAllReturns all content in the text.

Ii. fso. CreateTextFile
For example, if fso. CreateFolder creates a folder, fso. CreateTextFile creates a file.

3. creattxt. asp

<%
Whichfile = server. mappath ("info.txt ")
Set fso = CreateObject ("Scripting. FileSystemObject ")
Set MyFile = fso. CreateTextFile (whichfile, True)
MyFile. WriteLine ("My Name Is CN-Bruce ")
MyFile. WriteLine ("My Sex Is Male ")
MyFile. Close
%>
<A href = "opentxt. asp"> View content </a>

The current file is the info.txt file,Fso. CreateTextFile (whichfile, True)If the parameter is true, the existing file can be overwritten. After the creation, you need to add data to it and then use"MyFile. WriteLine.

Now we can create a simple text calculator. Remember the previous one? : 1. Count by application, session, and global. asa; 2. Count by Counter component. But there is a common problem between the two, that is, they cannot be saved. If all the records are cleared after the server is restarted, you can use text to record the data now, even if you restart, the file is extracted next time.

Test: Text counter

First, create a counter file counter.txt and set the initial value to "1"

4,counter.txt

1

The following is the count ASP file. The function is to display the count of the text. In this example, add 1 to the count, and then write the new count into the text file.

5, txtcount. asp

<%
Whichfile = server. mappath ("counter.txt ")
'Open the file, read its value, and close the connection to release the resource.
Set fso = createobject ("Scripting. FileSystemObject ")
Set openfile = fso. opentextfile (whichfile, 1)
Visitors = openfile. readline
Openfile. close
'The page displays the Count content and Adds 1.
Response. write "you are the first on this page" & visitors & "guest"
Visitors = visitors + 1
'Add new values to text, and close all connections to release resources
Set creatfile = fso. createtextfile (whichfile)
Creatfile. writeLine (visitors)
Creatfile. close
Set fso = nothing
%>

Based on this, you can continue to expand the content, for example, to display the records in digital images. Of course, the premise is that you need 10 records of 0-9 images and place the images in the img folder.
Next is the enhanced txtcount. asp content code.

<%
Whichfile = server. mappath ("counter.txt ")

Set fso = createobject ("Scripting. FileSystemObject ")
Set openfile = fso. opentextfile (whichfile, 1)
Visitors = openfile. readline
Openfile. close
CountLen = len (visitors)
Response. write "you are the first on this page"

For I = 1 to 6-countLen 'indicates the maximum value of 999999.
Response. write " </img>"
Next
For I = 1 to countlen
Response. write " </img>"
Next
Response. write "guest"

Visitors = visitors + 1
Set creatfile = fso. createtextfile (whichfile)
Creatfile. writeLine (visitors)
Creatfile. close
Set fso = nothing
%>

The mid function is used in this program. The function is used to returnA stringFromNumber of digitsCharacterSeveralCharacter. Format:Mid (string, start, length)
<Script language = vbs> <br/> cn_string = "cnbruce love cnrose" <br/> cn_start = 9 <br/> cn_length = 4 <br/> alert (mid (cn_string, cn_start, cn_length) <br/> </script> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]
Results page see http://www.cnbruce.com/code/txtcount.asp

Count Text View: http://www.cnbruce.com/code/counter.txt

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.