FSO component operation

xiaoxiao2021-03-06  114

FSO, as UFO is very exciting, God, of course, is more happy to worry. Jun does not see a space service provider advertisement: 100MB space as long as 60RMB / year, support database, what is supported ... I don't support FSO, and immediately. What is FSO is what is, how is its power? What is the principle of its operation? This time I have a thorough understanding.

First, FSO is a referred to as FileSystemObject. Of course, our commonly known as FSO component, which can be used to process drives, folders, and files.

It can detect and display the information allocation of the system drive; also can be created, changed, moved, and delete folders, and can detect if some given folders exist, if there is, the information of the folder can be extracted. Such as the date, the date created or the last revision, and so on. FSO also makes it easy for the processing of files.

First, fso.getdrive

As in the establishment of other components, the reference to the FSO must also establish a connection.

SET FSO = Server.createObject ("scripting.filesystemObject") Note The inside of CreateObject is not MSWC, but scripting. The driver can be processed by FSO below. For example, fso.getdriveName extract the drive name, fso.getdrive also extracts the standard drive name. such as:

1, fso.asp

<% Set fso = server.createObject ("scripting.filesystemObject")%> <% = fso.getdrivename ("D:")%>
<% = fso.getdrive ("D:")%> You will Discover GetDriveName ("D:") is "D:", and GetDrive ("D:") is a standard "D:", so we generally write fso.getdrive (fso.getdrivename (drvpath) to extract some A specific driver disk.

Second, Drv.getinfo has been extracted with a particular drive, and then the specific information of the drive disk is not extracted.

2, drv.asp

<% Set fso = server.createObject ("scripting.filesystemObject") set drv = fso.getdrive (fso.getdrivename ("D:"))%> The space size of the disk: <% = drv.totalsize%>
The remaining space size of the disc: <% = drv.freespace%> The above is just the information extracted D-disk drive, to a universal function, continue to test your own driver.

3, Drvinfo.asp

<% Function ShowdriveInfo (DRVPATH) DIM FSO, DRV, S set fso = createObject ("scripting.filesystemObject") SET DRV = fso.getdrive (fso.getdrivename (drvpath)) s = "Volume" & DRV & " The standard is: "s = s & drv.voluMename &"
"S = S &" total space: "& drv.totalsize &"
"S = S &" Remaining Space: "& Drv.Freespace &
"S = S &" file type: "& drv.drivetype"
"S = S &" file system: "& driLeSystem response.write send function%> <% on Error ResMe Nextwhatpath = Request.form ("Path") if whatpath <> "" "theshowdriveInfo (whatPath) end if%>

of DRV. TOTALSIZE and DRV.FREESPACE returns the number of bytes, we can use the formatNumber () function to process. For example, FormatNumber (Drive.Totalsize / 1024, 0) gets a value of how much g of the disk is known. There is also a file type: DRV.DRIVETYPE is the maximum of the display value "2", in fact, "2", "hard drive", "1" means "floppy drive", "4" means "CD" ...

Below with a program traversed information to display all drives on your machine.

4, Showall.asp

<% Function Tran (Driver) SELECT CASE DRIVERCASE 0: TRAN = "Device Unrecognizable" Case 1: TRAN = "Floppy Drive" Case 2: TRAN = "Hard Drive" Case 3: TRAN = "Network Hard Drive" CASE 4: TRAN = "CD Drive" Case 5: TRAN = "RAM Virtual Disk" End SelectEnd Function So = Server.createObject ("Scripting.FileSystemObject")%>

"Response.write"
"& drv.driveletter &" "response.write" "& TRAN (DRV.DRIVETYPE) &" "response.write" " & drv.volumename & "" Response.write "" & FormatNumber (DRV.TOTALSIZE / 1024, 0) & "" Response.write "" & FormatNumber (DRV. AvailableSpace / 1024, 0) & "" Response.write "" & DRV.FileSystem & "" Response.write "" & Drv.SerialNumber & " "Response.write" "& drv.isready &" "Response.write" "& drv.path &" "response.write" "NextSet FS = Nothing%> Demo

Well, is it very god? Then you can debug your own machine, then upload to your own space to debug, you will find that the service chamber will make some settings :) Of course, the god is still behind, such as the folder, the operation of the file (including Add, modify, and delete). PS: You can't easily add delete for your drive :)

Operate the driver, then it is to operate the folder. These include: extract folder information, create folders, delete folders, copy folders, mobile folders, etc. The following is specifically.

First, Fso.Getfolder understands that it is the extraction folder. Is that specific to extract which folder? Behind it is definitely the path to a folder. Apply the folder related information again? Is there a specific extraction. So, look at the program: 1, getfldr.asp

<% Set fso = createObject ("scripting.filesystemObject") set fldr = fso.getFolder ("C: / Program Files") response.write "parent folder name is:" & FLDR & "
" if Fldr. IsRootFolder = true thrite.write "This folder" & "
" Else Response.write "This folder is not root folder" & "
" End ifresponse.write "drive name is : "& Fldr.drive &"
"%> The connection to establish an FSO component is essential, then set fldr = fso.getFolder (" c: / program files ") Set the FLDR object assignment, To make the reference to the following procedures.

FLDR.ISRootfolder is to determine if the folder is a folder, the value is a true or false; FLDR.DRIVE displays the drive letter in the folder.

Second, fso.createfolder is more exciting to establish a folder through the ASP, you can establish any folder anywhere within your power. 2, Creatfldr.asp

<% Set fso = createObject ("scripting.filesystemObject") fso.createfolder ("c: / cnbruce") response.write "folder name" & fso.getBaseName ("c: / cnbruce")%> executor, It should be found that the C-disk has a multi-CNBRUCE folder, and fso.getBaseName is the extraction folder name.

Third, Fso.Deletefolder can establish a folder through the ASP, or the folder can also be deleted.

3, Delfldr.asp

<% Set fso = createObject ("scripting.filesystemObject") fso.deletefolder ("c: / cnbruce") response.write folder Delete "%> Discover the just created CNBRUCE folder is indeed deleted. Then use a general program to flexibly strain.

4, mainflr.asp <% Sub CreateAFolder (file) Dim fso Set fso = CreateObject ( "Scripting.FileSystemObject") fso.CreateFolder (file) response.write "has established" & 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 <> "" "" "& CREATE &" "" "& Create &" "" "& del") end if End IF%>


need to pay attention What is deleted does not prompt "Confirm to put into the recycle bin". This requires careful handling, especially for your system folder.

Fourth, the main role of fso.movefolder is the movement of the folder, which is equivalent to cutting and paste actions.

5, Movefldr.asp

<% Set fso = createObject ("scripting.filesystemObject") fso.createfolder ("c: / cnbruce") fso.movefolder "c: / cnbruce", "c: / program files /"%> Viewing the CNBRUCE folder Is there any moving format: fso.movefolder" Moved folder "," Moving to folder "

This program first creates a CNBRUCE folder under the C disc and then moves it below the C: / Program Files / folder.

However, it is also necessary to pay attention to your system folder can't move.

5. The main role of fso.copyfolder: copy the folder from a certain location to another. 6, copyfldr.asp <% set fso = createObject ("scripting.filesystemObject") fso.copyfolder "c: / program files / cnbruce", "C: /"%> View CNBRUCE The folder has no copy. This program is based on the execution result of the last Movefldr.asp to complete the copy to the root directory of the C drive. (The words are relatively long -_-!)

Of course, its copy is also included in the folder so that the subfolders and files are all copied.

Let's finally delete the C: / Program Files / CNBruce and C: / CNBruce two folders.

However, continue to remind you: Don't write wrong, such as writing into C: / Program Files, then you are miserable: This is called life, learn ASP to play.

It's almost the same as the operation of the folder, is it a lot to use? Some of the jokes: Good things are the double-edged sword. Justice and evil are only one line, pay attention to the regular reasonable use of the component. But there is still your rest assured that the website space service provider has already killed this power, and you will not bubbling anything else.

Then then is more subtle: FSO's operation of files.

In addition to the operation of the drive, folder, FSO is the most powerful function of the file. It can be used to count, content management, search can also generate dynamic HTML pages, and more.

First, fso.opentextfile does not need to say, fso.opentextfile is open a file, generally the open TXT text file. So first we build a TXT file first, then read the contents through FSO.

1, Info.txt

Name: CNBRUCESEX: MALE established the file, then do the ASP page below, of course, 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 & "
" & TXT.READLINERESPONSE.WRITE RLINERESPONSE.WRITE RLINETXT.CLOSE%> Need to note: Whether you open the drive, open the folder, open file, and open the database, you can only open an absolute physical path address. However, the general situation is to upload the space service provider, and it is impossible to learn from the location of its own file, so it is highly recommended to use the server.mappath method: the platform is strong, and the applicability is strong.

CreateObject ("scripting.filesystemObject") establishes the connection of the FSO component, FSO.OpenTextFile (WhichFile, 1) Opens info.txt. Parameter "1" means "Forreading: Open file in read-only mode. You can't write this file." And start from the end of the file. " Open the file, isn't it to display the content in the file? Then read the entire line in the text through the txt.readline method. If you need to continue to read the next line, continue using the TXT.Readline method. Of course, there are other reading methods, such as TXT.READ (7) read the specified number of characters, TXT.Readall returns all the contents in the text.

Second, fso.createtextfile is like FSO.CREATEFOLDER to create a folder, fso.createtextFile is a built 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%> View Content This time the file is the last info.txt file, fso.createtextfile (Whichfile, True) The parameter true indicates that the existing file can be overwritten. "MyFile.WriteLine" is used after the addition of data needs to be added.

That can now create a simple text record, remember the previous count? : 1, by Application, Session, Global.asa; 2, the count of the Counter component. But both have a disease, just can't save, if the server is restarted, all the notes are all empty :) Now you can use the text to record data, even if you restart, the next extraction is also the file. .

Test: Text Counter

First create a text file counter.txt, set the initial value of "1"

4, counter.txt

1 Next is the ASP file of the count, the function is to display the number of texts, this makes the number of points, and then write the new count to the text file.

5, TXTCOUNT.ASP

<% Whichfile = server.mappath ("counter.txt") 'Opens the file and reads its value, and finally closes the connection release resource set fso = creteObject ("scripting.filesystemObject") set openfile = fso.opentextfile (Whichfile, 1 Visitors = OpenFile.readLineOpenFile.close 'page Display count content and add 1 arithmetic response.write "You are the" & Visitors & "Visitor" Visitors = VITORS 1' to add new value to text Finally, close all connection release resources set createfile = fso.createtextFile (whichfile) Creatfile.writeline (Visitors) Creatfile.closset fso = NOTHING%> The content can continue to extend according to this: such as letting a count image display. Of course, the premise is that you need 0-9 10 notes pictures, and put this image in the IMG folder. The next is a 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 "represents the maximum 99999 Response.write" "Next for i = 1 To Countlen Response. Write " " NEXTRESPONSE.WRITE "bit" Visitors = Visitors 1Set CreatFile = fso.createtextfile (Whichfile) Creatfile. WriteLine (Visitors) Creatfile.closset FSO = Nothing%> The MID function is used in this program that the function is to return several characters starting from the first few characters in a string. The format is: MID (String, Start, Length)