我在旧的IIS机器上使用以下代码为我为Android和ios设备构建的移动应用程序生成XML ...它可以工作,但我现在想要弄清楚我将如何按日期排序SORTING最后一次修改,所以列表顶部有最新的文件...我的问题是,根据我在下面编写代码的方式,
这是否可以使用我现有的代码(以某种方式排序'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>
答案 0 :(得分:5)
您可以使用称为Recordset Sorting的旧时间技巧轻松地对VBScript中的任何内容进行排序。下面的代码是完全有效的,并且是从好的旧asp101站点,R.I.P(archive link)
中获取的。<%@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排序效率非常高,当我多年前做过自定义基准测试时,即使有数千个项目也可以快速运行。
答案 1 :(得分:1)
并非没有为排序引入替代技术 - FileSystemObject.Files属性不支持任何排序语义。
JScript解决方案似乎可以毫不费力地进行插入排序。