LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

通过数组给您的文件排序

admin
2010年7月8日 1:5 本文热度 5343

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



文件排序演示

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

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
' 结束

%>






显示<% = (filecount+1) %> 该目录下的文件<% = path %>

单击排序,再点一次反向排序











<%

for i = 0 to filecount
response.write "

" & vbnewline
for j = 0 to ubound( thefiles(i) )
response.write " " & vbnewline
next
response.write "" & vbnewline
next
%>
文件名扩展名类型大小建立时间上次修改时间上次存取时间
" & thefiles(i)(j) & "



该文章在 2010/7/8 1:05:55 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved