Directory list sort order

Discussion in 'ASP General' started by Jake, Jan 29, 2005.

  1. Jake

    Jake Guest

    Im using the following code to display the contents of a directory:

    <%
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set objFiles = objFso.GetFolder(Server.MapPath("."))
    Set fileList = objFiles.Files
    For Each i in fileList
    response.write i.name & " - " & i.DateCreated & "<br>"
    next
    %>
    Is there a way I can sort this by Date Created so the most recent files are
    on top?

    Thanks!
    Jake, Jan 29, 2005
    #1
    1. Advertising

  2. Jake

    McKirahan Guest

    "Jake" <> wrote in message
    news:#...
    > Im using the following code to display the contents of a directory:
    >
    > <%
    > Set objFso = CreateObject("Scripting.FileSystemObject")
    > Set objFiles = objFso.GetFolder(Server.MapPath("."))
    > Set fileList = objFiles.Files
    > For Each i in fileList
    > response.write i.name & " - " & i.DateCreated & "<br>"
    > next
    > %>
    > Is there a way I can sort this by Date Created so the most recent files

    are
    > on top?
    >
    > Thanks!


    Normally, I'd use ADO to sort the file names but ...

    Files are returned in ascending DateCreated sequence;
    thus, building an array and report it backwards will work.


    <%
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set objFiles = objFso.GetFolder(Server.MapPath("."))
    Set fileList = objFiles.Files
    '*
    Dim arrFiles()
    Dim intFiles
    intFiles = 0
    For Each i In fileList
    intFiles = intFiles + 1
    ReDim Preserve arrFiles(intFiles)
    arrFiles(intFiles) = i.name & " - " & i.DateCreated
    Next
    '*
    For intFiles = UBound(arrFiles) To 1 Step -1
    response.write arrFiles(intFiles) & "<br>"
    Next
    '*
    Set objFso = Nothing
    Set objFiles = Nothing
    %>
    McKirahan, Jan 29, 2005
    #2
    1. Advertising

  3. Jake

    Jake Guest

    Thanks for the reply -
    Any way to sort this by Date Created Acending? Its now sorting by filename.



    "McKirahan" <> wrote in message
    news:...
    > "Jake" <> wrote in message
    > news:#...
    >> Im using the following code to display the contents of a directory:
    >>
    >> <%
    >> Set objFso = CreateObject("Scripting.FileSystemObject")
    >> Set objFiles = objFso.GetFolder(Server.MapPath("."))
    >> Set fileList = objFiles.Files
    >> For Each i in fileList
    >> response.write i.name & " - " & i.DateCreated & "<br>"
    >> next
    >> %>
    >> Is there a way I can sort this by Date Created so the most recent files

    > are
    >> on top?
    >>
    >> Thanks!

    >
    > Normally, I'd use ADO to sort the file names but ...
    >
    > Files are returned in ascending DateCreated sequence;
    > thus, building an array and report it backwards will work.
    >
    >
    > <%
    > Set objFso = CreateObject("Scripting.FileSystemObject")
    > Set objFiles = objFso.GetFolder(Server.MapPath("."))
    > Set fileList = objFiles.Files
    > '*
    > Dim arrFiles()
    > Dim intFiles
    > intFiles = 0
    > For Each i In fileList
    > intFiles = intFiles + 1
    > ReDim Preserve arrFiles(intFiles)
    > arrFiles(intFiles) = i.name & " - " & i.DateCreated
    > Next
    > '*
    > For intFiles = UBound(arrFiles) To 1 Step -1
    > response.write arrFiles(intFiles) & "<br>"
    > Next
    > '*
    > Set objFso = Nothing
    > Set objFiles = Nothing
    > %>
    >
    >
    Jake, Jan 29, 2005
    #3
  4. Jake

    McKirahan Guest

    "Jake" <> wrote in message
    news:...
    > Thanks for the reply -
    > Any way to sort this by Date Created Acending? Its now sorting by

    filename.
    >
    >
    >
    > "McKirahan" <> wrote in message
    > news:...
    > > "Jake" <> wrote in message
    > > news:#...
    > >> Im using the following code to display the contents of a directory:
    > >>
    > >> <%
    > >> Set objFso = CreateObject("Scripting.FileSystemObject")
    > >> Set objFiles = objFso.GetFolder(Server.MapPath("."))
    > >> Set fileList = objFiles.Files
    > >> For Each i in fileList
    > >> response.write i.name & " - " & i.DateCreated & "<br>"
    > >> next
    > >> %>
    > >> Is there a way I can sort this by Date Created so the most recent files

    > > are
    > >> on top?
    > >>
    > >> Thanks!

    > >
    > > Normally, I'd use ADO to sort the file names but ...
    > >
    > > Files are returned in ascending DateCreated sequence;
    > > thus, building an array and report it backwards will work.


    [snip]

    I guess it's only my PC that displays files by DateCreated.

    Here's a subroutine that may do what you want.

    <%@ Language="VBScript" %>
    <% Option Explicit
    Call FileSort(".")

    Sub FileSort(folder)
    '****
    '*
    '* This subroutine lists files in a folder by DateCreated.
    '*
    '****
    '*
    '* Declare Constants
    '*
    Const cRS1 = "DateCreated"
    Const cRS2 = "FileName"
    '*
    Const adChar = 129
    Const adDate = 7
    Const adLockBatchOptimistic = 4
    Const adOpenStatic = 3
    Const adUseClient = 3
    '*
    '* Declare Objects
    '*
    Dim objFSO
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objGFO
    Set objGFO = objFso.GetFolder(Server.MapPath(folder))
    Dim objFIL
    Set objFIL = objGFO.Files
    Dim objRST
    Set objRST = CreateObject("ADODB.RecordSet")
    objRST.CursorLocation = adUseClient
    objRST.LockType = adLockBatchOptimistic
    objRST.CursorType = adOpenStatic
    objRST.ActiveConnection = Nothing
    objRST.Fields.Append cRS1, adDate
    objRST.Fields.Append cRS2, adChar, 255
    objRST.Open
    '*
    '* Files
    '*
    Dim intFIL
    For Each intFIL In objFIL
    objRST.AddNew
    objRST.Fields(cRS1) = intFIL.DateCreated
    objRST.Fields(cRS2) = intFIL.name
    objRST.Update
    Next
    '*
    '* Sort RecordSet
    '*
    objRST.Sort = cRS1 & " DESC"
    objRST.MoveFirst
    '*
    '* Read RecordSet
    '*
    Do While Not objRST.EOF
    Response.Write("<br>" & Trim(objRST.Fields(cRS2)) & " = " &
    objRST.Fields(cRS1))
    objRST.MoveNext
    Loop
    '*
    '* Destroy Objects
    '*
    objRST.Close
    Set objRST = Nothing
    Set objFIL = Nothing
    Set objGFO = Nothing
    Set objFSO = Nothing
    End Sub
    %>


    Of course you could always Shell out to the "dir" command.

    <%@ Language="VBScript" %>
    <% Option Explicit
    Const cTXT = "~dir_o-d.txt"
    Const cWSS = "%comspec% /c dir /o-d > "
    Dim strWSS
    strWSS = Server.MapPath(cTXT)
    Dim objWSS
    Set objWSS = CreateObject("WScript.Shell")
    objWSS.Run cWSS & strWSS, 0, True
    Set objWSS = Nothing
    %>

    Then use FSO to read this file.
    McKirahan, Jan 29, 2005
    #4
  5. Jake

    Jake Guest

    Thanks!


    "McKirahan" <> wrote in message
    news:...
    > "Jake" <> wrote in message
    > news:...
    >> Thanks for the reply -
    >> Any way to sort this by Date Created Acending? Its now sorting by

    > filename.
    >>
    >>
    >>
    >> "McKirahan" <> wrote in message
    >> news:...
    >> > "Jake" <> wrote in message
    >> > news:#...
    >> >> Im using the following code to display the contents of a directory:
    >> >>
    >> >> <%
    >> >> Set objFso = CreateObject("Scripting.FileSystemObject")
    >> >> Set objFiles = objFso.GetFolder(Server.MapPath("."))
    >> >> Set fileList = objFiles.Files
    >> >> For Each i in fileList
    >> >> response.write i.name & " - " & i.DateCreated & "<br>"
    >> >> next
    >> >> %>
    >> >> Is there a way I can sort this by Date Created so the most recent
    >> >> files
    >> > are
    >> >> on top?
    >> >>
    >> >> Thanks!
    >> >
    >> > Normally, I'd use ADO to sort the file names but ...
    >> >
    >> > Files are returned in ascending DateCreated sequence;
    >> > thus, building an array and report it backwards will work.

    >
    > [snip]
    >
    > I guess it's only my PC that displays files by DateCreated.
    >
    > Here's a subroutine that may do what you want.
    >
    > <%@ Language="VBScript" %>
    > <% Option Explicit
    > Call FileSort(".")
    >
    > Sub FileSort(folder)
    > '****
    > '*
    > '* This subroutine lists files in a folder by DateCreated.
    > '*
    > '****
    > '*
    > '* Declare Constants
    > '*
    > Const cRS1 = "DateCreated"
    > Const cRS2 = "FileName"
    > '*
    > Const adChar = 129
    > Const adDate = 7
    > Const adLockBatchOptimistic = 4
    > Const adOpenStatic = 3
    > Const adUseClient = 3
    > '*
    > '* Declare Objects
    > '*
    > Dim objFSO
    > Set objFSO = CreateObject("Scripting.FileSystemObject")
    > Dim objGFO
    > Set objGFO = objFso.GetFolder(Server.MapPath(folder))
    > Dim objFIL
    > Set objFIL = objGFO.Files
    > Dim objRST
    > Set objRST = CreateObject("ADODB.RecordSet")
    > objRST.CursorLocation = adUseClient
    > objRST.LockType = adLockBatchOptimistic
    > objRST.CursorType = adOpenStatic
    > objRST.ActiveConnection = Nothing
    > objRST.Fields.Append cRS1, adDate
    > objRST.Fields.Append cRS2, adChar, 255
    > objRST.Open
    > '*
    > '* Files
    > '*
    > Dim intFIL
    > For Each intFIL In objFIL
    > objRST.AddNew
    > objRST.Fields(cRS1) = intFIL.DateCreated
    > objRST.Fields(cRS2) = intFIL.name
    > objRST.Update
    > Next
    > '*
    > '* Sort RecordSet
    > '*
    > objRST.Sort = cRS1 & " DESC"
    > objRST.MoveFirst
    > '*
    > '* Read RecordSet
    > '*
    > Do While Not objRST.EOF
    > Response.Write("<br>" & Trim(objRST.Fields(cRS2)) & " = " &
    > objRST.Fields(cRS1))
    > objRST.MoveNext
    > Loop
    > '*
    > '* Destroy Objects
    > '*
    > objRST.Close
    > Set objRST = Nothing
    > Set objFIL = Nothing
    > Set objGFO = Nothing
    > Set objFSO = Nothing
    > End Sub
    > %>
    >
    >
    > Of course you could always Shell out to the "dir" command.
    >
    > <%@ Language="VBScript" %>
    > <% Option Explicit
    > Const cTXT = "~dir_o-d.txt"
    > Const cWSS = "%comspec% /c dir /o-d > "
    > Dim strWSS
    > strWSS = Server.MapPath(cTXT)
    > Dim objWSS
    > Set objWSS = CreateObject("WScript.Shell")
    > objWSS.Run cWSS & strWSS, 0, True
    > Set objWSS = Nothing
    > %>
    >
    > Then use FSO to read this file.
    >
    >
    Jake, Jan 29, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brett
    Replies:
    4
    Views:
    3,250
    Brett
    May 5, 2005
  2. nobody
    Replies:
    0
    Views:
    534
    nobody
    Jun 1, 2004
  3. Frank & Janny Plaza
    Replies:
    4
    Views:
    619
    Kenneth Hutson
    Sep 23, 2003
  4. =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=

    [B,IX] = sort(A,...) - Order for sort()-function

    =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=, May 29, 2007, in forum: Python
    Replies:
    0
    Views:
    319
    =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=
    May 29, 2007
  5. Navin
    Replies:
    1
    Views:
    684
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page