通过数组给您的文件排序
|
admin
2010年7月8日 1:5
本文热度 5248
|
[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]
[/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 " " & thefiles(i)(j) & " | " & vbnewline[br]next[br]response.write "
" & vbnewline[br]next[br]%>[br]
[/p]
[p][br] [/p]
该文章在 2010/7/8 1:05:55 编辑过