FSO 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.
Set up the file, and then make an ASP page, of course, the best two files are in the same directory.
<%
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 an absolute physical path address. But the general situation is to upload to the space service provider, not very directly
Understand the location of your own files, so strongly recommend the use of the Server.MapPath method: Platform portability strong, applicable
Strong sex.
CreateObject ("Scripting.FileSystemObject") establishes a connection to the FSO component,
Fso. OpenTextFile (whichfile,1) opens the Info.txt file. Parameter "1" indicates
ForReading: Opens the file as read-only. Can't write this file. "And the other there are parameters
"2" means "ForWriting: Open File as write", parameter "8" means "ForAppending:"
Open the file and start writing from the end of the file.
Open the file, and then do you want to display the contents of the file? That's through TXT. ReadLine
method to read an entire line in the text and continue using TXT if you need 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 document that was created 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".
Then you can build a simple text. condylar gu Yunyang black raised sheath,
By application, session, Global.asa, 2, through the counter component of 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 all the memory emptied it:
Now you can use the text to record the data, even if the reboot, 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,
You will then write the 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.
The next is 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
A few characters to start the character. 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>
Learned the FSO to extract the file value, but also learned to input the information into the file, then the following application.Do not know you have no such habit: see a file, the unconscious right to choose to open Notepad.
Oh, almost no document is not possible. So now, you can default all files are text,
Only the suffix name is different; that is, you can now extract the content information for any file. OK, Just imagine:
1, extract the path of a file (using the file button to find positioning)
2, open the path file and read all rows
3, displaying the read information
First, viewcode.asp
<%
Function showcode (filename)
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
Set CNRS = FSO. OpenTextFile (filename, 1)
While not CNRS. AtEndOfStream
Rsline = CNRS. ReadLine
Rsline = Server. HTMLEncode (Rsline)
Response.Write (Rsline & "<br>")
Wend
End Function
%>
<form action= "viewcode.asp" method= "POST" >
Input filename <input type= "file" name= "filename" >
<input type= "Submit" value= "View source program" >
</form>
<%
File=request.form ("filename")
Response.Write (File & "source program as follows If Trim (file) <> "" Then
Call ShowCode (file)
End If
%>
The above program debugging, you can choose Html,asp page, you can open any application and so on.
The ShowCode function is defined to open, read, and display all the contents of the information in the file.
Note the server is added. HTMLEncode (Rsline) for files that contain standard HTML code.
Displaying all the rows in a file is displayed by looping through a conditional loop.
While not CNRS. AtEndOfStream
...
Wend
Next, the following example specifically covers the question of the open method, remember? The normal situation
The open file is using the FSO. OpenTextFile ("C:\testfile.txt", 1), the function of parameter 1 is:
Open the file in read-only mode. This file cannot be written to. If a file is already present,
What if you need to write an append? Simple, the parameter is 8.
What's the use? Oh, Amazon's network story solitaire is so: to be able to solitaire will need to first
To display the original story, then add your own story to write to the file. One of the most exquisite writing files here is
Append write. So the following can be achieved.
Second, story.asp
<%
If not request. Form ("nextline") = "" Then
Set fso=server.createobject ("Scripting.FileSystemObject")
Textfile1=server.mappath ("Story.txt")
Set CNRS=FSO. OpenTextFile (textfile1,8)
CNRs. WriteLine (Request.Form ("nextline"))
CNRs. Close
End If
%>
The story is as follows:
<%
Set fso=server.createobject ("Scripting.FileSystemObject")
Textfile1=server.mappath ("Story.txt")
Set CNRS=FSO. OpenTextFile (textfile1,1)
While not CNRS. AtEndOfStream
Response.Write "" & CNRS. ReadLine
Wend
Cnrs.close
%>
<form method= "POST" action= "story.asp" >
Please enter a new line for this story: <input name= "nextline" type= "text" size= ">"
<input type= "Submit" value= "submitted" >
</form>
The whole is a very simple to read information and add information to the mixed use, I believe there is the basis of the previous
Understanding should not be a problem. Of course, the lack of a story.txt file, inside the beginning of a good story can be.
Debugging
Next, go on, the main focus is to practice some of the use of functions of skills.
1,instr function: Returns the position in which a string appears for the first time in another string.
For example, now look for the first occurrence of the letter "A" in the string "a110b121c119d1861", you can
<script language=vbs>
my_string = "a110b121c119d1861"
A_num = InStr (my_string, "A")
Alert (A_num)
</script>
The position of the same letter "B" can be determined. Here we go. The key: Advance letters "A" and "B"
The median value "110". Remember the mid function? The mid function's main function is to return a specified number of characters from a string.
For example, the current "110" should be from the 2nd bit of the string to get 3 units of value.
<script language=vbs>
my_string = "a110b121c119d1861"
A_value = Mid (my_string,2,3)
Alert (A_value)
</script>
But imagine: if not "110", but "1100", that is not to extract 4 bits ... This will
Show that the procedure is not perfect.
So keep thinking: the extracted value is always after the letter "A", and the value is always in the letter
Between "A" and "B",
So as long as the position of "a" and "B" is extracted separately, the starting bit of the median value should be the letter "a" bit +1,
The length of the median value should be
Is the letter "B" bit-the letter "A" bit-1
So now you can make the program perfect:
<script language=vbs>
my_string = "a110b121c119d1861"
A_num = InStr (my_string, "A")
B_num = InStr (my_string, "B")
A_value = Mid (My_string,a_num+1,b_num-a_num-1)
Alert (A_value)
</script>
OK, so now you can completely extract the value one by one after the letter "B", "C", and "D".
Of course, we need to pay attention to the "D" after a few how to take it? Take the total length of the string-the letter D is
The number of positions is OK.
<script language=vbs>
my_string = "a110b121c119d1861"
A_num = InStr (my_string, "A")
B_num = InStr (my_string, "B")
C_num = InStr (my_string, "C")
D_num = InStr (my_string, "D")
Total_num = Len (my_string)
A_value = Mid (My_string,a_num+1,b_num-a_num-1)
B_value = Mid (My_string,b_num+1,c_num-b_num-1)
C_value = Mid (My_string,c_num+1,d_num-c_num-1)
D_value = Mid (My_string,d_num+1,total_num-d_num)
Alert (A_value)
Alert (B_value)
Alert (C_value)
Alert (D_value)
</script>
Now, you may have learned a lot, but you may ask: this is placed in the FSO file operation
What's the role in it?
The following is our topic: Use the FSO for simple text voting.
The main point of the voting page is to show the number of votes for each item and assign it to a variable. And then
To judge the election of this vote,
Add the voting value to 1, and then continue writing all values to the text.
1, an HTML form page website.html
To do voting click on the platform.
You may have learned a lot by now, but you may have some questions: this is in the FSO file operation
What's the effect?
The following is our topic: Use the FSO for simple text voting.
<form action= "result.asp" method= "POST" >
<input type= "Radio" name= "website" value= "A" checked> cnbruce.com<br>
<input type= "Radio" name= "website" value= "B" > Blueidea.com<br>
<input type= "Radio" name= "website" value= "C" > It365cn.com<br>
<input type= "Radio" name= "website" value= "D" > 5d.cn<br>
<input type= "Submit" >
<input type= "Reset" >
2, result.asp to accept the form page value
<%
Whichfile=server.mappath ("Site.txt")
Set Fso=createobject ("Scripting.FileSystemObject")
Set Thisfile=fso.opentextfile (Whichfile)
My_string=thisfile.readline
A_num = InStr (my_string, "A")
B_num = InStr (my_string, "B")
C_num = InStr (my_string, "C")
D_num = InStr (my_string, "D")
Total_num = Len (my_string)
A_value = Mid (My_string,a_num+1,b_num-a_num-1)
B_value = Mid (My_string,b_num+1,c_num-b_num-1)
C_value = Mid (My_string,c_num+1,d_num-c_num-1)
D_value = Mid (My_string,d_num+1,total_num-d_num)
Select Case Request.Form ("website")
Case "A": a_value=a_value+1
Case "B": b_value=b_value+1
Case "C": c_value=c_value+1
Case "D": d_value=d_value+1
End Select
Mynew_string= "A" & CStr (A_value) & "B" & CStr (B_value) & "C"
& CStr (C_value) & "D" & CStr (D_value)
Set Newfile=fso.createtextfile (Whichfile)
Newfile.writeline (mynew_string)
Newfile.close
Set fso=nothing
%>
Current voting:<br>
Cnbruce.com:<%=a_value%><br>
Blueidea.com:<%=b_value%><br>
It356cn.com:<%=c_value%><br>
5d.cn:<%=d_value%><br>
<a href= "website.html" > Back to continue </a>
With the basis of the above function, it should not be difficult to see this.
3, in the end don't forget the Count file Site.txt
Format: A1B1C1D1
Debugging
OK, three files can do a very simple voting system, if you want to strengthen,
Need to refine the combination of previous knowledge, such as a vote after a session or cookies,
When voting again to judge if the session or cookies exist is not allowed, that is, simple voting defense
False means ... Of course, more still want to go to the practice of their own.
One, FSO. GetFile
Extracts files corresponding to the file object
1,getfile.asp
<%
Whichfile=server.mappath ("Cnbruce.txt")
Set fso = CreateObject ("Scripting.FileSystemObject")
Set f1 = fso. CreateTextFile (Whichfile,true)
F1. Write ("This is a test". My Name is cnbruce. ")
F1. Close
Set F2 = fso. GetFile (Whichfile)
s = "file name:" & F2.name & "<br>"
s = S & "file short path name:" & F2.shortpath & "<br>"
s = S & "File Physical Address:" & F2. Path & "<br>"
s = S & "file attributes:" & F2. Attributes & "<br>"
s = S & "File size:" & f2.size & "<br>"
s = S & "file type:" & F2.type & "<br>"
s = S & "File creation time:" & F2. DateCreated & "<br>"
s = S & recent access time: & F2. Datelastaccessed & "<br>"
s = S & "Last modified:" & F2. DateLastModified
Response.Write (s)
%>
The effect is the right key to a file, see the specific property information.
Where the value "32" returned by attributes represents: (Archive) A file that has changed since the last backup. can read and write.
Other values are annexed below:
Normal 0 normal files. No properties are set.
ReadOnly 1 read-only files. can read and write.
Hidden 2 hidden files. can read and write.
System 4 Systems files. can read and write.
Directory 16 folders or directories. Read-only.
Archive 32 files that have changed since the last backup. can read and write.
Alias 1024 link or shortcut. Read-only.
Compressed 2048 compressed files. Read-only.
Second, File.move
Action moves the specified file or folder from one location to another. In fact, the method still belongs to
Fso. An application after the getfile.
2,movefile.asp
<%
Whichfile=server.mappath ("Cnbruce.txt")
Set fso = CreateObject ("Scripting.FileSystemObject")
Set f1 = fso. CreateTextFile (Whichfile,true)
F1. Write ("This is a test". My Name is cnbruce. ")
F1. Close
Set F2 = fso. GetFile (Whichfile)
F2. Move "C:\"
%>
<a href= "C:\" > see if there is </a>
Simple Cut-and-paste function is implemented.
Three, File.Copy
Also belong to FSO. An application after the getfile. Simply copy the file to a location.
3,copyfile.asp
<%
Whichfile=server.mappath ("Cnbruce.txt")
Set fso = CreateObject ("Scripting.FileSystemObject")
Set f1 = fso. CreateTextFile (Whichfile,true)
F1. Write ("This is a test". My Name is cnbruce. ")
F1. Close
Set F2 = fso. GetFile (Whichfile)
F2. Copy "D:\"
%>
<a href= "D:\" > see if there is </a>
The cnbruce.txt files that are in the same directory as this ASP page still exist.
Four, file. Delete
Obviously, the file is deleted directly.
4,delfile.asp
<%
Whichfile=server.mappath ("Cnbruce.txt")
Set fso = CreateObject ("Scripting.FileSystemObject")
Set f1 = fso. CreateTextFile (Whichfile,true)
F1. Write ("This is a test". My Name is cnbruce. ")
F1. Close
Set F2 = fso. GetFile (Whichfile)
F2.move "D:\"
Set F3 = fso. GetFile ("D:\cnbruce.txt")
F3.delete
%>
<a href= "D:\" > View is not the file of </a>
Of course, the FSO has not finished, such as uploading files, ASP to HTML, etc. need to use the FSO. The more wonderful is still in the back.