动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   Asp → 阅读文章

 通过数组给您的文件排序

作者来源: 
阅读 1080 人次 , 2006-4-3 0:04:00 


当您使用filesystemobject对象获得某个目录下的文件列表的时候,你有没有发现无法控制它们的排序方式,比如按照名字排序,按照扩展名排序,按照文件大小排序等等,让我们试着用数组给它们排排序儿。
如果您想通过名字排序,那将是非常简单的,但是假如你想通过文件大小或者文件创立时间等等来排序的时候,那么将有点麻烦。我们将通过二维数组做到这一点。
下面的代码演示了如何通过选择排序方式达到的我们目的,单击排序,点两次就反着排了。

<html>
<head>
<title>文件排序演示</title>
</head>

<body>

<%
' 设定一个演示目录,:)

const directory = "/"

' 用常数定义排序方式
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

'获得 排序命令,默认为按照名字排序

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)

'设置倒序
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

' 给这些文件做一个循环

dim thefiles( )
redim thefiles( 500 ) ' 我随便定的一个大小
currentslot = -1 ' start before first slot

' 我们将文件的所有相关信息放到数组里面

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
' 放到数组里
thefiles(currentslot) = array(fname,fext,ftype,fsize,fcreate,fmod,faccess)
next

' 现在都在数组里了,开始下一步

filecount = currentslot ' 文件数量
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 '数字、时间。。。
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
' 结束

%>
<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>
单击排序,再点一次反向排序
<p>
<table border=1 cellpadding=3>
<tr>
<th><a href="javascript:resort(0);">文件名</a></th>
<th><a href="javascript:resort(1);">扩展名</a></th>
<th><a href="javascript:resort(2);">类型</a></th>
<th><a href="javascript:resort(3);">大小</a></th>
<th><a href="javascript:resort(4);">建立时间</a></th>
<th><a href="javascript:resort(5);">上次修改时间</a></th>
<th><a href="javascript:resort(6);">上次存取时间</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>

 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:处理驱动器和文件夹
· 下一篇:用ASP做一个TOP COOL的站内搜索
· 正则表达式中的特殊字符一览
· Asp Object 之:BinaryWrite
· 验证码的程序及原理
· 跟我学做最强功能的网站统计,一个ASP统计制作实例
· 在ADO使用SELECT语法六


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077