[ASP技巧] 巧给FSO文件夹列表内容排序
自己写排序算法十分繁琐,而且不够灵活,百度搜索“ASP 文件夹排序”搜到的结果可以100%视为垃圾。本文推荐一种最小代价的做法。
<%
[vb]
'定义获取排序文件列表的函数
Function getSortedFiles(folderPath)
Dim rs, fso, folder, File
Const adInteger = 3
Const adDate = 7
Const adVarChar = 200
Set rs = Server.CreateObject("ADODB.Recordset")
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderPath)
Set fso = Nothing
With rs.Fields
.Append "Name", adVarChar, 200
.Append "Type", adVarChar, 200
.Append "DateCreated", adDate
.Append "DateLastAccessed", adDate
.Append "DateLastModified", adDate
.Append "Size", adInteger
.Append "TotalFileCount", adInteger
End With
rs.Open
For Each File In folder.Files
rs.AddNew
rs("Name") = File.Name
rs("Type") = File.Type
rs("DateCreated") = File.DateCreated
rs("DateLastAccessed") = File.DateLastAccessed
rs("DateLastModified") = File.DateLastModified
rs("Size") = File.Size
rs.Update
Next
'设置排序规则:按名称排序
rs.Sort = "Name ASC"
''设置排序规则:依次按文件大小倒序,按修改日期倒序
'rs.Sort = "Size DESC, DateLastModified DESC"
rs.MoveFirst
Set folder = Nothing
Set getSortedFiles = rs
End Function
'调用函数并输出结果集
Dim rs
Set rs = getSortedFiles( Server.MapPath("/") )
While Not rs.EOF
Response.Write "<p>" & rs("Name") & " | " & rs("Type") & "</p>"
rs.MoveNext
Wend
rs.Close
Set rs = Nothing
'定义获取排序文件列表的函数
Function getSortedFiles(folderPath)
Dim rs, fso, folder, File
Const adInteger = 3
Const adDate = 7
Const adVarChar = 200
Set rs = Server.CreateObject("ADODB.Recordset")
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderPath)
Set fso = Nothing
With rs.Fields
.Append "Name", adVarChar, 200
.Append "Type", adVarChar, 200
.Append "DateCreated", adDate
.Append "DateLastAccessed", adDate
.Append "DateLastModified", adDate
.Append "Size", adInteger
.Append "TotalFileCount", adInteger
End With
rs.Open
For Each File In folder.Files
rs.AddNew
rs("Name") = File.Name
rs("Type") = File.Type
rs("DateCreated") = File.DateCreated
rs("DateLastAccessed") = File.DateLastAccessed
rs("DateLastModified") = File.DateLastModified
rs("Size") = File.Size
rs.Update
Next
'设置排序规则:按名称排序
rs.Sort = "Name ASC"
''设置排序规则:依次按文件大小倒序,按修改日期倒序
'rs.Sort = "Size DESC, DateLastModified DESC"
rs.MoveFirst
Set folder = Nothing
Set getSortedFiles = rs
End Function
'调用函数并输出结果集
Dim rs
Set rs = getSortedFiles( Server.MapPath("/") )
While Not rs.EOF
Response.Write "<p>" & rs("Name") & " | " & rs("Type") & "</p>"
rs.MoveNext
Wend
rs.Close
Set rs = Nothing
%>