ASP Programming Entry Advanced (18): FSO Component file operation (middle)

Source: Internet
Author: User
Tags modify readline string
Fso| programming |fso learned the FSO to extract file values, but also learned to input 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


<%
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? 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.

PS: There is also a way of reading.



<%
Whichfile=server.mappath ("Test.txt")
Set fso = CreateObject ("Scripting.FileSystemObject")
Set txt = fso. OpenTextFile (whichfile,1)
Rline = txt. ReadAll
Rline=replace (Rline), Chr (Server.HTMLEncode), "<br>"
Response.Write Rline
Txt. Close
%>




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.

Debug Address:
Http://www.cnbruce.com/code/story.asp

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>
[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]

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>
[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]

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>
[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]

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&quo



Related Article

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.