當(dāng)您使用FILESYSTEMOBJECT對(duì)象獲得某個(gè)目錄下的文件列表的時(shí)候,你有沒有發(fā)現(xiàn)無法控制它們的排序方式,比如按照名字排序,按照擴(kuò)展名排序,按照文件大小排序等等,讓我們?cè)囍脭?shù)組給它們排排序兒。 如果您想通過名字排序,那將是非常簡單的,但是假如你想通過文件大小或者文件創(chuàng)立時(shí)間等等來排序的時(shí)候,那么將有點(diǎn)麻煩。我們將通過二維數(shù)組做到這一點(diǎn)。 下面的代碼演示了如何通過選擇排序方式達(dá)到的我們目的,單擊排序,點(diǎn)兩次就反著排了。
<HTML> <HEAD> <TITLE>文件排序演示</TITLE> </HEAD>
<BODY>
<% ' 設(shè)定一個(gè)演示目錄,:)
CONST DIRECTORY = "/"
' 用常數(shù)定義排序方式 CONST FILE_NAME = 0 '按照名字排序……依次類推 CONST FILE_EXT = 1 CONST FILE_TYPE = 2 CONST FILE_SIZE = 3 CONST FILE_CREATED = 4 CONST FILE_MODIFIED = 5 CONST FILE_ACCESSED = 6
'獲得 排序命令,默認(rèn)為按照名字排序
req = Request("sortBy") If Len(req) < 1 Then sortBy = 0 Else sortBy = CInt(req) req = Request("priorSort") If Len(req) < 1 Then priorSort = -1 Else priorSort = CInt(req)
'設(shè)置倒序 If sortBy = priorSort Then reverse = true priorSort = -1 Else reverse = false priorSort = sortBy End If
' 接下來開始我們真正的代碼了。。。
path = Server.MapPath( DIRECTORY )
Set fso = CreateObject("Scripting.FileSystemObject") Set theCurrentFolder = fso.GetFolder( path ) Set curFiles = theCurrentFolder.Files
' 給這些文件做一個(gè)循環(huán)
Dim theFiles( ) ReDim theFiles( 500 ) ' 我隨便定的一個(gè)大小 currentSlot = -1 ' start before first slot
' 我們將文件的所有相關(guān)信息放到數(shù)組里面
For Each fileItem in curFiles fname = fileItem.Name fext = InStrRev( fname, "." ) If fext < 1 Then fext = "" Else fext = Mid(fname,fext+1) ftype = fileItem.Type fsize = fileItem.Size fcreate = fileItem.DateCreated fmod = fileItem.DateLastModified faccess = fileItem.DateLastAccessed currentSlot = currentSlot + 1 If currentSlot > UBound( theFiles ) Then ReDim Preserve theFiles( currentSlot + 99 ) End If ' 放到數(shù)組里 theFiles(currentSlot) = Array(fname,fext,ftype,fsize,fcreate,fmod,faccess) Next
' 現(xiàn)在都在數(shù)組里了,開始下一步
fileCount = currentSlot ' 文件數(shù)量 ReDim Preserve theFiles( currentSlot )
' 排序 ' (8 表示 string)
If VarType( theFiles( 0 )( sortBy ) ) = 8 Then If reverse Then kind = 1 Else kind = 2 ' 給字符排序 Else If reverse Then kind = 3 Else kind = 4 '數(shù)字、時(shí)間。。。 End If
For i = fileCount TO 0 Step -1 minmax = theFiles( 0 )( sortBy ) minmaxSlot = 0 For j = 1 To i Select Case kind Case 1 mark = (strComp( theFiles(j)(sortBy), minmax, vbTextCompare ) < 0) Case 2 mark = (strComp( theFiles(j)(sortBy), minmax, vbTextCompare ) > 0) Case 3 mark = (theFiles( j )( sortBy ) < minmax) Case 4 mark = (theFiles( j )( sortBy ) > minmax) End Select If mark Then minmax = theFiles( j )( sortBy ) minmaxSlot = j End If Next If minmaxSlot <> i Then temp = theFiles( minmaxSlot ) theFiles( minmaxSlot ) = theFiles( i ) theFiles( i ) = temp End If Next ' 結(jié)束
%> <FORM Name="doSort" Method="Get"> <INPUT Type=Hidden Name=priorSort Value="<% = priorSort %>"> <INPUT Type=Hidden Name=sortBy Value="-1"> </FORM>
<SCRIPT Language="JavaScript"> function reSort( which ) { document.doSort.sortBy.value = which; document.doSort.submit( ); } </SCRIPT>
<CENTER> <FONT Size="+2"> 顯示<% = (fileCount+1) %> 該目錄下的文件<% = path %> </FONT> <P> 單擊排序,再點(diǎn)一次反向排序 <P> <TABLE Border=1 CellPadding=3> <TR> <TH><A HREF="javascript:reSort(0);">文件名</A></TH> <TH><A HREF="javascript:reSort(1);">擴(kuò)展名</A></TH> <TH><A HREF="javascript:reSort(2);">類型</A></TH> <TH><A HREF="javascript:reSort(3);">大小</A></TH> <TH><A HREF="javascript:reSort(4);">建立時(shí)間</A></TH> <TH><A HREF="javascript:reSort(5);">上次修改時(shí)間</A></TH> <TH><A HREF="javascript:reSort(6);">上次存取時(shí)間</A></TH> </TR> <%
For i = 0 To fileCount Response.Write "<TR>" & vbNewLine For j = 0 To UBound( theFiles(i) ) Response.Write " <TD>" & theFiles(i)(j) & "</TD>" & vbNewLine Next Response.Write "</TR>" & vbNewLine Next %> </TABLE>
</BODY> </HTML>
|
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!