Directory list sort order

J

Jake

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!
 
M

McKirahan

Jake said:
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
%>
 
J

Jake

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

McKirahan

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

[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.
 
J

Jake

Thanks!


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

[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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top