Introduction to ASP Programming (18): FSO Component file operation (top) _asp Foundation

Source: Internet
Author: User
FSO In addition to the drive, folder operation, the most powerful function is the operation of the file. It can be used for counting, content management, searching, generating Dynamic HTML pages, and so on.

first, FSO. OpenTextFile
Needless to say, FSO. OpenTextFile is to open a file, under normal circumstances is open txt text file. So first we create a TXT file, and then through the FSO to read the content.

1,info.txt

Name:cnbruce
Sex:male


Set up the file, and then make an ASP page, of course, the best 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 open the drive through the FSO, open the folder, open the file, and then access the open database, you can only open Absolute Physical path address。 But the general situation is to upload to the space service provider that can not very directly understand the location of their own files, so highly recommended use of Server.MapPathMethods: The platform has strong portability and strong applicability.

CreateObject ("Scripting.FileSystemObject")A connection to the FSO component was established, FSO. OpenTextFile (whichfile,1)The Info.txt file is open. The argument "1" means "ForReading: Open the file as read-only." Can't write this file. , and the other parameter "2" means "ForWriting: Open File as Write", and parameter "8" means "ForAppending: Open the file and start writing at the end of the file."

Open the file, and then do you want to display the contents of the file? Then it's through. txt. ReadLinemethod to read the text in the a whole line, continue using TXT if you need to continue reading the next line. ReadLine method. Of course, there are other ways to read this at first, such as txt. Read (7)Reads a specified number of characters, txt. ReadAllReturns the entire contents of the text.

Second, FSO. CreateTextFile
such as FSO. CreateFolder Create a folder-like FSO. CreateTextFile is the establishment of the document.

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 file created this time is the previous Info.txt file, FSO. CreateTextFile (whichfile,true)The parameter true indicates that an existing file can be overwritten. After the establishment of the need to add data to the use of " MyFile.WriteLine"Up.

Now you can create a simple text register, remember the previous count? : 1, through the application, session, Global.asa to count, 2, through the counter component for the number of notes. But both have a common problem, that is, can not be saved, if the server reboot, is not all the number of the total empty it: Then you can use the text to record the data, even if the restart, the next fetch is also the file.

Experiment: Text counter

First, establish a number of text file Counter.txt, set the initial value of "1"

4,counter.txt

1

Then is the count of the ASP file, the function is to display the text of the count, this do plus 1 of the count, and then also to write a new count to the text file.

5,txtcount.asp


<%
Whichfile=server.mappath ("Counter.txt")
' Open the file and read its value, then close the connection to release the resource
Set Fso=createobject ("Scripting.FileSystemObject")
Set Openfile=fso.opentextfile (whichfile,1)
Visitors=openfile.readline
Openfile.close
' page displays the contents of the count and adds 1.
Response.Write "You are the first &visitors& guest of this page"
Visitors=visitors+1
' Adds a new numeric value to the text, and finally closes all connections to free resources
Set Creatfile=fso.createtextfile (Whichfile)
Creatfile.writeline (visitors)
Creatfile.close
Set fso=nothing
%>



That can continue to expand the content according to this: for example, let the count be displayed with a digital picture. Of course, the premise is that you need 0-9 of the 10 picture, and put this picture in the IMG folder.
Next to an 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 ' represents a 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
%>



In this program is the mid function, the function is to return A StringIn from first fewCharacter to start A fewCharacter. The format is: Mid (string,start,length)
<textarea class="bk" id="temp57246" rows="12"><script language=vbs> cn_string= "Cnbruce love cnrose" Cn_start = 9 Cn_length = 4 Alert (Mid Cn_string,cn_start, cn_length)) </script></textarea>
[Ctrl + a All choose to copy tips: You can modify some of the code, and then click to Run]
Effects 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.