In addition to operating drives and folders, the most powerful function in FSO is operating files. It can be used for counting, content management, searching, and generate dynamic HTML pages, etc.
1. fso.OpenTextFile
Needless to say, fso.OpenTextFile is to open a file, which is usually an open txt text file. So first we create a txt file and then read the contents through FSO.
1, info.txt
| name:cnbruce sex:male |
After creating this file, make an ASP page below. Of course, it is best for the two files to be 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 %> |
It should be noted that whether you open the drive, folder, file through FSO, or open the database you want to contact in the future, you can only open the absolute physical path address. But generally speaking, uploading it to the space service provider will not directly understand the location of your file, so it is highly recommended to use the server.mappath method: the platform is highly portable and has strong applicability.
CreateObject("Scripting.FileSystemObject") establishes the connection to the FSO component, and fso.OpenTextFile(whichfile,1) opens the info.txt file. Parameter "1" means "ForReading: Open the file in read-only mode. You cannot write this file." There are also parameters "2" means "ForWriting: Open the file in write mode", and parameter "8" means "ForAppending: Open the file and start writing from the end of the file".
After opening the file, should you display the contents in the file? Then read a whole line in the text through the txt.ReadLine method. If you need to continue reading the next line, continue to use the txt.ReadLine method. Of course, there are other reading methods at first, such as txt.Read(7) reading a specified number of characters, and txt.ReadAll returns all the contents in the text.
2. fso.CreateTextFile
Just like 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 file created this time is the previous info.txt file. The parameter true in fso.CreateTextFile(whichfile,True) means that the existing file can be overwritten. After creation, you need to add data to it and use "MyFile.WriteLine".
Then you can now create a simple text counter. Remember the previous counting? : 1. Counting through application, session, global.asa; 2. Counting through Counter component. But both have a common problem, that is, they cannot be saved. If the server restarts, will all the records be cleared?) Then you can use text to record the data now. Even if you restart, the file will still be extracted next time.