FSO Component file operation (medium)

Source: Internet
Author: User
Tags html form readline
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, but the suffix name is different, so that is, you can now extract the contents of any file information. 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


<%

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? Under normal circumstances, open the file is to use the FSO. OpenTextFile ("C:\testfile.txt", 1), and the function of parameter 1 is to open the file in read-only mode. This file cannot be written to. What if a file already exists and an append write is required? Simple, the parameter is 8.

What's the use? Oh, Amazon's network story solitaire is so: can Solitaire need to first show the original story, and then add their own stories to write files. One of the most exquisite writing files is the 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 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 the letter "A" and "B" in the middle of the 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 shows that the procedure is not perfect.
So keep thinking: the extracted value is always after the letter "a", and the value is always between the letter "a" and "B", so as long as the "a", "B" position, then the starting bit of the median value should be the letter "a" bit +1, the middle value should be the length of 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 number of places where the letter D is.


<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: what is the role of this in the FSO file operation?
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. The voting is then judged by adding 1 to the voting value and then continuing to write all values to the text.

1, an HTML form page website.html
To do voting click on the platform.

Now, you may have learned a lot, but you may ask: what is the role of this in the FSO file operation?
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_va Lue
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 be qualified for a very simple voting system, if you want to strengthen, the need to refine the combination of previous knowledge, such as a vote after a session or cookies, when the vote again to judge if the session or cookies exist is not allowed, It's a simple way to vote and cheat. Of course, more still want to go to the practice of their own.




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.