1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development ASP - Link only newest file in a folder

Discussion in 'Software' started by Silver51, 28 Nov 2010.

  1. Silver51

    Silver51 I cast flare!

    Joined:
    24 Jul 2006
    Posts:
    2,962
    Likes Received:
    287
    Okay, I'm looking to tidy up and generally overhaul the back end of a website for one of our local primary schools. Unfortunately, it only uses old school ASP. No PHP, no ASP.net, just ASP. And, I guess, javascript.

    They upload their newsletters to a folder which is then presented on an ASP page which lists the directory contents and inverts the list (with newest first,) here.

    It's great as an archive, but a little untidy if people are coming here looking for only the latest newsletter. Is there any way of using ASP (or javascript) to only link the most recent newsletter and not the whole directory?

    They currently use this:

    Code:
    <%
    Dim strPath 'Path of directory to show
    Dim objFSO 'FileSystemObject variable
    Dim objFolder 'Folder variable
    Dim objItem 'Variable used to loop through the contents of the folder
    Dim rstFiles
    Const adVarChar = 200
    Const adInteger = 3
    Const adDate = 7
    strPath = "./newsletters/"
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(Server.MapPath(strPath))
    %>
    <font face="Arial">Content of <strong><%= strPath %></strong></font><br />
    <br />
    <%
    For Each objItem In objFolder.SubFolders
    If InStr(1, objItem, "_vti", 1) = 0 Then
    %>
    <a href="<%= strPath & objItem.Name %>"><%= objItem.Name %></a>
    <%= objItem.Size %>
    <%= objItem.DateCreated %>
    <%= objItem.Type %>
    <%
    End If
    Next 'objItem
    Set rstFiles = Server.CreateObject("ADODB.Recordset")
    rstFiles.Fields.Append "name", adVarChar, 255
    rstFiles.Fields.Append "size", adInteger
    rstFiles.Fields.Append "date", adDate
    rstFiles.Fields.Append "type", adVarChar, 255
    rstFiles.Open
    For Each objItem In objFolder.Files
    rstFiles.AddNew
    rstFiles.Fields("name").Value = objItem.Name
    rstFiles.Fields("size").Value = objItem.Size
    rstFiles.Fields("date").Value = objItem.DateCreated
    rstFiles.Fields("type").Value = objItem.Type
    Next 'objItem
    Set objItem = Nothing
    Set objFolder = Nothing
    Set objFSO = Nothing
    rstFiles.Sort = "date DESC"
    rstFiles.MoveNext
    Do While Not rstFiles.EOF
    %>
    <li><font face="Arial"><a href="<%= strPath & rstFiles.Fields("name").Value %>"><%= rstFiles.Fields("name").Value %></a></font></li>
    <%
    rstFiles.MoveNext
    Loop
    rstFiles.Close
    Set rstFiles = Nothing
    %>
     
  2. PhoenixTank

    PhoenixTank From The Ashes

    Joined:
    5 May 2010
    Posts:
    465
    Likes Received:
    28
    Code:
    rstFiles.Sort = "date DESC"
    rstFiles.MoveNext
    Do While Not rstFiles.EOF
    %>
    <li><font face="Arial"><a href="<%= strPath & rstFiles.Fields("name").Value %>"><%= rstFiles.Fields("name").Value %></a></font></li>
    <%
    rstFiles.MoveNext
    Loop
    rstFiles.Close
    Easiest way (but probably not the best) would be to change the above code to just output the first object.

    The first line is sorting the files by date, so removing the following should just output the first.

    Code:
    Do While Not rstFiles.EOF
    and
    Code:
    rstFiles.MoveNext
    Loop
     
    Silver51 likes this.
  3. Silver51

    Silver51 I cast flare!

    Joined:
    24 Jul 2006
    Posts:
    2,962
    Likes Received:
    287
    Thanks, that works. :thumb:
     
  4. Zoon

    Zoon Hunting Wabbits since the 80s

    Joined:
    12 Mar 2001
    Posts:
    5,888
    Likes Received:
    824
    If you still want to maintain the full archive, you should read each line into an array, then sort the array by DateCreated, and then when you do a For Each loop on the array, index 0 will always be the newest item, so you can make the font bigger and add a line break, then continue the rest afterwards.
     
    Silver51 likes this.
  5. Silver51

    Silver51 I cast flare!

    Joined:
    24 Jul 2006
    Posts:
    2,962
    Likes Received:
    287
    I would, but... I have no idea how to do it. ASP isn't something I ever got into. I'd much rather do this in PHP, but unfortunately they're not about to change hosting any time soon.

    Also, is there any way of arranging by name, so that the higher numbers such as 'file 094' is listed above 'file 001'?
     
  6. DarkLord7854

    DarkLord7854 What's a Dremel?

    Joined:
    22 Jun 2005
    Posts:
    4,643
    Likes Received:
    121
    ASP VB is not very pretty.. I have a JScript-based version of what you're trying to do, and I insert it into an array and then sort it, however, I can't find it.

    Are you sure their hosting doesn't do PHP? Pretty much all hosts support PHP nowadays.
     
    Silver51 likes this.
  7. Silver51

    Silver51 I cast flare!

    Joined:
    24 Jul 2006
    Posts:
    2,962
    Likes Received:
    287
    Nope, educational hosting, but a legacy thing. It's an IIS host using basic ASP and has only a singe folder with write access. Personally I'd rather tear it down and start over with PHP and CSS, but the school isn't about to change host any time soon. I'm basically trying to get it tidied up for mum, as she's the one tasked with maintaining the site.
     
  8. Zoon

    Zoon Hunting Wabbits since the 80s

    Joined:
    12 Mar 2001
    Posts:
    5,888
    Likes Received:
    824
    Okay well in that case then your easiest method of sorting the table is going to be TableSorter (http://tablesorter.com/docs/) IMO.

    That makes it completely client side, and as its javascript and a little CSS (which can be edited into that single file directly) then it will just work as it does now for anyone who has an ancient browser.

    First, download or link to the minimised version of http://jquery.com/

    Code:
    <head>
    	<link rel="stylesheet" href="/path/to/tablesorter.css" type="text/css"/>
    	<script src="/path/to/jquery.min.js"></script>
    	<script src="/path/to/jquery.tablesorter.min.js"></script>
    	$(document).ready(function() {
    	
    		// Tablesorter
    
    		$(".tablesorter").tablesorter({widgets: ['zebra']});
     
    	});
    </head>
    Then add class="tablesorter" into the table its outputting the data in.

    You'll need to update the table code to use extended tables for it to work eg:
    Code:
    <table>
    <thead>
    <tr>
    <th> Column 1 </th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td> data 1 </td>
    </tr>
    </tbody>
    </table>
    If its not doing a table it should be easy enough to insert the tags around it, but if not, please do say, and I'll try and help with that!

    You can use tablesorter to add a default sort, so you can have it sort by the date column as the page renders. You'll need to do more research than I have thus far however, as I'm only using it to start a basic alphabetical list at the moment.
     
    Silver51 likes this.
  9. Silver51

    Silver51 I cast flare!

    Joined:
    24 Jul 2006
    Posts:
    2,962
    Likes Received:
    287
    Okay, thanks for the suggestions but I've got it sorting by descending name.

    rstFiles.Sort = "name DESC"

    Soo, yeah. Doh.

    Basically, having it sort by date uploaded was causing problems when a file was replaced and the list didn't work. Sorting by name gets round that using ASC or DESC for ascending or descending.

    But the part that's now causing a problem is the page that's supposed to only display the latest newsletter. The main list gets all of them, but the single file only displays the second to last file, in this case Newsletter 092 (093 is missing,) where it should be Newsletter 094.

    http://www.marazion.cornwall.sch.uk/databases/newsdownload/test.asp

    Can fudge it by adding a txt file with zArchive and some random text about it being an archive (!) but have no idea how robust that'd be.
     
  10. Zoon

    Zoon Hunting Wabbits since the 80s

    Joined:
    12 Mar 2001
    Posts:
    5,888
    Likes Received:
    824
    What's your exact bit of code to get only the most recent one?

    Did you duplicate the page and just have it stop after one entry?
     
  11. Silver51

    Silver51 I cast flare!

    Joined:
    24 Jul 2006
    Posts:
    2,962
    Likes Received:
    287
    Yep, duplicated the original code without "Do While Not rstFiles.EOF" and "rstFiles.MoveNext"
    "Loop" and with the sort changed to name descending.


    Code:
    <%
    Dim strPath 'Path of directory to show
    Dim objFSO 'FileSystemObject variable
    Dim objFolder 'Folder variable
    Dim objItem 'Variable used to loop through the contents of the folder
    Dim rstFiles
    Const adVarChar = 200
    Const adInteger = 3
    Const adDate = 7
    strPath = "./newsletters/"
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(Server.MapPath(strPath))
    %>
    <font face="Arial">Current newsletter:</font><br />
    <br />
    <%
    For Each objItem In objFolder.SubFolders
    If InStr(1, objItem, "_vti", 1) = 0 Then
    %>
    <a href="<%= strPath & objItem.Name %>"><%= objItem.Name %></a>
    <%= objItem.Size %>
    <%= objItem.DateCreated %>
    <%= objItem.Type %>
    <%
    End If
    Next 'objItem
    Set rstFiles = Server.CreateObject("ADODB.Recordset")
    rstFiles.Fields.Append "name", adVarChar, 255
    rstFiles.Fields.Append "size", adInteger
    rstFiles.Fields.Append "date", adDate
    rstFiles.Fields.Append "type", adVarChar, 255
    rstFiles.Open
    For Each objItem In objFolder.Files
    rstFiles.AddNew
    rstFiles.Fields("name").Value = objItem.Name
    rstFiles.Fields("size").Value = objItem.Size
    rstFiles.Fields("date").Value = objItem.DateCreated
    rstFiles.Fields("type").Value = objItem.Type
    Next 'objItem
    Set objItem = Nothing
    Set objFolder = Nothing
    Set objFSO = Nothing
    rstFiles.Sort = "name DESC"
    rstFiles.MoveNext
    
    %>
    <li><font face="Arial"><a href="<%= strPath & rstFiles.Fields("name").Value %>"><%= rstFiles.Fields("name").Value %></a></font></li>
    <%
    
    rstFiles.Close
    Set rstFiles = Nothing
    %>
     
  12. PhoenixTank

    PhoenixTank From The Ashes

    Joined:
    5 May 2010
    Posts:
    465
    Likes Received:
    28
    Try swapping out
    Code:
    rstFiles.MoveNext
    for
    Code:
    rstFiles.MoveFirst
     
    Silver51 likes this.
  13. Silver51

    Silver51 I cast flare!

    Joined:
    24 Jul 2006
    Posts:
    2,962
    Likes Received:
    287
    Thanks again, that's got it.
     
  14. PhoenixTank

    PhoenixTank From The Ashes

    Joined:
    5 May 2010
    Posts:
    465
    Likes Received:
    28
    Cool - glad to help!
     

Share This Page