fso| Programming |fso FSO In addition to the drive, folder operation, the most powerful 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 a drive through the FSO, open a folder, open a file, or open a database that you want to access later, you can only open the absolute physical path address. But the general situation is to upload to the space service provider that can not be very straightforward to understand the location of their own files, so strongly recommend the use of Server.MapPath method: Platform portability, strong applicability.
CreateObject ("Scripting.FileSystemObject") establishes the FSO component connection, the FSO. OpenTextFile (whichfile,1) opens the Info.txt file. 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? That's through TXT. The ReadLine method reads an entire line in the text and continues using TXT if it is necessary to continue reading the next line. ReadLine method. Of course, there are other reading methods, such as TXT. Read (7) reads a specified number of characters, txt. ReadAll returns 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) where the parameter True means that an existing file can be overwritten. After the establishment of the need to add data to the use of "MyFile.WriteLine".
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 string from the first few characters in the beginning. The format is: Mid (string,start,length)
<script language=vbs>
Cn_string= "Cnbruce Love Cnrose"
Cn_start = 9
Cn_length = 4
Alert (Mid (Cn_string,cn_start,cn_length))
</script>
[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]