Matt
Matt

Reputation: 503

List most recent files in ASP Classic

I am redesigning our department website and our IT department does not support the intranet development. The server runs ASP Classic and is able to run VB scripts and Javascript to an extent (somethings work others don't).

So here is my problem:

I modified a simple code that I got from http://www.brainjar.com/asp/dirlist/ to list all the PDF Files in a directory including sub directories but I am not sure how to sort it.

As of now it sorts it in alphabetical order per each folder it reads. I would like it to sort every file of every sub directory by the item.DateLastModified property I do not know if this possible.

I am thinking I would need to store the items in an array and then sort the array and print the data but I have no idea how to do that it has been 10 years since I took a programming course.

Any help would be greatly appreciated!

Current code I am using :

====>

<% sub ListFolderContents(path)
 dim fs, folder, file, item, url

 set fs = CreateObject("Scripting.FileSystemObject")
 set folder = fs.GetFolder(path)

 for each item in folder.SubFolders
   ListFolderContents(item.Path)
 next

 'Display a list of files
 for each item in folder.Files
   url = MapURL(item.path)
   if item.type = "PDF File" then
   Response.Write("<dt><a href=""" & url & """>" & item.Name & "</a>" _
 & vbCrLf)
   end if
 next
 Response.Write("</dt>" & vbCrLf)
 end sub


 function MapURL(path)
 dim rootPath, url

 'Convert a physical file path to a URL for hypertext links.
 rootPath = Server.MapPath("/")
 url = Right(path, Len(path) - Len(rootPath))
 MapURL = Replace(url, "\", "/")

 end function %>

The original is at http://www.brainjar.com/asp/dirlist/

Upvotes: 2

Views: 1614

Answers (1)

Shadow Wizard
Shadow Wizard

Reputation: 66388

Well, it's your lucky day! I happen to have old code that I wrote for personal use ~10 years ago, so with little tweaking it can fit your case almost perfectly. The key is using a disconnected recordset to hold all the data, then sort by date last modified. The crawling itself is similar to what you already have, by recursion. Note that no need to create new folder instance in each iteration - it's waste of resources, since you already have the folder object in the loop.

Anyway, here it is:

Const adVarChar = 200
Const adInteger = 3
Const adDate = 7
Dim objFSO, oFolder, objRS
Sub ExtractAllFiles(oFolder)
    Dim oSubFolder, oFile
    'recurse all sub folders
    For Each oSubFolder In oFolder.SubFolders
        Call ExtractAllFiles(oSubFolder)
    Next

    '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("Url").Value = MapURL(oFile.Path)
        objRS.Fields("Type") = oFile.Type
        objRS.Fields("DateLastModified").Value = oFile.DateLastModified
    Next
End Sub

Sub ListFolderContents(sPath, sTypeToShow)
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    Set oFolder = objFSO.GetFolder(sPath)

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

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

    'extract all files in given path:
    Call ExtractAllFiles(oFolder)    

    'sort and apply:
    If Not(objRS.EOF) Then 
        objRS.Sort = "DateLastModified DESC"
        objRS.MoveFirst
    End If

    'loop through all the records:
    Do Until objRS.EOF
        If (Len(sTypeToShow)=0) Or (LCase(sTypeToShow)=LCase(objRS("Type"))) Then
            Response.Write("<dt><a href=""" & objRS("Url") & """>" & objRS("Name") & "</a> (Type: " & objRS("Type") & ", Last modified: " & objRS("DateLastModified") & ")</dt>" _
                 & vbCrLf)
        End If
        objRS.MoveNext()
    Loop

    'clean up resources
    Set oFolder = Nothing
    Set objFSO = Nothing
    objRS.Close
    Set objRS = Nothing
End Sub

To use it in your code, have such line in the HTML body:

<% Call ListFolderContents(Server.MapPath("."), "PDF File") %>

You can of course use different path and change the display to show only what you need.

Upvotes: 2

Related Questions