2011-10-08 109 views
1

我有一些asp代碼來列出文件夾(僅限txt文件)的內容。我想知道是否有辦法只列出最後創建的10個文件。如何使用ASP列出目錄的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 
%> 

回答

3

嗨。將文件名和創建日期放入記錄集中,對其進行排序並獲得前十條記錄。

<% 
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 
%> 
相關問題