FSO is as exciting and fascinating as UFOs. Of course, more is also a matter of joy and sorrow. June does not see a space service provider's advertisement: MB space is only 60 RMB/year, supports database, what is supported ...... FSO is not supported. So what exactly is FSO, how powerful is it, and how it operates? This is a thorough understanding.
FSO is short for FileSystemObject. The FSO component can be used to process drives, folders, and files.
It can detect and display information distribution of the system drive, create, change, move, and delete folders, and detect the existence of some specified folders, the folder information can also be extracted, such as the name, the date on which the folder was created or last modified, and so on. FSO also makes File Processing easy.
I. FSO. getdrive
Just like other components, FSO references must also be connected.
Set FSO = server. Createobject ("scripting. FileSystemObject ")
Note that the inside of Createobject is no longer mswc, but scripting.
Then you can use FSO to process the drive. For example, FSO. getdrivename extracts the drive name, and FSO. getdrive extracts the standard drive name. For example:
1, FSO. asp
<% Set FSO = server. createobject ("scripting. fileSystemObject ") %> <% = FSO. getdrivename ("D:") %> <br> <% = FSO. getdrive ("D:") %>
You will find that getdrivename ("D:") is "D:", while getdrive ("D:") is the standard "D:", so we generally write FSO like this. getdrive (FSO. getdrivename (drvpath) is used to extract a specific drive disk.
Ii. DRV. getinfo
The above has extracted a specific drive, then whether to extract the specific information of the drive disk.
2. DRV. asp
<% Set FSO = server. createobject ("scripting. fileSystemObject ") set DRV = FSO. getdrive (FSO. getdrivename ("D:") %> disk space: <% = DRV. totalsize %> <br> remaining disk space: <% = DRV. freespace %>
The above is just the information of the D Drive. Use a common function to test your own driver separately.
3. drvinfo. asp
<% Function showdriveinfo (drvpath) dim FSO, DRV, s set FSO = Createobject ("scripting. fileSystemObject ") set DRV = FSO. getdrive (FSO. getdrivename (drvpath) S = "drive disk" & DRV & "is marked as" s = S & DRV. volumename & "<br>" s = S & "total space:" & DRV. totalsize & "<br>" s = S & "available space:" & DRV. freespace & "<br>" s = S & "file type:" & DRV. drivetype & "<br>" s = S & "File System:" & DRV. filesystem response. write send function %> <% on error resume nextwhatpath = request. form ("path") if whatpath <> "" thenshowdriveinfo (whatpath) end if %> <form action = "drvinfo. ASP "method =" Post "> <input name =" path "> <input type =" Submit "> </form>
The DRV. totalsize and DRV. freespace return the number of bytes, which can be processed using the formatnumber () function. For example, formatnumber (drive. totalsize/, 0) can be used to obtain the disk GB at a glance.
Another file type is DRV. when drivetype is the most, the value "2" is displayed. In fact, "2" indicates the "Hard Drive", and "1" indicates the "floppy disk drive ", "4" indicates "Disc Drive "......
Next we will use a program to traverse and display the information of all the drives on our machine.
4. showall. asp
<% Function Tran (driver) Select case drivercase 0: TRAN = "unable to recognize device" Case 1: TRAN = "floppy disk drive" Case 2: TRAN = "Hard Drive" Case 3: TRAN = "Network hard drive" case 4: TRAN = "Disc Drive" case 5: TRAN = "Ram Virtual Disk" End selectend functionset FSO = server. createobject ("scripting. fileSystemObject ") %> <Table border = 1 width = "100%"> <tr> <TD> drive letter </TD> <TD> type </TD> <TD> volume mark </TD> <TD> total size </TD> <TD> available space </TD> <TD> File System </TD> <TD> serial number </TD> <TD> available </TD> <TD> path </TD> </tr> <% on error resume nextfor each DRV in Fso. drives response. write "<tr>" response. write "<TD>" & DRV. driveletter & "</TD>" response. write "<TD>" & Tran (DRV. drivetype) & "</TD>" response. write "<TD>" & DRV. volumename & "</TD>" response. write "<TD>" & formatnumber (DRV. totalsize/1024, 0) & "</TD>" response. write "<TD>" & formatnumber (DRV. availablespace/1024, 0) & "</TD>" response. write "<TD>" & DRV. filesystem & "</TD>" response. write "<TD>" & DRV. serialnumber & "</TD>" response. write "<TD>" & DRV. isready & "</TD>" response. write "<TD>" & DRV. path & "</TD>" response. write "</tr>" nextset FS = nothing %> </table>
Demo
Well, isn't it awesome? Then you can debug your machine and upload it to your own space for debugging. You will find that you have made some settings in the service Chamber :)
Of course, even more powerful, such as folder and file operations (including adding, modifying, and deleting ).
PS: You cannot easily add or delete the drive :)
After the drive is operated, the folder is operated. Including extracting Folder Information, creating folders, deleting folders, copying folders, and moving folders. Next, let's take a look.
I. FSO. getfolder
At first glance, we can see that the folder is extracted. Which folder is extracted? Follow the path of a folder. After the information is extracted, the folder information is displayed? Is there any need to extract it. So, check the program:
1. getfldr. asp
<% Set FSO = Createobject ("scripting. fileSystemObject ") set FLDR = FSO. getfolder ("C:/Program Files") response. write "parent folder name:" & FLDR & "<br>" If FLDR. isrootfolder = true then response. write "this folder is a folder" & "<br>" else response. write "this folder is not the root folder" & "<br>" End ifresponse. write "drive name:" & FLDR. drive & "<br>" %>
It is essential to establish a connection to the FSO component first, and then set FLDR = FSO. getfolder ("C:/Program Files") sets the FLDR object to be assigned a value for reference in the following program.
FLDR. isrootfolder determines whether the folder is a root folder with a Boolean value (true or false). FLDR. Drive displays the drive letter of the folder.
Ii. FSO. createfolder
The following is an exciting example of creating folders through ASP. You can create any folder anywhere in your power.
2. creatfldr. asp
<% Set FSO = Createobject ("scripting. fileSystemObject ") FSO. createfolder ("C:/cnbruce") response. write "folder name" & FSO. getbasename ("C:/cnbruce") %>
Execute the program and you should find that the C drive has an additional cnbruce folder, and FSO. getbasename is the extraction folder name.
Iii. FSO. deletefolder
You can use ASP to create a folder and delete the folder.
3. delfldr. asp
<% Set FSO = Createobject ("scripting. FileSystemObject") FSO. deletefolder ("C:/cnbruce") response. Write "folder deleted" %>
The created cnbruce folder is indeed deleted.
Next we will adopt a general program to flexibly adapt to the situation.
4, mainflr. asp
<% Sub createafolder (File) dim FSO set FSO = Createobject ("scripting. fileSystemObject ") FSO. createfolder (File) response. write "& fileend subsub deleteafolder (File) dim FSO set FSO = Createobject (" scripting. fileSystemObject ") FSO. deletefolder (File) response. write "deleted" & fileend sub %> <% subname = request. form ("Submit") Create = request. form ("CREATE") del = request. form ("Del") if subname <> "" Then if create <> "" Then call createafolder ("" & create &"") end if del <> "" Then call deleteafolder ("" & del & "") end if %> <form action = "mainflr. ASP "method =" Post "> <input name =" CREATE "> <input type =" Submit "value =" CREATE "name =" Submit "> </form> <HR> <form action = "mainflr. ASP "method =" Post "> <input name =" Del "> <input type =" Submit "value =" delete "name =" Submit "> </form>
Note that the deletion does not prompt "confirm to be placed in the recycle bin. This requires careful processing, especially for your system folders.
Iv. FSO. movefolder
The main function is to move folders, which is equivalent to cutting and pasting.
5. movefldr. asp
<% Set FSO = Createobject ("scripting. fileSystemObject ") FSO. createfolder ("C:/cnbruce") FSO. movefolder "C:/cnbruce", "C:/program files/" %> <a href = "C: /program files/"> check whether the cnbruce folder has been moved </a>
Format: FSO. movefolder "Moved folder", "Moved folder"
In this program, the cnbruce folder is created under drive C, and then moved to the C:/program files/folder.
However, you must also note that your system folders cannot be moved randomly.
V. FSO. copyfolder
Main function: copy a folder from one location to another.
6. copyfldr. asp
<% Set FSO = Createobject ("scripting. fileSystemObject ") FSO. copyfolder "C:/program files/cnbruce", "C:/" %> <a href = "C:/"> check whether the cnbruce folder has been copied. </a>
The program is copied to the C root directory based on the execution result of movefldr. asp. (Long -_-!)
Of course, it also copies all subfolders and files in the folder.
Finally, try to delete the C:/program files/cnbruce and C:/cnbruce folders.
However, keep reminding you: do not make mistakes, such as writing C:/program files, so you are miserable: This is called a joke, learning ASP to play out a heartbeat.
The operations on folders are almost the same. Is it very useful? Good things are a double-edged sword. Justice and evil are only the first line. Pay attention to the regular and rational use of this component. However, you can rest assured that the website space service provider has been bound to this power, and you will not bubble up any more reasons :)
The next step is more subtle: FSO file operations.
In FSO, apart from the operations on the drive and folder, the most powerful function is the operations on the file. It can be used to count, manage content, search, and generate dynamic HTML pages.
I. FSO. opentextfile
Needless to say, FSO. opentextfile is used to open a file, which is generally a TXT text file. Therefore, first create a TXT file and then read the content through FSO.
1. info.txt
Name: cnbrucesex: Male
After this file is created, create an ASP page. Of course, it is best that the two files are 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. readlinerline = rline & "<br>" & TXT. readlineresponse. write rlinetxt. close %>
Note: Whether you use FSO to open a drive, open a folder, open a file, or open a database to be accessed later, you can only open an absolute physical path address. However, it is generally uploaded to the space service provider, so you cannot directly learn the location of your file. Therefore, we strongly recommend that you use the server. mappath method, which is highly portable and applicable.
Createobject ("scripting. FileSystemObject" creates a connection to the fsogroup. fso.opentextfile(whichfile,1)open info.txt. Parameter "1" indicates "forreading: open the file in read-only mode. This file cannot be written .", The other parameter "2" indicates "forwriting: open the file in write mode", and parameter "8" indicates "forappending: open the file and start writing from the end of the file ".
After opening the file, do you want to display the content in the file? Use the TXT. Readline method to read the entire line of text. If you want to continue reading the next line, continue to use the TXT. Readline method. Of course, there are other reading methods at the beginning. For example, TXT. Read (7) reads a specified number of characters and TXT. readall returns all the content in the text.
Ii. FSO. createtextfile
For example, if 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 current file is the info.txt file. The parameter "true" in Fso. createtextfile (whichfile, true) indicates that the existing file can be overwritten. After creation, you need to add data to it and then use "myfile. writeline.
Now we can create a simple text calculator. Remember the previous one? : 1. Count by application, session, and global. Asa; 2. Count by counter component. But there is a common problem between the two, that is, they cannot be saved. If all the records are cleared after the server is restarted, you can use text to record the data now, even if you restart, the file is extracted next time.
Test: Text counter
First, create a counter file counter.txt and set the initial value to "1"
4,counter.txt
1
The following is the count ASP file. The function is to display the count of the text. In this example, add 1 to the count, and then write the new count into the text file.
5, txtcount. asp
<% Whichfile = server. mappath ("counter.txt") 'open the file and read its value, and close the connection to release the resource set FSO = Createobject ("scripting. fileSystemObject ") set openfile = FSO. opentextfile (whichfile, 1) visitors = openfile. readlineopenfile. close 'page shows the Count content and Add 1 operation response. write "you are the" & Visitors & "visitor" visitors = visitors + 1' on this page to add new values to the text, finally, close all connections to release the resource set creatfile = FSO. createtextfile (whichfile) creatfile. writeline (visitors) creatfile. closeset FSO = nothing %>
Based on this, you can continue to expand the content, for example, to display the records in digital images. Of course, the premise is that you need 10 records of 0-9 images and place the images in the IMG folder.
Next is the enhanced txtcount. ASP content code.
<% Whichfile = server. mappath ("counter.txt") set FSO = Createobject ("scripting. fileSystemObject ") set openfile = FSO. opentextfile (whichfile, 1) visitors = openfile. readlineopenfile. closecountlen = Len (visitors) response. write "you are the" for I = 1 to 6-countlen "on this page. The maximum value is 999999 response. write " </img>" Next for I = 1 to countlen response. write " </img> "nextresponse. write "guest" visitors = visitors + 1 Set creatfile = FSO. createtextfile (whichfile) creatfile. writeline (visitors) creatfile. closeset FSO = nothing %>
In this program, the mid function is used to return the characters starting from the nth character in a string. The format is mid (string, start, length)
<Script language = vbs> cn_string = "cnbruce love cnrose" cn_start = 9cn_length = 4 alert (mid (cn_string, cn_start, cn_length) </SCRIPT>
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>") wendend 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 <HR>") 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.
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. closeend 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. readlinewendcnrs. 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
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> my_string = "a110b121c119d1861" a_num = instr (my_string, "A") Alert (a_num) </SCRIPT>
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> my_string = "a110b121c119d1861" a_value = mid (my_string, 2, 3) Alert (a_value) </SCRIPT>
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 be the letter "A" + 1, the length of the intermediate value must be the letter "B"-the letter "A"-1
Now we 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. 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? Use the total length of the string-the number of locations where the letter D is located.
<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 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.
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.
<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. 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. readlinea_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 + 1end selectmynew_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. closeset 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 is not very difficult to see this.
3. Do not forget the last note file site.txt
Format: a1b1c1d1
Debugging
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.
1. FSO. GetFile
Extract file objects
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.closeset F2 = FSO. getFile (whichfile) S = "file name:" & f2.name & "<br>" s = S & "Short file path name: "& f2.20.path &" <br> "s = S &" physical file 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 &" last access time: "& f2.datelastaccessed &" <br> "s = S &" Last modified: "& f2.datelastmodifiedresponse. write (s) %>
The result is as follows: Right-click a file to view the specific property information.
The value "32" returned by Attributes indicates: (archive) the file that has been changed since the last backup. Read/write.
The appendix of other values is as follows:
Normal 0 common file. No attribute is set. Readonly 1 read-only file. Read/write. Hidden 2 hides the file. Read/write. System 4 system file. Read/write. Directory 16 folder or directory. Read-only. Archive 32 files changed after the last backup. Read/write. Alias 1024 links or shortcuts. Read-only. Compressed 2048. Read-only.
Ii. file. Move
To move a specified file or folder from one location to another. In fact, this method still belongs to an application after FSO. 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.closeset F2 = FSO. getFile (whichfile) f2.move "C:/" %> <a href = "C:/"> check whether </a>
Simple cut and paste functions.
Iii. file. Copy
It also belongs to an application after FSO. 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.closeset F2 = FSO. getFile (whichfile) f2.copy "D:/" %> <a href = "D:/"> check whether </a>
The cnbruce.txt file in the same directory as the ASP page still exists.
4. file. Delete
Obviously, the object 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.closeset F2 = FSO. getFile (whichfile) f2.move "D:/" set F3 = FSO. getFile ("D:/cnbruce.txt") f3.delete %> <a href = "D:/"> check if the file is not found. </a>
Of course, FSO is not over yet. For example, FSO is required for uploading files and converting ASP to HTML.