1

我在旧的 IIS 机器上使用以下代码为我为 android 和 ios 设备构建的移动应用程序生成 XML ......它有效,但我现在想弄清楚我将如何按上次修改日期排序所以该列表顶部有最新的文件......我的问题是,根据我在下面的代码结构,

这可能与我现有的代码(以某种方式排序'x'吗?)?

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%Response.ContentType = "text/xml"%>
<%Response.AddHeader "Content-Type","text/xml"%>

<songlist>
<%
dim fs,fo,x
dim i
set fs=Server.CreateObject("Scripting.FileSystemObject")

'point to a specific folder on the server to get files listing from...
set fo=fs.GetFolder(Server.MapPath("./songs"))
i = -1


for each x in fo.files
'loop through all the files found, use var 'i' as a counter for each...
i = i + 1
'only get files where the extension is 'mp3' -- we only want the mp3 files to show in list...
if right(x,3) = "mp3" then   
%>

<song>
<songid><%=i%></songid>
<name><%= replace(replace(x.Name, "-", " "), ".mp3", "")%></name>
<filename><%=x.Name%></filename>
<datemodified><%=x.DateLastModified%></datemodified>
</song>


<% 
end if 
next

set fo=nothing
set fs=nothing
%>
</songlist>
4

2 回答 2

6

您可以使用称为记录集排序的旧技巧轻松地对 VBScript 中的任何内容进行排序。下面的代码完全正常工作,想法取自古老的 asp101 站点,RIP(存档链接

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<% Option Explicit %>
<%Response.ContentType = "text/xml"%>
<%Response.AddHeader "Content-Type","text/xml"%>
<songlist>
<%
Const adVarChar = 200
Const adInteger = 3
Const adDate = 7

Dim objFSO, oFolder, oFile
Dim fileCounter, objRS
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

'point to a specific folder on the server to get files listing from...
Set oFolder = objFSO.GetFolder(Server.MapPath("."))
Set objFSO = Nothing

'create a disconnected recordset
Set objRS = Server.CreateObject("ADODB.Recordset")

'append proper fields
objRS.Fields.Append "Name", adVarChar, 255
objRS.Fields.Append "DateLastModified", adDate
objRS.Open

'loop through all the files found, add to the recordset
For Each oFile in oFolder.Files
    objRS.AddNew
    objRS.Fields("Name").Value = oFile.Name
    objRS.Fields("DateLastModified").Value = oFile.DateLastModified
Next
Set oFolder=nothing

'sort and apply:
objRS.Sort = "DateLastModified DESC"
objRS.MoveFirst

fileCounter = 0
'loop through all the records:
Do Until objRS.EOF %>
<song>
<songid><%=fileCounter%></songid>
<filename><%=objRS("Name")%></filename>
<datemodified><%=objRS("DateLastModified")%></datemodified>
</song><% 
    fileCounter = fileCounter + 1
    objRS.MoveNext()
Loop
objRS.Close
Set objRS = Nothing
%>
</songlist>

(我在本地测试时删除了一些位,您当然可以将它们添加回来)

值得一提的是,Recordset 排序非常有效,当我几年前进行自定义基准测试时,即使处理数千个项目,它也能快速运行。

于 2013-09-03T08:32:31.357 回答
1

并非没有引入用于排序的替代技术 - FileSystemObject.Files 属性不支持任何排序语义。

不过,JScript 解决方案似乎可以相当轻松地进行插入排序。

于 2013-09-02T02:52:53.787 回答