如何使用 ASP 列出目录中最新的 10 个文件

发布于 2024-12-08 14:11:07 字数 1967 浏览 0 评论 0原文

我有一些 asp 代码来列出文件夹的内容(仅限 txt 文件)。我想知道是否有办法只列出最后创建的 10 个文件。

我的应用程序每天将创建一个文件,使用 AAMMDD.txt 作为名称。

我希望能够仅列出最后 10 个文件。

这里有人有一些可以分享的例子吗???

先感谢您。

这是我发现的列出所有内容的代码(我已经对脚本进行了一些更改):

<%
Const ImageFilePath = "logs"

Dim objFSO
Dim objFolder
Dim objFile

Dim strFileName
Dim strFileExtension

Dim blnShowFiles

If Request.QueryString("ShowFiles") = "" Then
    blnShowFiles = True
Else
    blnShowFiles = CBool(Request.QueryString("ShowFiles"))
End If


    Set objFSO = Nothing
%>

<style>

ul.dropdownPC, ul.dropdownPC li, ul.dropdownPC ul 
            {
    list-style: none;
    margin: 8px;
    float: left;
    vertical-align: middle;
    padding:0px;
    border:solid;
    border-width:0px;
    border-color:#ccc;
    background-color: #fff;
}
ul.dropdownPC li 
            {
    padding:5px;
    border-width:0px;
}
</style>

<ul class="dropdownPC dropdown-horizontal">
<%
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath(ImageFilePath))

For Each objFile In objFolder.Files
    strFileExtension = LCase(Mid(objFile.Name, InStrRev(objFile.Name, ".", -1, 1) + 1))

    If strFileExtension = "txt" Then
        %>
        <li><a href="<%= ImageFilePath & "/" & objFile.Name %>" target=_blank><img width="80" height="80" border="0" src="images/texticon01.png"><br><center><%= objFile.Name %></center></a>
        <%
        If blnShowFiles Then
            %>
            <!-- <%= objFile.Name %> --></li>
            <%
        Else
            %>
            <!-- <a href="<%=ImageFilePath & "/" & objFile.Name %>">View `Logs</a> --></li>`
            <%
        End If
        %>

        <%
    End If
Next ' objFile

Set objFolder = Nothing
Set objFSO = Nothing
%>

I got some asp code to list the content of a folder (txt files only). I would like to know if there would be a way to list only the last 10 files created.

My application will create one file a day, using AAMMDD.txt as name.

I would like to be able to list only the 10 last files.

Does anyone here have some example that could share???

thank you in advance.

here is the code I found that list everything (I already made some changes on the script):

<%
Const ImageFilePath = "logs"

Dim objFSO
Dim objFolder
Dim objFile

Dim strFileName
Dim strFileExtension

Dim blnShowFiles

If Request.QueryString("ShowFiles") = "" Then
    blnShowFiles = True
Else
    blnShowFiles = CBool(Request.QueryString("ShowFiles"))
End If


    Set objFSO = Nothing
%>

<style>

ul.dropdownPC, ul.dropdownPC li, ul.dropdownPC ul 
            {
    list-style: none;
    margin: 8px;
    float: left;
    vertical-align: middle;
    padding:0px;
    border:solid;
    border-width:0px;
    border-color:#ccc;
    background-color: #fff;
}
ul.dropdownPC li 
            {
    padding:5px;
    border-width:0px;
}
</style>

<ul class="dropdownPC dropdown-horizontal">
<%
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath(ImageFilePath))

For Each objFile In objFolder.Files
    strFileExtension = LCase(Mid(objFile.Name, InStrRev(objFile.Name, ".", -1, 1) + 1))

    If strFileExtension = "txt" Then
        %>
        <li><a href="<%= ImageFilePath & "/" & objFile.Name %>" target=_blank><img width="80" height="80" border="0" src="images/texticon01.png"><br><center><%= objFile.Name %></center></a>
        <%
        If blnShowFiles Then
            %>
            <!-- <%= objFile.Name %> --></li>
            <%
        Else
            %>
            <!-- <a href="<%=ImageFilePath & "/" & objFile.Name %>">View `Logs</a> --></li>`
            <%
        End If
        %>

        <%
    End If
Next ' objFile

Set objFolder = Nothing
Set objFSO = Nothing
%>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人接听 2024-12-15 14:11:07

将文件名和创建日期放入记录集中,对其进行排序并获取前十条记录。

<%
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath(ImageFilePath))

'Required ADO Constants
Const adVarChar = 200
Const adDate = 7
Dim objRs
Set objRS = Server.CreateObject("Adodb.Recordset") 'Recordset for the sort
objRs.Fields.Append "FileName", adVarChar, 255
objRs.Fields.Append "CreateDate", adDate
objRs.Open

For Each objFile In objFolder.Files
    strFileExtension = LCase(Mid(objFile.Name, InStrRev(objFile.Name, ".", -1, 1) + 1))
    If strFileExtension = "txt" Then 'All Text Files Into Recordset
        objRS.AddNew Array("FileName", "CreateDate"), Array(objFile.Name, objFile.DateCreated)
        objRs.Update
    End If
Next ' objFile

objRs.Sort = "CreateDate Desc"
For i = 1 To 10
If objRS.Eof Then Exit For 
%>
        <li><a href="<%= ImageFilePath & "/" & objRS("FileName") %>" target=_blank><img width="80" height="80" border="0" src="images/texticon01.png"><br><center><%= objRS("FileName") %></center></a>
        <% If blnShowFiles Then %>
        <!-- <%= objRS("FileName") %> --></li>
        <% Else %>
        <!-- <a href="<%=ImageFilePath & "/" & objRS("FileName") %>">View `Logs</a> --></li>`
        <% End If %>
<%
objRS.MoveNext
Next
Set objFolder = Nothing
Set objFSO = Nothing
objRS.Close
Set objRS = Nothing
%>

Put the file names and creation dates into a recordset, sort it and get top ten records.

<%
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath(ImageFilePath))

'Required ADO Constants
Const adVarChar = 200
Const adDate = 7
Dim objRs
Set objRS = Server.CreateObject("Adodb.Recordset") 'Recordset for the sort
objRs.Fields.Append "FileName", adVarChar, 255
objRs.Fields.Append "CreateDate", adDate
objRs.Open

For Each objFile In objFolder.Files
    strFileExtension = LCase(Mid(objFile.Name, InStrRev(objFile.Name, ".", -1, 1) + 1))
    If strFileExtension = "txt" Then 'All Text Files Into Recordset
        objRS.AddNew Array("FileName", "CreateDate"), Array(objFile.Name, objFile.DateCreated)
        objRs.Update
    End If
Next ' objFile

objRs.Sort = "CreateDate Desc"
For i = 1 To 10
If objRS.Eof Then Exit For 
%>
        <li><a href="<%= ImageFilePath & "/" & objRS("FileName") %>" target=_blank><img width="80" height="80" border="0" src="images/texticon01.png"><br><center><%= objRS("FileName") %></center></a>
        <% If blnShowFiles Then %>
        <!-- <%= objRS("FileName") %> --></li>
        <% Else %>
        <!-- <a href="<%=ImageFilePath & "/" & objRS("FileName") %>">View `Logs</a> --></li>`
        <% End If %>
<%
objRS.MoveNext
Next
Set objFolder = Nothing
Set objFSO = Nothing
objRS.Close
Set objRS = Nothing
%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文