Solved

FSO Object - Order by date created

Posted on 2004-10-25
352 Views
Last Modified: 2008-02-26
I'm using FSO object to list files in a folder. Is there a way to list the files by date created instead of the file name?

Here is my code...

<%
      Dim okToShow
      okToShow="Yes"

            'Build table for files
                  response.write "<table width=500>"
            'Header Row      
                  response.Write "<tr>"
                  response.write "<td width=250>" & "<b>" & "<font color=#000000>" & "Created" &  "</font>" & "</b>" & "</td>"
                  response.write "<td width=150>" & "<b>" & "<font color=#000000>" & "Files" & "</font>" & "</b>" & "</td>"
                  response.write "<td width=100>" & "<b>" & "<font color=#000000>" & "Size" &  "</font>" & "</b>" & "</td>"
                  response.write "<td width=100>" & "<b>" & "<font color=#000000>" & "Delete?" & "</font>" & "</td>"
                  response.write "</tr>"
            
            ' Show the Folders and files subroutine:
            Sub ShowFile(file)
                   if file = "upload_success.asp" or file = "deletefile.asp" then
                  If okToShow="Yes" Then
                  response.Write "<tr>"
                  response.Write "<td width=600 colspan=4>" & "</td>"
                  Response.Write "</tr>"
                  okToShow="No"
                  End If
               
                     else
               'write table row with files available

                  response.Write "<tr>"
                  response.write "<td width=250>" & "<font color=#000000>" & objFile.DateCreated &  "</font>" & "</td>"
                  response.Write "<td width=150>" & "<a href=""" & file & """>" & file & "</a>" & "</td>"
                  response.write "<td width=100>" & "<font color=#000000>" & objFile.Size & " bytes" & "</font>" & "</td>"
                  response.write "<td width=100><font color=#000000><a href='deletefile.asp?file=" & server.urlencode(objFile.Path) & "'>DELETE</a></font></td>"
                  Response.Write "</tr>"
                  end if
            End Sub
                  
            Const DIR = "./"

            ' Variables for our FileSystemObject objects
            Dim objFSO, objFolderObject, objFileCollection, objFile, objSubF

            ' A pair of integers for our random image selection
            Dim intFileNumberToUse, intFileLooper

            ' A "handle" to the file we choose to use
            Dim objImageFileToUse

            ' A variable to build our image tag
            Dim strImageSrcText

            ' Lets see what's in the directory:
            Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
            Set objFolderObject = objFSO.GetFolder(Server.MapPath(DIR))
            
            Set objFileCollection = objFolderObject.Files
            
            'Files within this folder
            For Each objFile in objFileCollection
                        ' Get a "handle" on the appropriate file
                        Set objImageFileToUse = objFile
                        If objImageFileToUse.Name <> "index.asp" and objImageFileToUse.Name <> "index.htm" Then
                              ShowFile(objImageFileToUse.Name)
                        End If
                        
            Next

            Set objFileCollection = Nothing
            Set objImageFileToUse = Nothing
            Set objFSO = Nothing
            Set objFolderObject = Nothing

            response.write "</table>"
            %></p>

Thanks
j
0
Question by:kevinbenedict
    7 Comments
     
    LVL 14

    Expert Comment

    by:dfu23
    I don't think that you can sort the FSO object but you could read the results into an array and sort from there ...
    0
     

    Author Comment

    by:kevinbenedict
    Could you direct me how to do that as I'm just figuring this stuff out.

    Thanks
    0
     
    LVL 14

    Accepted Solution

    by:
    If you look at the example here:

    http://www.asp101.com/samples/viewasp.asp?file=dir%5Flist%5Fsort%2Easp

    You'll see that he uses a RecordSet to read the results of the FileSystemObject into ... and then sorts the RecordSet ... it should work fine for your purposes too. You would probably just want to do:

    rstFiles.Sort = "date ASC"

    OR

    rstFiles.Sort = "date DESC"
    0
     

    Author Comment

    by:kevinbenedict
    Thanks for the link. I'll check it out.
    0
     

    Author Comment

    by:kevinbenedict
    I'm real close to getting this resolved. I'm getting an "object expected error"  What am I missing on this line?

    <td align="left" ><a href='deletefile.asp?file="<%= strPath & rst.Files.Fields("name").Value %>"'>Delete</a></td>

    thanks
    j
    0
     
    LVL 14

    Expert Comment

    by:dfu23
    Did you already figure this part out? Is it something like the following?

    <td align="left" ><a href='deletefile.asp?file="<%= strPath & rstFiles.Fields("name").Value %>"'>Delete</a></td>
    0
     

    Author Comment

    by:kevinbenedict
    Yes. Sorry about that.

    Thanks for your help.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    Select and format date 7 34
    Summer note focus on load 19 112
    API Location, separating out information 10 34
    Issue with Loop 4 26
    I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
    This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
    This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now