FSO,正如UFO般令人激動、令人神往,當然更多的亦是讓人歡喜讓人憂。君不見某空間服務商廣告:100MB空間只要60RMB/年,支持數據庫,支持什么什么……一問不支持FSO,立馬泄氣。那FSO究竟是什么東西,它的力量又是如何巨大,其操作的原理又是怎的怎的呢?這次來個徹底的理解。
首先,FSO是FileSystemObject的簡稱。當然也就是我們的俗稱FSO組件了,該組件可以用來處理驅動器、文件夾以及文件。
它可以檢測并顯示出系統驅動器的信息分配情況;還能夠創建、改變、移動和刪除文件夾,并能探測一些給定的文件夾是否存在,若存在,還能提取出該文件夾的信息,如名稱、被創建或最后一次修改的日期,等等。FSO還使得對文件的處理變得很容易。
一、fso.GetDrive
正如其它組件的建立一樣,FSO的引用也必須建立連接。
Set fso=Server.CreateObject("Scripting.FileSystemObject")
注意CreateObject的內部再也不是MSWC了,而是Scripting。 那下面就可以通過fso來處理驅動器了。比如fso.GetDriveName提取驅動器名,fso.GetDrive同樣提取標準驅動器名。比如:
1,fso.asp
<%Set fso=Server.CreateObject("Scripting.FileSystemObject")%> <%=fso.GetDriveName("d:")%><br> <%=fso.GetDrive("d:")%>
你會發現GetDriveName("d:")就是“d:”,而GetDrive("d:")則為標準的“D:”,所以我們一般這樣寫fso.GetDrive(fso.GetDriveName(drvPath))來提取某個具體的驅動盤。
二、drv.GetInfo 上面已經把某個特定的驅動器提取了,那接著是不是提取該驅動盤的具體信息。
2,drv.asp
<% Set fso=Server.CreateObject("Scripting.FileSystemObject") Set drv=fso.GetDrive(fso.GetDriveName("d:")) %> 該盤的空間大小:<%=drv.TotalSize%><br> 該盤的剩余空間大小:<%=drv.FreeSpace%>
以上只是提取的D盤驅動器的信息,來個通用的函數,繼續分別測試自己的驅動吧。
3,drvinfo.asp
<% Function ShowDriveInfo(drvPath) Dim fso, drv, s Set fso = CreateObject("Scripting.FileSystemObject") Set drv = fso.GetDrive(fso.GetDriveName(drvPath)) s = "驅動盤" & drv & "的卷標是:" s = s & drv.VolumeName & "<br>" s = s & "總計空間:" & drv.TotalSize & "<br>" s = s & "剩余空間:" & drv.FreeSpace & "<br>" s = s & "文件類型:" & drv.DriveType & "<br>" s = s & "文件系統:" & drv.FileSystem Response.Write s End Function %> <% on error resume next whatpath=request.form("path") if whatpath<>"" then ShowDriveInfo(whatpath) end if%> <form action="drvinfo.asp" method="post"> <input name="path"> <input type="submit"> </form>
其中的drv.TotalSize和drv.FreeSpace返回的是字節數,我們可以用FormatNumber()函數處理下。比如FormatNumber(Drive.TotalSize/1024,0)得到一眼就知磁盤多少G的值。
還有一個文件類型:drv.DriveType最多的時候是顯示數值“2”,其實“2”就表示的“硬盤驅動器”,“1”表示“軟盤驅動器”,“4”表示“光盤驅動器”……
下面就用一個程序遍歷顯示自己機器上所有驅動器的信息
4,showall.asp
<% Function tran(Driver) Select Case Driver Case 0: tran="設備無法識別" Case 1: tran="軟盤驅動器" Case 2: tran="硬盤驅動器" Case 3: tran="網絡硬盤驅動器" Case 4: tran="光盤驅動器" Case 5: tran="RAM虛擬磁盤" End Select End Function set fso=Server.CreateObject("Scripting.FileSystemObject") %>
<table border=1 width="100%"> <tr> <td>盤符</td> <td>類型</td> <td>卷標</td> <td>總計大小</td> <td>可用空間</td> <td>文件系統</td> <td>序列號</td> <td>是否可用</td> <td>路徑</td> </tr> <% on error resume next For 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>" Next set fs=nothing %>
http://www.cnbruce.com/code/showall.asp
恩,是不是很神氣?那你可以對自己的機器調試,然后上傳到自己的空間去調試,你會發現服務商會你進行了些設置:) 當然更神的還在后面面,比如對文件夾,對文件的操作(包括添加、修改和刪除)。ps:你對驅動器是不能輕易添加刪除的:)
|