Sorting through a folder of files to display on webpage

Discussion in 'C#' started by drnibbles, Nov 18, 2007.

  1. #1
    Hi

    I'm trying to list files from a folder on a asp webapge and I have come so far that I can list all the files in a folder. However I would like to limit the list to only 5 files even if there are 300 files in the folder. I'm a newbe and not really sure what i'm doing. Below is the code... any help would be great


    <%
    Dim strPath2007
    Dim objFSO2007
    Dim objFolder2007
    Dim objItem2007
    Dim rstFiles2007
    Const adVarChar2007 = 200
    Const adInteger2007 = 3
    Const adDate2007 = 7

    ' NOTE: As currently implemented, this needs to end with the /
    strPath2007 = "./media/2007/announcements/"

    ' Create our FSO
    Set objFSO2007 = Server.CreateObject("Scripting.FileSystemObject")


    ' Get a handle on our folder
    Set objFolder2007 = objFSO2007.GetFolder(Server.MapPath(strPath2007))


    %>

    <%

    Set rstFiles2007 = Server.CreateObject("ADODB.Recordset")
    rstFiles2007.Fields.Append "name", adVarChar2007, 255
    rstFiles2007.Fields.Append "size", adInteger2007
    rstFiles2007.Fields.Append "date", adDate2007
    rstFiles2007.Fields.Append "type", adVarChar2007, 255
    rstFiles2007.Open



    For Each objItem2007 In objFolder2007.Files
    rstFiles2007.AddNew
    rstFiles2007.Fields("name").Value = objItem2007.Name
    rstFiles2007.Fields("size").Value = objItem2007.Size
    rstFiles2007.Fields("date").Value = objItem2007.DateLastModified


    If Len(objItem2007.Type) > 17 Then
    rstFiles2007.Fields("type").Value = Left(objItem2007.Type, 14) & ""
    Else
    rstFiles2007.Fields("type").Value = objItem2007.Type
    End If
    Next 'objItem2007

    ' All done! Kill off our File System Object variables.
    Set objItem2007 = Nothing
    Set objFolder2007 = Nothing
    Set objFSO2007 = Nothing

    ' Now we can sort our data and display it:

    rstFiles2007.Sort = "date DESC"

    rstFiles2007.MoveFirst

    Do While Not rstFiles2007.EOF

    %>
    <dl class="Announcements">
    <dt><%= FormatDateTime(rstFiles2007.Fields("date").Value,2) %></dt>
    <dd><a class="doc" href="<%= strPath2007 & rstFiles2007.Fields("name").Value %>" target="_blank"><%= rstFiles2007.Fields("name").Value %></a></dd>
    </dl>
    <%
    rstFiles2007.MoveNext
    Loop

    ' Close the ADO Recordset object
    rstFiles2007.Close
    Set rstFiles2007 = Nothing
    %>
     
    drnibbles, Nov 18, 2007 IP
  2. drnibbles

    drnibbles Peon

    Messages:
    346
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    So there is no one that can help me :-O
     
    drnibbles, Nov 19, 2007 IP
  3. orielo

    orielo Peon

    Messages:
    175
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hey dude,
    this is quite simple, all you need is a counter, change this code :
    Do While Not rstFiles2007.EOF
    
    %>
    <dl class="Announcements">
    <dt><%= FormatDateTime(rstFiles2007.Fields("date").Value,2) %></dt>
    <dd><a class="doc" href="<%= strPath2007 & rstFiles2007.Fields("name").Value %>" target="_blank"><%= rstFiles2007.Fields("name").Value %></a></dd>
    </dl> 
    <%
    rstFiles2007.MoveNext
    Loop
    Code (markup):
    To This :

    Didnt tested it , but it should work.
     
    orielo, Nov 20, 2007 IP
  4. drnibbles

    drnibbles Peon

    Messages:
    346
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    yea thank you sooooo much
     
    drnibbles, Nov 20, 2007 IP
  5. orielo

    orielo Peon

    Messages:
    175
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    no problem mate.
    gl.
     
    orielo, Nov 20, 2007 IP
    drnibbles likes this.
  6. drnibbles

    drnibbles Peon

    Messages:
    346
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I was a bit to fast on this. I did never check that it did what I wanted. I does actually do nothing. It shows as many files as before. I just didn't count them when i stuck your code in there :) So after testing the code bit today i see that it does nothing, and it does not break the page either.
     
    drnibbles, Nov 21, 2007 IP
  7. drnibbles

    drnibbles Peon

    Messages:
    346
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I solved it like this

    n=0
    Do until n = 5

    %>
    <dl class="Announcements">
    <dt><%= FormatDateTime(rstFiles2007.Fields("date").Value,2) %></dt>
    <dd><a class="doc" href="<%= strPath2007 & rstFiles2007.Fields("name").Value %>" target="_blank"><%= rstFiles2007.Fields("name").Value %></a></dd>
    </dl>
    <%
    rstFiles2007.MoveNext
    n = n + 1
    Loop
     
    drnibbles, Nov 21, 2007 IP
  8. orielo

    orielo Peon

    Messages:
    175
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Just to not confuse any noobs :

    problem eventually solved like :

    Do until (rsFiles2007.EOF or rsFiles2007.BOF or n=5) 
    Code (markup):
     
    orielo, Nov 22, 2007 IP