通过数组给您的文件排序
程序员文章站
2022-06-05 12:27:46
当您使用filesystemobject对象获得某个目录下的文件列表的时候,你有没有发现无法控制它们的排序方式,比如按照名字排序,按照扩展名排序,按照文件大小排序等等,让我...
当您使用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>
如果您想通过名字排序,那将是非常简单的,但是假如你想通过文件大小或者文件创立时间等等来排序的时候,那么将有点麻烦。我们将通过二维数组做到这一点。
下面的代码演示了如何通过选择排序方式达到的我们目的,单击排序,点两次就反着排了。
<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>
上一篇: 小孩吃什么食物营养价值高,将来还聪明