通过数组给您的文件排序
admin
2023-07-30 20:17:02
0

当您使用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) & \”


相关内容

AdaptAsp.netJ...
将 Asp.net 中使用的 jsgantt-improved G...
2024-05-24 00:42:04
Adafruit 16x2...
要解决Adafruit 16x2显示屏带按键在Raspberry ...
2024-05-24 00:15:53
ActiveReports...
问题的根本在于HashLocationStrategy的路由模式不...
2024-05-22 01:00:16
AcquireTokenA...
在ASP.NET中,如果AcquireTokenAsync方法失败...
2024-05-19 01:13:52
ABPASP.NET:如何...
首先,需要在ABP应用程序的模块中配置JwtBearer身份验证。...
2024-05-15 01:21:45
ABP vs Asp.ne...
ABP (ASP.NET Boilerplate) 和 Asp.n...
2024-05-15 01:15:44

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
项目管理和工程管理的区别 项目管理 项目管理,顾名思义就是专注于开发和完成项目的管理,以实现目标并满足成功标准和项目要求。 工...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
Apache Doris 常见... 什么是 Apache Doris Apache Doris 是一款 MPP 架构的 OLAP 列式存...
Vmware简易安装ubunt... 大晚上的折腾死我了VMware安装ubuntu,用简易安装结果设置的用户名密码死活进不去再重装一次,...
‘WebDriver‘ obj... selenium库报错"‘WebDriver’ object has no attribute ‘f...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
WiFi中继器和WiFi扩展器... WiFi中继器以无线方式连接到 WiFi 网络并重新广播信号。它就像一个中继系统,连接到我们的 Wi...