I learned how to extract the file value from FSO and input the information into the file. Then I will apply it again.
I don't know if you have the habit of seeing a file, right-click it unconsciously and choose to open it with notepad. No file is acceptable. So now, by default, all files are text, but their suffix names are different. That is to say, the content of any file can be extracted now. OK. Just imagine:
1. Extract the path of a file (use the file button for searching and locating)
2. Open the path file and read all rows.
3. Display read information
I. 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 file name <input type = "file" name = "filename">
<Input type = "submit" value = "View Source program">
</Form>
<%
File = request. form ("filename ")
Response. write (file & "source program If trim (file) <> "then
Call ShowCode (file)
End If
%> |
When debugging the above program, you can select html, asp page, or open any application.
The ShowCode function is used to open, read, and display all information in the file. Note that server. HTMLEncode (rsline) is added for files containing standard HTML code.
All rows in the display file are displayed in a conditional loop.
While Not cnrs. AtEndOfStream
...
Wend
Next, the following example involves the open method. Remember? Normally, fso. OpenTextFile ("c: \ testfile.txt", 1) is used to open a file. Parameter 1 is used to open the file in read-only mode. You cannot write this file. What if an object already exists and needs to be appended to it? Simple. The parameter is 8.
PS: there is also a read method.
<%
Whichfile = server. mappath ("test.txt ")
Set fso = CreateObject ("Scripting. FileSystemObject ")
Set txt = fso. OpenTextFile (whichfile, 1)
Rline = txt. ReadAll
Rline = replace (Server. HtmlEncode (rline), Chr (13), "<br> ")
Response. Write rline
Txt. Close
%>
|
How can this be used? Haha, This Is What Amazon's network story is like: to be able to connect to a dragon, you need to first display the original story, and then add the story to the file. The most important thing to write data to this file is append data. So we can implement it below.
Ii. 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
%>
<Hr>
<Form method = "post" action = "story. asp">
Enter a new line for this story: <input name = "NextLine" type = "text" size = "70">
<Input type = "submit" value = "submit">
</Form>
|
The whole process is a simple mix of reading information and adding information. I believe that with the previous basic understanding, it should not be a problem. Of course there are still few story.txt files, which can be written at the beginning of the story.
Debugging address:
Http://www.cnbruce.com/code/story.asp
Next, let's continue. The focus is to practice some function usage skills.
1. instr function: returns the position where a string first appears in another string.
For example, if you want to locate the first occurrence of the letter "A" in the string "A110B121C119D1861", you can
<Script language = vbs> <br/> my_string = "A110B121C119D1861" <br/> a_num = instr (my_string, "A") <br/> alert (a_num) <br/> </script> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]
The location of the letter "B" is also determined. Now, the most important thing is that the values between the letters "A" and "B" are "110" in advance ".
Do you still remember the mid function? The mid function is mainly used to return a specified number of characters from a string.
For example, the current "110" value should be 3 units from the string's 2nd bits.
<Script language = vbs> <br/> my_string = "A110B121C119D1861" <br/> a_value = mid (my_string, 2, 3) <br/> alert (a_value) <br/> </script> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]
But imagine: if it is not "110", but "1100", is it necessary to extract four digits ...... This shows that the program 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, then, as long as the positions of "A" and "B" are extracted separately, the starting position of the intermediate value should beLetter "A" + 1The length of the intermediate value must beLetter "B"-letter "A"-1
Now we can make the program perfect:
<Script language = vbs> <br/> my_string = "A110B121C119D1861" <br/> a_num = instr (my_string, "A") <br/> B _num = instr (my_string, "B") <br/> a_value = mid (my_string, a_num + 1, B _num-a_num-1) <br/> alert (a_value) <br/> </script> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]
OK. Now you can extract the values following the letters "B", "C", and "D" one by one.
Of course, it should be noted that how many digits are next to "D? UseTotal String Length-number of locations where the letter D is locatedYou can.
<Script language = vbs> <br/> my_string = "A110B121C119D1861" <br/> a_num = instr (my_string, "A") <br/> B _num = instr (my_string, "B") <br/> c_num = instr (my_string, "C") <br/> d_num = instr (my_string, "D ") <br/> total_num = len (my_string) <br/> a_value = mid (my_string, a_num + 1, B _num-a_num-1) <br/> B _value = mid (my_string, B _num + 1, c_num-b_num-1) <br/> c_value = mid (my_string, c_num + 1, d_num-c_num-1) <br/> d_value = mid (my_string, d_num + 1, total_num-d_num) <br/> alert (a_value) <br/> alert (B _value) <br/> alert (c_value) <br/> alert (d_value) <br/> </script> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]
Now, you may have learned a lot, but you may ask: what is the role of this operation in the FSO file?
Next is our question: Use FSO for simple text voting.
The voting page displays the number of votes of various projects and assigns them to a variable. Then, judge the voting phase, add the voting value to 1, and then write all the values into the text.
1. website.html
Platforms used for voting and clicking.
<Form action = "result. asp "method =" post "> <br/> <input type =" radio "name =" website "value =" A "checked> cnbruce.com <br> <br/> <input type = "radio" name = "website" value = "B"> blueidea.com <br> <br/> <input type = "radio" name = "website" value =" C "> it365cn.com <br> <br/> <input type =" radio "name =" website "value =" D "> 5d.cn <br> <br/> <input type = "submit"> <br/> <input type = "reset"> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]
2. Accept the result. asp of the single page value of the table.
<%
Whichfile = server. mappath ("site.txt ")
Set fso = createobject ("Scripting. FileSystemObject ")
Set thisfile = fso. opentextfile (whichfile)
My_string = thisfile. readline
A_num = instr (my_string, "")
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 vote: <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"> return to continue </a> |
With the foundation of the above functions, it should not be difficult to see this.
3. Do not forget the last note file site.txt
Format:A1B1C1D1
Debugging address:
Http://www.cnbruce.com/code/website.html
OK, the three files will be competent for a very simple voting system. If you want to strengthen the system, you should combine the previous knowledge, such as setting session or cookies after one vote, when the second round of voting, it is not allowed to judge if the session or cookies exist, that is, a simple method of voting to prevent fraud ...... Of course, more things need to be done by yourself.