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

通过数组给您的文件排序

admin
2010年7月8日 1:5 本文热度 4928
[p]当您使用filesystemobject对象获得某个目录下的文件列表的时候,你有没有发现无法控制它们的排序方式,比如按照名字排序,按照扩展名排序,按照文件大小排序等等,让我们试着用数组给它们排排序儿。[br]如果您想通过名字排序,那将是非常简单的,但是假如你想通过文件大小或者文件创立时间等等来排序的时候,那么将有点麻烦。我们将通过二维数组做到这一点。[br]下面的代码演示了如何通过选择排序方式达到的我们目的,单击排序,点两次就反着排了。[/p] [p][br][br]文件排序演示[br][/p] [p][/p] [p]<%[br]' 设定一个演示目录,:)[/p] [p]const directory = "/" [/p] [p]' 用常数定义排序方式[br]const file_name = 0 '按照名字排序……依次类推[br]const file_ext = 1[br]const file_type = 2[br]const file_size = 3[br]const file_created = 4[br]const file_modified = 5[br]const file_accessed = 6[/p] [p]'获得 排序命令,默认为按照名字排序[/p] [p]req = request("sortby")[br]if len(req) < 1 then sortby = 0 else sortby = cint(req)[br]req = request("priorsort")[br]if len(req) < 1 then priorsort = -1 else priorsort = cint(req)[/p] [p]'设置倒序[br]if sortby = priorsort then[br]reverse = true[br]priorsort = -1[br]else[br]reverse = false[br]priorsort = sortby[br]end if[/p] [p]' 接下来开始我们真正的代码了。。。[/p] [p]path = server.mappath( directory )[/p] [p]set fso = createobject("scripting.filesystemobject")[br]set thecurrentfolder = fso.getfolder( path ) [br]set curfiles = thecurrentfolder.files [/p] [p]' 给这些文件做一个循环[/p] [p]dim thefiles( )[br]redim thefiles( 500 ) ' 我随便定的一个大小[br]currentslot = -1 ' start before first slot[/p] [p]' 我们将文件的所有相关信息放到数组里面[/p] [p]for each fileitem in curfiles[br]fname = fileitem.name[br]fext = instrrev( fname, "." )[br]if fext < 1 then fext = "" else fext = mid(fname,fext+1)[br]ftype = fileitem.type[br]fsize = fileitem.size[br]fcreate = fileitem.datecreated[br]fmod = fileitem.datelastmodified[br]faccess = fileitem.datelastaccessed[br]currentslot = currentslot + 1[br]if currentslot > ubound( thefiles ) then[br]redim preserve thefiles( currentslot + 99 )[br]end if[br]' 放到数组里[br]thefiles(currentslot) = array(fname,fext,ftype,fsize,fcreate,fmod,faccess)[br]next[/p] [p]' 现在都在数组里了,开始下一步[/p] [p][br]filecount = currentslot ' 文件数量[br]redim preserve thefiles( currentslot ) [/p] [p]' 排序[br]' (8 表示 string)[/p] [p]if vartype( thefiles( 0 )( sortby ) ) = 8 then [br]if reverse then kind = 1 else kind = 2 ' 给字符排序[br]else[br]if reverse then kind = 3 else kind = 4 '数字、时间。。。[br]end if[/p] [p]for i = filecount to 0 step -1[br]minmax = thefiles( 0 )( sortby )[br]minmaxslot = 0[br]for j = 1 to i[br]select case kind [br]case 1 [br]mark = (strcomp( thefiles(j)(sortby), minmax, vbtextcompare ) < 0)[br]case 2 [br]mark = (strcomp( thefiles(j)(sortby), minmax, vbtextcompare ) > 0)[br]case 3 [br]mark = (thefiles( j )( sortby ) < minmax)[br]case 4 [br]mark = (thefiles( j )( sortby ) > minmax)[br]end select[br]if mark then [br]minmax = thefiles( j )( sortby )[br]minmaxslot = j[br]end if[br]next[/p] [p]if minmaxslot <> i then [/p] [p]temp = thefiles( minmaxslot )[br]thefiles( minmaxslot ) = thefiles( i )[br]thefiles( i ) = temp[br]end if[br]next[br]' 结束[/p] [p]%>[br]
[br][br][br]
[/p] [p][/p] [p]
[br][br]显示<% = (filecount+1) %> 该目录下的文件<% = path %>[br][br]

[br]单击排序,再点一次反向排序[br]

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


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