In addition to the operation of the drive, folder, FSO is the most powerful function of the file. It can be used to count, content management, search can also generate dynamic HTML pages, and more. First, fso.opentextfile does not need to say, fso.opentextfile is open a file, generally the open TXT text file. So first we build a TXT file first, then read the contents through FSO. 1, Info.txt
Name: CNBRUCESEX: MALE
Established this file, then do an ASP page below, of course, the two files are under the same directory. 2, OpenTxt.asp
<% WhichFile = Server.mappath ("info.txt") set fso = createObject ("scripting.filesystemObject") set txt = fso.opentextfile (Whichfile, 1) rline = txt.readlinerline = rline & "
" & TXT.READLINERESPONSE.WRITE RLINETXT.CLOSE%>
Note: Whether you turn on the drive through the FSO, open the folder, open the file, and the open database you want to come into contact, you can only open the absolute physical path address. However, the general situation is to upload the space service provider, and it is impossible to learn from the location of its own file, so it is highly recommended to use the server.mappath method: the platform is strong, and the applicability is strong. CreateObject ("scripting.filesystemObject") establishes the connection of the FSO component, FSO.OpenTextFile (WhichFile, 1) Opens info.txt. Parameter "1" means "Forreading: Open file in read-only mode. You can't write this file." And start from the end of the file. " Open the file, isn't it to display the content in the file? Then read the entire line in the text through the txt.readline method. If you need to continue to read the next line, continue using the TXT.Readline method. Of course, there are other reading methods, such as TXT.READ (7) read the specified number of characters, TXT.Readall returns all the contents in the text. Second, fso.createtextfile is like FSO.CREATEFOLDER to create a folder, fso.createtextFile is a built 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%> View Content
The file established is the previous info.txt file, and the parameter true in fso.createtextfile (Whichfile, true) is indicating that the existing file can be overwritten. "MyFile.WriteLine" is used after the addition of data needs to be added. That can now create a simple text record, remember the previous count? : 1, by Application, Session, Global.asa; 2, the count of the Counter component. But both have a disease, just can't save, if the server is restarted, all the notes are all empty :) Now you can use the text to record data, even if you restart, the next extraction is also the file. . Test: The text counter first established a text file counter.txt, set the initial value of "1" 4, counter.txt1
Then, the ASP file of the count, the function is to display the number of texts, the number of points plus 1, and then write the new record to the text file. 5, TXTCOUNT.ASP
<% Whichfile = server.mappath ("counter.txt") 'Opens the file and reads its value, and finally closes the connection release resource set fso = creteObject ("scripting.filesystemObject") set openfile = fso.opentextfile (Whichfile, 1 Visitors = OpenFile.readLineOpenFile.close 'page Display count content and add 1 arithmetic response.write "You are the" & Visitors & "Visitor" Visitors = VITORS 1' to add new value to text Finally close all connection release resources set createfile = fso.createtextFile (Whichfile) Creatfile.Writeline (visitors) Creatfile.closset Fso = Nothing%>
Then, according to this, you can continue to extend the content: such as a number of records to display. Of course, the premise is that you need 0-9 10 notes pictures, and put this image in the IMG folder. Next to a enhanced txtcount.asp content code
<% Whichfile = server.mappath ( "counter.txt") set fso = createobject ( "Scripting.FileSystemObject") set openfile = fso.opentextfile (whichfile, 1) visitors = openfile.readlineopenfile.closeCountLen = len (visitors) response. WRITE "You are the" for i = 1 to 6-countlen "represents the maximum 99999 Response.write" img> "Next for i = 1 To Countlen Response. Write "
IMG>" NEXTRESPONSE.WRITE "bit" Visitors = Visitors 1Set CreatFile = fso.createtextfile (Whichfile) Creatfile. WriteLine (Visitors) Creatfile.closset FSO = Nothing%> The MID function is used in this program that the function is to return several characters starting from the first few characters in a string. The format is: MID (String, Start, Length)
CN_String = "CNBRUCE Love CNROSE"
CN_START = 9
CN_LENGTH = 4
Alert (MID (CN_String, CN_START, CN_LENGTH))
script>
[Ctrl a All Select Tips: You can modify some code first, then press the run] effect page See
http://www.cnbruce.com/code/txtcount.asp
Count text View:
http://www.cnbruce.com/code/counter.txt